Last active
January 19, 2016 18:15
-
-
Save Adlai-Holler/f811809e57d06a6e7a2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LockingTests.swift | |
// LockingTests | |
// | |
// Created by Adlai Holler on 1/18/16. | |
// Copyright © 2016 Adlai Holler. All rights reserved. | |
// | |
/** | |
Results in iOS 9.2 simulator (iPhone 6) on my iMac: | |
'-[LockingTests.LockingTests testDispatchSemaphore]' measured [Time, seconds] average: 1.012, relative standard deviation: 6.997%, values: [1.090330, 1.071983, 1.048037, 0.911319, 1.042073, 0.920707, 1.079228, 0.962281, 0.919077, 1.076169], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 | |
'-[LockingTests.LockingTests testNSLock]' measured [Time, seconds] average: 4.868, relative standard deviation: 1.557%, values: [4.782288, 4.882415, 4.841834, 4.776851, 4.752024, 4.913981, 4.918518, 4.918231, 4.888296, 5.008807], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 | |
'-[LockingTests.LockingTests testPthreadMutex]' measured [Time, seconds] average: 4.740, relative standard deviation: 1.924%, values: [4.760173, 4.994403, 4.763439, 4.717273, 4.699853, 4.665326, 4.730564, 4.719830, 4.655418, 4.693427], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 | |
'-[LockingTests.LockingTests testSpinLock]' measured [Time, seconds] average: 0.462, relative standard deviation: 1.087%, values: [0.469458, 0.462443, 0.459723, 0.451885, 0.462273, 0.468570, 0.465032, 0.461615, 0.463144, 0.456066], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 | |
*/ | |
import XCTest | |
final class LockingTests: XCTestCase { | |
func testSpinLock() { | |
var spinLock = OS_SPINLOCK_INIT | |
executeLockTest({ | |
OSSpinLockLock(&spinLock) | |
}, unlock: { | |
OSSpinLockUnlock(&spinLock) | |
}) | |
} | |
func testDispatchSemaphore() { | |
let sem = dispatch_semaphore_create(1) | |
executeLockTest({ | |
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER) | |
}, unlock: { | |
dispatch_semaphore_signal(sem) | |
}) | |
} | |
func testNSLock() { | |
let lock = NSLock() | |
executeLockTest({ | |
lock.lock() | |
}, unlock: { | |
lock.unlock() | |
}) | |
} | |
func testPthreadMutex() { | |
var mutex = pthread_mutex_t() | |
pthread_mutex_init(&mutex, nil) | |
executeLockTest({ | |
pthread_mutex_lock(&mutex) | |
}, unlock: { | |
pthread_mutex_unlock(&mutex) | |
}) | |
pthread_mutex_destroy(&mutex); | |
} | |
func disabled_testNoLock() { | |
executeLockTest({}, unlock: {}) | |
} | |
private func executeLockTest(lock: () -> Void, unlock: () -> Void) { | |
let dispatchBlockCount = 16 | |
let iterationCountPerBlock = 100_000 | |
// This is an example of a performance test case. | |
let q = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0) | |
var value = 100 | |
measureBlock { | |
let group = dispatch_group_create() | |
for _ in 0..<dispatchBlockCount { | |
dispatch_group_enter(group) | |
dispatch_async(q) { | |
for _ in 0..<iterationCountPerBlock { | |
lock() | |
value = value + 1 | |
value = value - 1 | |
unlock() | |
} | |
dispatch_group_leave(group) | |
} | |
} | |
dispatch_group_wait(group, DISPATCH_TIME_FOREVER) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment