forked from ReactiveCocoa/ReactiveCocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterceptingPerformanceTests.swift
148 lines (116 loc) · 3.63 KB
/
InterceptingPerformanceTests.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
import XCTest
@testable import ReactiveCocoa
import ReactiveSwift
private let iterationCount = 500
private final class Receiver1: NSObject {
@objc dynamic func message() {}
}
private final class Receiver2: NSObject {
@objc dynamic var value: Int = 0
}
private final class Receiver3: NSObject {
@objc dynamic var value: Int = 0
}
private final class Receiver4: NSObject {
@objc dynamic var value: Int = 0
}
private final class Receiver5: NSObject {
@objc dynamic var value: Int = 0
}
private final class Receiver6: NSObject {
@objc dynamic var value: Int = 0
}
class InterceptingTests: XCTestCase {
override class func setUp() {
// Swizzle all classes ahead.
// General interception.
let receiver1 = Receiver1()
_ = receiver1.reactive.trigger(for: #selector(receiver1.message))
// Setter: RAC then KVO
let receiver2 = Receiver2()
_ = receiver2.reactive.trigger(for: #selector(setter: receiver2.value))
receiver2.reactive.producer(forKeyPath: #keyPath(Receiver2.value)).start()
// Setter: KVO then RAC
let receiver3 = Receiver3()
receiver3.reactive.producer(forKeyPath: #keyPath(Receiver3.value)).start()
_ = receiver3.reactive.trigger(for: #selector(setter: receiver3.value))
// RAC swizzled getter, and then KVO swizzled setter.
let receiver4 = Receiver4()
_ = receiver4.reactive.trigger(for: #selector(getter: receiver4.value))
receiver4.reactive.producer(forKeyPath: #keyPath(Receiver4.value)).start()
// KVO swizzled setter, and then RAC swizzled getter.
let receiver5 = Receiver5()
receiver5.reactive.producer(forKeyPath: #keyPath(Receiver5.value)).start()
_ = receiver5.reactive.trigger(for: #selector(getter: receiver5.value))
// Normal KVO
let receiver6 = Receiver6()
receiver6.reactive.producer(forKeyPath: #keyPath(Receiver6.value)).start()
}
func testInterceptedMessage() {
let receiver = Receiver1()
_ = receiver.reactive.trigger(for: #selector(receiver.message))
measure {
for _ in 0 ..< iterationCount {
receiver.message()
}
}
}
func testRACKVOInterceptSetterThenSet() {
let receiver = Receiver2()
_ = receiver.reactive.trigger(for: #selector(setter: receiver.value))
receiver.reactive.producer(forKeyPath: #keyPath(Receiver2.value)).start()
measure {
for i in 0 ..< iterationCount {
receiver.value = i
}
}
}
func testKVORACInterceptSetterThenSet() {
let receiver = Receiver3()
receiver.reactive.producer(forKeyPath: #keyPath(Receiver3.value)).start()
_ = receiver.reactive.trigger(for: #selector(setter: receiver.value))
measure {
for i in 0 ..< iterationCount {
receiver.value = i
}
}
}
func testRACKVOInterceptSetterThenGet() {
let receiver = Receiver4()
_ = receiver.reactive.trigger(for: #selector(getter: receiver.value))
receiver.reactive.producer(forKeyPath: #keyPath(Receiver4.value)).start()
measure {
for _ in 0 ..< iterationCount {
_ = receiver.value
}
}
}
func testKVORACInterceptSetterThenGet() {
let receiver = Receiver5()
receiver.reactive.producer(forKeyPath: #keyPath(Receiver5.value)).start()
_ = receiver.reactive.trigger(for: #selector(getter: receiver.value))
measure {
for _ in 0 ..< iterationCount {
_ = receiver.value
}
}
}
func testJustKVOInterceptSetterThenSet() {
let receiver = Receiver6()
receiver.reactive.producer(forKeyPath: #keyPath(Receiver6.value)).start()
measure {
for i in 0 ..< iterationCount {
receiver.value = i
}
}
}
func testJustKVOInterceptSetterThenGet() {
let receiver = Receiver6()
receiver.reactive.producer(forKeyPath: #keyPath(Receiver6.value)).start()
measure {
for _ in 0 ..< iterationCount {
_ = receiver.value
}
}
}
}