Skip to content

Instantly share code, notes, and snippets.

@shoumikhin
Created February 6, 2018 10:40
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 shoumikhin/50117254c9615e3d4b8efaf68bb9d3b3 to your computer and use it in GitHub Desktop.
Save shoumikhin/50117254c9615e3d4b8efaf68bb9d3b3 to your computer and use it in GitHub Desktop.
- (void)testContinueWithBlockOnSerialQueue {
// Arrange.
XCTestExpectation *expectation = [self expectationWithDescription:@""];
expectation.expectedFulfillmentCount = FBLPromisePerformanceTestIterationCount;
dispatch_queue_t queue = dispatch_queue_create(
__FUNCTION__,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0));
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
BFExecutor *executor = [BFExecutor executorWithDispatchQueue:queue];
// Act.
dispatch_async(dispatch_get_main_queue(), ^{
uint64_t time = dispatch_benchmark(FBLPromisePerformanceTestIterationCount, ^{
[[BFTask taskWithResult:@YES] continueWithExecutor:executor withBlock:^id(BFTask *task) {
dispatch_semaphore_signal(semaphore);
[expectation fulfill];
return task.result;
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
FBLLogAverageTime(time);
});
// Assert.
[self waitForExpectationsWithTimeout:10 handler:nil];
}
- (void)testDoubleContinueWithBlockOnSerialQueue {
// Arrange.
XCTestExpectation *expectation = [self expectationWithDescription:@""];
expectation.expectedFulfillmentCount = FBLPromisePerformanceTestIterationCount;
dispatch_queue_t queue = dispatch_queue_create(
__FUNCTION__,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0));
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
BFExecutor *executor = [BFExecutor executorWithDispatchQueue:queue];
// Act.
dispatch_async(dispatch_get_main_queue(), ^{
uint64_t time = dispatch_benchmark(FBLPromisePerformanceTestIterationCount, ^{
[[[BFTask taskWithResult:@YES] continueWithExecutor:executor
withBlock:^id(BFTask *task) {
return task.result;
}]
continueWithExecutor:executor
withBlock:^id(BFTask *task) {
dispatch_semaphore_signal(semaphore);
[expectation fulfill];
return task.result;
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
FBLLogAverageTime(time);
});
// Assert.
[self waitForExpectationsWithTimeout:10 handler:nil];
}
- (void)testTripleContinueWithBlockOnSerialQueue {
// Arrange.
XCTestExpectation *expectation = [self expectationWithDescription:@""];
expectation.expectedFulfillmentCount = FBLPromisePerformanceTestIterationCount;
dispatch_queue_t queue = dispatch_queue_create(
__FUNCTION__,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0));
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
BFExecutor *executor = [BFExecutor executorWithDispatchQueue:queue];
// Act.
dispatch_async(dispatch_get_main_queue(), ^{
uint64_t time = dispatch_benchmark(FBLPromisePerformanceTestIterationCount, ^{
[[[[BFTask taskWithResult:@YES] continueWithExecutor:executor
withBlock:^id(BFTask *task) {
return task.result;
}] continueWithExecutor:executor
withBlock:^id(BFTask *task) {
return task.result;
}]
continueWithExecutor:executor
withBlock:^id(BFTask *task) {
dispatch_semaphore_signal(semaphore);
[expectation fulfill];
return task.result;
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
FBLLogAverageTime(time);
});
// Assert.
[self waitForExpectationsWithTimeout:10 handler:nil];
}
- (void)testContinueWithBlockOnConcurrentQueue {
// Arrange.
dispatch_queue_t queue = dispatch_queue_create(
__FUNCTION__, dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT,
QOS_CLASS_USER_INITIATED, 0));
dispatch_group_t group = dispatch_group_create();
NSMutableArray<BFTaskCompletionSource *> *sources =
[NSMutableArray arrayWithCapacity:FBLPromisePerformanceTestIterationCount];
BFExecutor *executor = [BFExecutor executorWithDispatchQueue:queue];
for (NSUInteger i = 0; i < FBLPromisePerformanceTestIterationCount; ++i) {
dispatch_group_enter(group);
BFTaskCompletionSource *source = [BFTaskCompletionSource taskCompletionSource];
[source.task continueWithExecutor:executor
withBlock:^id(BFTask *task) {
dispatch_group_leave(group);
return task.result;
}];
[sources addObject:source];
}
NSDate *startDate = [NSDate date];
// Act.
[sources makeObjectsPerformSelector:@selector(setResult:) withObject:@YES];
// Assert.
XCTAssert(dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC)) == 0,
@"Asynchronous wait failed: Exceeded timeout of 1 second.");
NSDate *endDate = [NSDate date];
FBLLogTotalTime([endDate timeIntervalSinceDate:startDate]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment