测量代码块的性能

1.对于同步方法:

- (void)testPerformanceReverseString {
    NSString *originalString = @"hi_my_name_is_siddharth";
    [self measureBlock:^{
        [self.someObject reverseString:originalString];
    }];
}

2.对于异步方法:

- (void)testPerformanceOfAsynchronousBlock {
   [self measureMetrics:@[XCTPerformanceMetric_WallClockTime] automaticallyStartMeasuring:YES forBlock:^{
    
    XCTestExpectation *expectation = [self expectationWithDescription:@"performanceTestWithResponse"];
    
    [self.someObject doSomethingThatTakesSomeTimesWithCompletionBlock:^(NSString *result) {
        [expectation fulfill];
    }];
    [self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
    }];
}];
}
  • 这些性能测量块连续执行 10 次,然后计算平均值,并根据此平均性能结果创建并接受基线进行进一步评估。
  • 将性能结果与先前的测试结果和基线进行比较,并具有可定制的最大标准偏差。