diff --git a/Service/Tests/PerfTests/EDOServicePerfTest.m b/Service/Tests/PerfTests/EDOServicePerfTest.m index 4d5b0137..5a27457f 100644 --- a/Service/Tests/PerfTests/EDOServicePerfTest.m +++ b/Service/Tests/PerfTests/EDOServicePerfTest.m @@ -18,6 +18,7 @@ #include #include +#include #import "Channel/Sources/EDOHostPort.h" #import "Service/Sources/EDOClientService.h" @@ -57,6 +58,12 @@ // The number of times to execute the measured blocks. static const size_t kNumOfBenchmarkExecutions = 100; +static int CompareUint64(const void *lhs, const void *rhs) { + const uint64_t left = *(const uint64_t *)lhs; + const uint64_t right = *(const uint64_t *)rhs; + return (left > right) - (left < right); +} + @interface EDOUITestAppPerfTests : XCTestCase @property(readonly) EDOTestDummy *remoteDummy; @property(readonly) Class remoteClass; @@ -193,24 +200,42 @@ - (void)testClassMethodLotsTimes { } - (void)testIteratingReturnByValueResultLotsTimes { + __block BOOL byValueValid = YES; uint64_t byValueResult = [self assertPerformBlockWithWeight:1 executions:10 block:^(EDOTestDummy *remoteDummy) { NSArray *result = [[remoteDummy returnByValue] returnLargeArray]; + if (result.count != 1000) { + byValueValid = NO; + return; + } for (NSInteger i = 0; i < 1000; i++) { - XCTAssert(((NSNumber *)result[i]).integerValue == i); + if (((NSNumber *)result[i]).integerValue != i) { + byValueValid = NO; + } } }]; + XCTAssertTrue(byValueValid); + + __block BOOL byReferenceValid = YES; uint64_t byReferenceResult = [self assertPerformBlockWithWeight:1000 executions:10 block:^(EDOTestDummy *remoteDummy) { NSArray *result = [remoteDummy returnLargeArray]; + if (result.count != 1000) { + byReferenceValid = NO; + return; + } for (NSInteger i = 0; i < 1000; i++) { - XCTAssert(((NSNumber *)result[i]).integerValue == i); + if (((NSNumber *)result[i]).integerValue != i) { + byReferenceValid = NO; + } } }]; + XCTAssertTrue(byReferenceValid); + XCTAssertLessThan(byValueResult * 100, byReferenceResult); } @@ -244,17 +269,42 @@ - (uint64_t)assertPerformBlockWithWeight:(uint64_t)weight block:(void (^)(EDOTes } /** - * Assert the block is performed @excutions times within the @weight multiple of threshold. + * Assert the median execution time of @c block over @c executions runs is within the @c weight + * multiple of the threshold. */ - (uint64_t)assertPerformBlockWithWeight:(uint64_t)weight executions:(NSInteger)executions block:(void (^)(EDOTestDummy *))block { + XCTAssertGreaterThan(executions, 0); + if (executions <= 0) { + return 0; + } + EDOTestDummy *remoteDummy = self.remoteDummy; - uint64_t result = dispatch_benchmark(executions, ^{ + // Run one unmeasured warm-up iteration to prime method-signature caches, socket channels, and + // allocator state before taking timing samples. + @autoreleasepool { block(remoteDummy); - }); - XCTAssertLessThanOrEqual(result, kRemoteInvocationThresholdInNano * weight); - return result; + } + + NS_VALID_UNTIL_END_OF_SCOPE NSMutableData *samplesData = + [NSMutableData dataWithLength:(NSUInteger)executions * sizeof(uint64_t)]; + uint64_t *samples = (uint64_t *)samplesData.mutableBytes; + for (NSInteger i = 0; i < executions; i++) { + samples[i] = dispatch_benchmark(1, ^{ + @autoreleasepool { + block(remoteDummy); + } + }); + } + + qsort(samples, (size_t)executions, sizeof(uint64_t), CompareUint64); + NSInteger mid = executions / 2; + uint64_t medianResult = (executions % 2 == 1) + ? samples[mid] + : samples[mid - 1] + (samples[mid] - samples[mid - 1]) / 2; + XCTAssertLessThanOrEqual(medianResult, kRemoteInvocationThresholdInNano * weight); + return medianResult; } @end