Skip to content

Generalize the JSON perf tests to Codable #20921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ set(SWIFT_BENCH_MODULES
single-source/CharacterProperties
single-source/Chars
single-source/ClassArrayGetter
single-source/Codable
single-source/Combos
single-source/CountAlgo
single-source/DataBenchmarks
Expand Down Expand Up @@ -94,7 +95,6 @@ set(SWIFT_BENCH_MODULES
single-source/InsertCharacter
single-source/Integrate
single-source/IterateData
single-source/JSON
single-source/Join
single-source/LazyFilter
single-source/LinkedList
Expand Down
146 changes: 146 additions & 0 deletions benchmark/single-source/Codable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
//===--- JSON.swift -------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import TestsUtils
import Foundation

#if _runtime(_ObjC)
public let Codable = [
BenchmarkInfo(name: "JSONPerfEncode", runFunction: run_JSONPerfEncode, tags: [.validation, .bridging], setUpFunction: setup_json),
BenchmarkInfo(name: "JSONPerfDecode", runFunction: run_JSONPerfDecode, tags: [.validation, .bridging], setUpFunction: setup_json),
BenchmarkInfo(name: "PlistPerfEncode", runFunction: run_PlistPerfEncode, tags: [.validation, .bridging], setUpFunction: setup_plist),
BenchmarkInfo(name: "PlistPerfDecode", runFunction: run_PlistPerfDecode, tags: [.validation, .bridging], setUpFunction: setup_plist),
]
#else
public let Codable = [
BenchmarkInfo(name: "JSONPerfEncode", runFunction: run_JSONPerfEncode, tags: [.validation, .bridging], setUpFunction: setup_json),
BenchmarkInfo(name: "JSONPerfDecode", runFunction: run_JSONPerfDecode, tags: [.validation, .bridging], setUpFunction: setup_json),
]
#endif



struct Tove: Codable {
var slithy: Bool = true
var gyreInRadians: Double = 0.3
}

struct Borogove : Codable {
var mimsyness: Int = Int.max
}

struct Wabe : Codable {
var toves: [Tove]
var borogoves: [Borogove]?
static var canonical: Wabe {
return Wabe(toves: [Tove(), Tove(), Tove(), Tove(),
Tove(slithy: false, gyreInRadians: 1.8)],
borogoves: [Borogove(mimsyness: 18), Borogove(mimsyness: 74),
Borogove(), Borogove()])
}
}

struct Beast : Codable {
var name: String
}


struct Jabberwocky : Codable {
var time = "brillig"
var wabe = Wabe.canonical
var beware: [Beast] = [ Beast(name: "Jabberwock"), Beast(name: "Bandersnatch"), Beast(name: "Jubjub Bird")]
var swordType = "vörpal"
}

protocol GenericEncoder {
func encode<T : Encodable>(_ value: T) throws -> Data
}

protocol GenericDecoder {
func decode<T : Decodable>(_ type: T.Type, from data: Data) throws -> T
}

extension JSONEncoder : GenericEncoder {}
extension JSONDecoder : GenericDecoder {}
#if _runtime(_ObjC)
extension PropertyListEncoder : GenericEncoder {}
extension PropertyListDecoder : GenericDecoder {}
#endif

struct CodablePerfTester<Enc: GenericEncoder, Dec: GenericDecoder> {
var poems = Array(repeating: Jabberwocky(), count: 6)
var encoder: Enc
var decoder: Dec
var data: Data! = nil

init(encoder e: Enc, decoder d: Dec) {
encoder = e
decoder = d
data = try! encoder.encode(Array(poems.prefix(3)))
//pre-warm everything to try to reduce variance
let _ = try! encoder.encode(poems)
let _ = try! decoder.decode(Array<Jabberwocky>.self,
from: data)
}

func encode() {
let _ = try! encoder.encode(poems)
}

func decode() {
let _ = try! decoder.decode(Array<Jabberwocky>.self, from: data)
}
}

var JSONTester: CodablePerfTester<JSONEncoder, JSONDecoder>! = nil

public func setup_json() {
JSONTester = CodablePerfTester(encoder: JSONEncoder(), decoder: JSONDecoder())
}

@inline(never)
public func run_JSONPerfEncode(_ N: Int) {
for _ in 0 ..< N {
JSONTester.encode()
}
}

@inline(never)
public func run_JSONPerfDecode(_ N: Int) {
for _ in 0 ..< N {
JSONTester.decode()
}
}

#if _runtime(_ObjC)

var plistTester: CodablePerfTester<PropertyListEncoder, PropertyListDecoder>! = nil

public func setup_plist() {
plistTester = CodablePerfTester(encoder: PropertyListEncoder(), decoder: PropertyListDecoder())
}

@inline(never)
public func run_PlistPerfEncode(_ N: Int) {
for _ in 0 ..< N {
plistTester.encode()
}
}

@inline(never)
public func run_PlistPerfDecode(_ N: Int) {
for _ in 0 ..< N {
plistTester.decode()
}
}

#endif
88 changes: 0 additions & 88 deletions benchmark/single-source/JSON.swift

This file was deleted.

4 changes: 2 additions & 2 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import CharacterLiteralsSmall
import CharacterProperties
import Chars
import ClassArrayGetter
import Codable
import Combos
import CountAlgo
import DataBenchmarks
Expand Down Expand Up @@ -82,7 +83,6 @@ import Histogram
import InsertCharacter
import Integrate
import IterateData
import JSON
import Join
import LazyFilter
import LinkedList
Expand Down Expand Up @@ -255,7 +255,7 @@ registerBenchmark(Histogram)
registerBenchmark(InsertCharacter)
registerBenchmark(IntegrateTest)
registerBenchmark(IterateData)
registerBenchmark(JSON)
registerBenchmark(Codable)
registerBenchmark(Join)
registerBenchmark(LazyFilter)
registerBenchmark(LinkedList)
Expand Down