Skip to content

Instantly share code, notes, and snippets.

@andersio
Last active May 30, 2017 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersio/61ed3c1fb460f52e8aa5a3b7eabd8824 to your computer and use it in GitHub Desktop.
Save andersio/61ed3c1fb460f52e8aa5a3b7eabd8824 to your computer and use it in GitHub Desktop.
import XCTest
import ReactiveSwift
import Result
private func _measure(times: UInt64 = 2_000_000, label: String = #function, _ action: (() -> UInt64) -> Void) {
var result: UInt64 = 0
var minResult: UInt64 = .max
for i in 0 ..< times {
var start: UInt64!
action {
start = mach_absolute_time()
return i
}
let end = mach_absolute_time()
let r = (end - start)
result += r
minResult = min(minResult, r)
}
var base = mach_timebase_info()
_ = withUnsafeMutablePointer(to: &base, mach_timebase_info)
let ns = (result / times) / UInt64(base.denom) * UInt64(base.numer)
let minNs = minResult / UInt64(base.denom) * UInt64(base.numer)
print("@\(label): avg \(ns) ns; min \(minNs) ns")
}
private func _measureAndStart(times: UInt64 = 2_000_000, label: String = #function, _ action: () -> Void) {
return _measure(times: times, label: label) { start in
_ = start()
action()
}
}
final class Token {}
class SignalTest: XCTestCase {
func testNormal() {
Thread.current.threadDictionary["reactiveswift_disable_inherit_serialization"] = true
defer {
Thread.current.threadDictionary["reactiveswift_disable_inherit_serialization"] = false
}
runTest()
}
func testNonatomic() {
runTest()
}
func runTest(label: String = #function) {
let property = MutableProperty((0, 0, 0, 0))
// Wrapped in a dedicated scope to guarantee the intermediate property would deinitialize.
let composed = { property.map { ($0.1, $0.3) }.combinePrevious((-1, -1)) }()
//let (signal, observer) = Signal<(Int, Int, Int, Int), NoError>.pipe()
//let composed = signal.map { ($0.1, $0.3) }.combinePrevious((-1, -1))
withExtendedLifetime(composed) {
_measureAndStart(times: 1_000_000, label: label) {
let value = 1
//observer.send(value: (value, value, value, value))
property.value = (value, value, value, value)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment