This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Benchmark.swift
88 lines (71 loc) · 2.69 KB
/
Benchmark.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
//
// JSONBenchmark.swift
// FreddyTests
//
// Created by Zachary Waldowski on 5/20/15.
// Copyright (c) 2015 Big Nerd Ranch Inc. All rights reserved.
//
import XCTest
import Freddy
private enum ErrorFromObjCModel: ErrorType {
case FailedToDecode
}
class JSONBenchmark: XCTestCase {
private var jsonData: NSData!
override func setUp() {
super.setUp()
let testBundle = NSBundle(forClass: JSONBenchmark.self)
guard let data = testBundle.URLForResource("AllSetsArray", withExtension: "json", subdirectory: "Benchmark").flatMap(NSData.init) else {
XCTFail("Could not read stress test data from test bundle")
return
}
jsonData = data
}
private func measureWithoutRR<T>(body: () throws -> T, assertions: (T -> ())?) {
measureMetrics(self.dynamicType.defaultPerformanceMetrics(), automaticallyStartMeasuring: false) {
var value: T!
self.startMeasuring()
value = try? body()
self.stopMeasuring()
XCTAssertNotNil(value)
assertions?(value)
value = nil
}
}
func testJSONDeserializeCocoa() {
measureWithoutRR({
try NSJSONSerialization.JSONObjectWithData(self.jsonData, options: [])
}, assertions: nil)
}
func testJSONDeserializeCustom() {
measureWithoutRR({
try JSON(data: self.jsonData, usingParser: JSONParser.self)
}, assertions: nil)
}
func testJSONDeserializeCustomViaCocoa() {
measureWithoutRR({
try JSON(data: self.jsonData, usingParser: NSJSONSerialization.self)
}, assertions: nil)
}
func testJSONDeserializeToCocoaModel() {
measureWithoutRR({ () throws -> ([CardSetObjC], Int) in
let cocoaJSON = try NSJSONSerialization.JSONObjectWithData(self.jsonData, options: []) as! [[String: AnyObject]]
guard let objects = CardSetObjC.cardSetsFromDictionaries(cocoaJSON) else {
throw ErrorFromObjCModel.FailedToDecode
}
return (objects, cocoaJSON.count)
}, assertions: {
XCTAssertEqual($0.0.count, $0.1, "Failed to convert all sets")
})
}
func testJSONDeserializeToModel() {
measureWithoutRR({ () throws -> ([CardSet], Int) in
let json = try JSON(data: self.jsonData, usingParser: JSONParser.self)
let expectedCount = try json.array().count
let objects = try json.arrayOf(type: CardSet.self)
return (objects, expectedCount)
}, assertions: {
XCTAssertEqual($0.0.count, $0.1, "Failed to convert all sets")
})
}
}