-
Notifications
You must be signed in to change notification settings - Fork 104
/
BenchmarkTests.swift
243 lines (199 loc) · 6.51 KB
/
BenchmarkTests.swift
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import XCTest
@testable import XMLCoder
class BenchmarkTests: XCTestCase {
struct Container<T: Codable>: Codable {
let unkeyed: [T]
let keyed: [String: T]
}
func container<T: Codable>(with value: T) -> Container<T> {
// unkeyed.count + keyed.count = self.count:
let count = self.count / 2
let unkeyed = [T](repeating: value, count: count)
let keyed = Dictionary(uniqueKeysWithValues: unkeyed.enumerated().map { index, value in
("key_\(index)", value)
})
return Container(unkeyed: unkeyed, keyed: keyed)
}
func encodedContainer<T: Codable>(of _: T.Type, with value: T) throws -> Data {
let container = self.container(with: value)
let encoder = XMLEncoder()
encoder.outputFormatting = .prettyPrinted
return try encoder.encode(container, withRootKey: "container")
}
func testEncoding<T: Codable>(with value: T, _ closure: (() -> ()) -> ()) throws {
try testEncoding(of: T.self, with: value, closure)
}
func testEncoding<T: Codable>(of _: T.Type, with value: T, _ closure: (() -> ()) -> ()) throws {
let container: Container<T> = self.container(with: value)
let encoder = XMLEncoder()
var caughtError: Error?
closure {
do {
_ = try encoder.encode(container, withRootKey: "container")
} catch {
caughtError = error
}
}
if let error = caughtError {
throw error
}
}
func testDecoding<T: Codable>(with value: T, _ closure: (() -> ()) -> ()) throws {
try testDecoding(of: T.self, with: value, closure)
}
func testDecoding<T: Codable>(of _: T.Type, with value: T, _ closure: (() -> ()) -> ()) throws {
let data: Data = try encodedContainer(of: T.self, with: value)
let decoder = XMLDecoder()
var caughtError: Error?
closure {
do {
_ = try decoder.decode(Container<T>.self, from: data)
} catch {
caughtError = error
}
}
if let error = caughtError {
throw error
}
}
let count: Int = 1000
let null: Bool? = nil
let bool: Bool = true
let int: Int = -42
let uint: UInt = 42
let float: Float = 42.0
let decimal: Decimal = 42.0
let date: Date = Date()
let data: Data = Data(base64Encoded: "bG9yZW0gaXBzdW0=")!
let url: URL = URL(string: "http://example.com")!
let array: [Int] = [1, 2, 3, 4, 5]
let dictionary: [String: Int] = ["key_1": 1, "key_2": 2, "key_3": 3, "key_4": 4, "key_5": 5]
func testEncodeNulls() throws {
try testEncoding(with: null) { closure in
self.measure { closure() }
}
}
func testDecodeNulls() throws {
try testDecoding(with: null) { closure in
self.measure { closure() }
}
}
func testEncodeBools() throws {
try testEncoding(with: bool) { closure in
self.measure { closure() }
}
}
func testDecodeBools() throws {
try testDecoding(with: bool) { closure in
self.measure { closure() }
}
}
func testEncodeInts() throws {
try testEncoding(with: int) { closure in
self.measure { closure() }
}
}
func testDecodeInts() throws {
try testDecoding(with: int) { closure in
self.measure { closure() }
}
}
func testEncodeUInts() throws {
try testEncoding(with: uint) { closure in
self.measure { closure() }
}
}
func testDecodeUInts() throws {
try testDecoding(with: uint) { closure in
self.measure { closure() }
}
}
func testEncodeFloats() throws {
try testEncoding(with: float) { closure in
self.measure { closure() }
}
}
func testDecodeFloats() throws {
try testDecoding(with: float) { closure in
self.measure { closure() }
}
}
func testEncodeDecimals() throws {
try testEncoding(with: decimal) { closure in
self.measure { closure() }
}
}
func testDecodeDecimals() throws {
try testDecoding(with: decimal) { closure in
self.measure { closure() }
}
}
func testEncodeDates() throws {
try testEncoding(with: date) { closure in
self.measure { closure() }
}
}
func testDecodeDates() throws {
try testDecoding(with: date) { closure in
self.measure { closure() }
}
}
func testEncodeDatas() throws {
try testEncoding(with: data) { closure in
self.measure { closure() }
}
}
func testDecodeDatas() throws {
try testDecoding(with: data) { closure in
self.measure { closure() }
}
}
func testEncodeURLs() throws {
try testEncoding(with: url) { closure in
self.measure { closure() }
}
}
func testDecodeURLs() throws {
try testDecoding(with: url) { closure in
self.measure { closure() }
}
}
func testEncodeArrays() throws {
try testEncoding(with: array) { closure in
self.measure { closure() }
}
}
func testDecodeArrays() throws {
try testDecoding(with: array) { closure in
self.measure { closure() }
}
}
func testEncodeDictionaries() throws {
try testEncoding(with: dictionary) { closure in
self.measure { closure() }
}
}
func testDecodeDictionaries() throws {
try testDecoding(with: dictionary) { closure in
self.measure { closure() }
}
}
static var allTests = [
("testEncodeNulls", testEncodeNulls),
("testDecodeNulls", testDecodeNulls),
("testEncodeBools", testEncodeBools),
("testDecodeBools", testDecodeBools),
("testEncodeInts", testEncodeInts),
("testDecodeInts", testDecodeInts),
("testEncodeUInts", testEncodeUInts),
("testDecodeUInts", testDecodeUInts),
("testEncodeFloats", testEncodeFloats),
("testDecodeFloats", testDecodeFloats),
("testEncodeDecimals", testEncodeDecimals),
("testDecodeDecimals", testDecodeDecimals),
("testEncodeArrays", testEncodeArrays),
("testDecodeArrays", testDecodeArrays),
("testEncodeDictionaries", testEncodeDictionaries),
("testDecodeDictionaries", testDecodeDictionaries),
]
}