|
| 1 | +//===--- JSON.swift -------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import TestsUtils |
| 14 | +import Foundation |
| 15 | + |
| 16 | +#if _runtime(_ObjC) |
| 17 | +public let Codable = [ |
| 18 | + BenchmarkInfo(name: "JSONPerfEncode", runFunction: run_JSONPerfEncode, tags: [.validation, .bridging], setUpFunction: setup_json), |
| 19 | + BenchmarkInfo(name: "JSONPerfDecode", runFunction: run_JSONPerfDecode, tags: [.validation, .bridging], setUpFunction: setup_json), |
| 20 | + BenchmarkInfo(name: "PlistPerfEncode", runFunction: run_PlistPerfEncode, tags: [.validation, .bridging], setUpFunction: setup_plist), |
| 21 | + BenchmarkInfo(name: "PlistPerfDecode", runFunction: run_PlistPerfDecode, tags: [.validation, .bridging], setUpFunction: setup_plist), |
| 22 | +] |
| 23 | +#else |
| 24 | +public let Codable = [ |
| 25 | + BenchmarkInfo(name: "JSONPerfEncode", runFunction: run_JSONPerfEncode, tags: [.validation, .bridging], setUpFunction: setup_json), |
| 26 | + BenchmarkInfo(name: "JSONPerfDecode", runFunction: run_JSONPerfDecode, tags: [.validation, .bridging], setUpFunction: setup_json), |
| 27 | +] |
| 28 | +#endif |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +struct Tove: Codable { |
| 33 | + var slithy: Bool = true |
| 34 | + var gyreInRadians: Double = 0.3 |
| 35 | +} |
| 36 | + |
| 37 | +struct Borogove : Codable { |
| 38 | + var mimsyness: Int = Int.max |
| 39 | +} |
| 40 | + |
| 41 | +struct Wabe : Codable { |
| 42 | + var toves: [Tove] |
| 43 | + var borogoves: [Borogove]? |
| 44 | + static var canonical: Wabe { |
| 45 | + return Wabe(toves: [Tove(), Tove(), Tove(), Tove(), |
| 46 | + Tove(slithy: false, gyreInRadians: 1.8)], |
| 47 | + borogoves: [Borogove(mimsyness: 18), Borogove(mimsyness: 74), |
| 48 | + Borogove(), Borogove()]) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +struct Beast : Codable { |
| 53 | + var name: String |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +struct Jabberwocky : Codable { |
| 58 | + var time = "brillig" |
| 59 | + var wabe = Wabe.canonical |
| 60 | + var beware: [Beast] = [ Beast(name: "Jabberwock"), Beast(name: "Bandersnatch"), Beast(name: "Jubjub Bird")] |
| 61 | + var swordType = "vörpal" |
| 62 | +} |
| 63 | + |
| 64 | +protocol GenericEncoder { |
| 65 | + func encode<T : Encodable>(_ value: T) throws -> Data |
| 66 | +} |
| 67 | + |
| 68 | +protocol GenericDecoder { |
| 69 | + func decode<T : Decodable>(_ type: T.Type, from data: Data) throws -> T |
| 70 | +} |
| 71 | + |
| 72 | +extension JSONEncoder : GenericEncoder {} |
| 73 | +extension JSONDecoder : GenericDecoder {} |
| 74 | +#if _runtime(_ObjC) |
| 75 | +extension PropertyListEncoder : GenericEncoder {} |
| 76 | +extension PropertyListDecoder : GenericDecoder {} |
| 77 | +#endif |
| 78 | + |
| 79 | +struct CodablePerfTester<Enc: GenericEncoder, Dec: GenericDecoder> { |
| 80 | + var poems = Array(repeating: Jabberwocky(), count: 6) |
| 81 | + var encoder: Enc |
| 82 | + var decoder: Dec |
| 83 | + var data: Data! = nil |
| 84 | + |
| 85 | + init(encoder e: Enc, decoder d: Dec) { |
| 86 | + encoder = e |
| 87 | + decoder = d |
| 88 | + data = try! encoder.encode(Array(poems.prefix(3))) |
| 89 | + //pre-warm everything to try to reduce variance |
| 90 | + let _ = try! encoder.encode(poems) |
| 91 | + let _ = try! decoder.decode(Array<Jabberwocky>.self, |
| 92 | + from: data) |
| 93 | + } |
| 94 | + |
| 95 | + func encode() { |
| 96 | + let _ = try! encoder.encode(poems) |
| 97 | + } |
| 98 | + |
| 99 | + func decode() { |
| 100 | + let _ = try! decoder.decode(Array<Jabberwocky>.self, from: data) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +var JSONTester: CodablePerfTester<JSONEncoder, JSONDecoder>! = nil |
| 105 | + |
| 106 | +public func setup_json() { |
| 107 | + JSONTester = CodablePerfTester(encoder: JSONEncoder(), decoder: JSONDecoder()) |
| 108 | +} |
| 109 | + |
| 110 | +@inline(never) |
| 111 | +public func run_JSONPerfEncode(_ N: Int) { |
| 112 | + for _ in 0 ..< N { |
| 113 | + JSONTester.encode() |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +@inline(never) |
| 118 | +public func run_JSONPerfDecode(_ N: Int) { |
| 119 | + for _ in 0 ..< N { |
| 120 | + JSONTester.decode() |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#if _runtime(_ObjC) |
| 125 | + |
| 126 | +var plistTester: CodablePerfTester<PropertyListEncoder, PropertyListDecoder>! = nil |
| 127 | + |
| 128 | +public func setup_plist() { |
| 129 | + plistTester = CodablePerfTester(encoder: PropertyListEncoder(), decoder: PropertyListDecoder()) |
| 130 | +} |
| 131 | + |
| 132 | +@inline(never) |
| 133 | +public func run_PlistPerfEncode(_ N: Int) { |
| 134 | + for _ in 0 ..< N { |
| 135 | + plistTester.encode() |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +@inline(never) |
| 140 | +public func run_PlistPerfDecode(_ N: Int) { |
| 141 | + for _ in 0 ..< N { |
| 142 | + plistTester.decode() |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#endif |
0 commit comments