/
PerformanceTests.swift
174 lines (149 loc) · 5.11 KB
/
PerformanceTests.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
//
// PerformanceTests.swift
// ObjectMapper
//
// Created by Tristan Himmelman on 2015-09-21.
//
// The MIT License (MIT)
//
// Copyright (c) 2014-2018 Tristan Himmelman
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
import XCTest
import ObjectMapper
class PerformanceTests: XCTestCase {
let JSONTestString: String = {
let subObjectJSON = "{\"string\":\"This is a string\", \"int\": 12,\"double\":12.27,\"float\":12.3212, \"bool\":false, \"arr\":[ \"bla\", true, 42 ], \"dict\":{ \"key1\" : \"value1\", \"key2\" : false, \"key3\" : 142 } }"
let objectJSONString = "{\"string\":\"This is a string\", \"int\": 12,\"double\":12.27,\"float\":12.3212, \"bool\":false, \"arr\":[ \"bla\", true, 42 ], \"dict\":{ \"key1\" : \"value1\", \"key2\" : false, \"key3\" : 142 }, \"object\": \(subObjectJSON), \"objects\":{ \"key1\": \(subObjectJSON), \"key2\": \(subObjectJSON)}}"
var JSONString = "["
for _ in 0...1000 {
JSONString += "\(objectJSONString),"
}
JSONString += "\(objectJSONString)]"
return JSONString
}()
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testPerformance() {
self.measure {
// Put the code you want to measure the time of here.
_ = Mapper<PerformanceMappableObject>().mapArray(JSONString: self.JSONTestString)
}
}
func testPerformanceCluster() {
self.measure {
// Put the code you want to measure the time of here.
_ = Mapper<PerformanceStaticMappableObject>().mapArray(JSONString: self.JSONTestString)
}
}
func testPerformanceImmutable() {
self.measure {
_ = try? Mapper<PerformanceImmutableMappableObject>().mapArray(JSONString: self.JSONTestString)
}
}
}
class PerformanceMappableObject: Mappable {
var string: String?
var int: Int?
var double: Double?
var float: Float?
var bool: Bool?
var array: [Any]?
var dictionary: [String: Any]?
var object: PerformanceMappableObject?
var objects: [PerformanceMappableObject]?
required init?(map: Map){
}
func mapping(map: Map) {
string <- map["string"]
int <- map["int"]
double <- map["double"]
float <- map["float"]
bool <- map["bool"]
array <- map["array"]
dictionary <- map["dictionary"]
object <- map["object"]
objects <- map["objects"]
}
}
class PerformanceStaticMappableObject: StaticMappable {
var string: String?
var int: Int?
var double: Double?
var float: Float?
var bool: Bool?
var array: [Any]?
var dictionary: [String: Any]?
var object: PerformanceStaticMappableObject?
var objects: [PerformanceStaticMappableObject]?
static func objectForMapping(map: Map) -> BaseMappable? {
return PerformanceStaticMappableObject()
}
func mapping(map: Map) {
string <- map["string"]
int <- map["int"]
double <- map["double"]
float <- map["float"]
bool <- map["bool"]
array <- map["array"]
dictionary <- map["dictionary"]
object <- map["object"]
objects <- map["objects"]
}
}
class PerformanceImmutableMappableObject: ImmutableMappable {
let string: String?
let int: Int?
let double: Double?
let float: Float?
let bool: Bool?
let array: [Any]?
let dictionary: [String: Any]?
let object: PerformanceImmutableMappableObject?
let objects: [PerformanceImmutableMappableObject]?
required init(map: Map) throws {
string = try map.value("string")
int = try map.value("int")
double = try map.value("double")
float = try map.value("float")
bool = try map.value("bool")
array = try map.value("array")
dictionary = try map.value("dictionary")
object = try map.value("object")
objects = try map.value("objects")
}
func mapping(map: Map) {
string >>> map["string"]
int >>> map["int"]
double >>> map["double"]
float >>> map["float"]
bool >>> map["bool"]
array >>> map["array"]
dictionary >>> map["dictionary"]
object >>> map["object"]
objects >>> map["objects"]
}
}