File: CodableTest.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (154 lines) | stat: -rw-r--r-- 4,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//===--- CodableTest.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 benchmarks = [
  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 benchmarks = [
  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) {
  autoreleasepool {
    for _ in 0 ..< n {
      jsonTester.encode()
    }
  }
}

@inline(never)
public func run_JSONPerfDecode(_ n: Int) {
  autoreleasepool {
    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) {
  autoreleasepool {
    for _ in 0 ..< n {
      plistTester.encode()
    }
  }
}

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

#endif