我写了一个方法loadCountries()
,它在调用时执行异步文件加载,然后通知它的输出工作完成。我试图在测试中测量这个方法的性能,但是我不知道如何在没有回调的情况下处理异步操作。
我发现有startMeasuring()
和stopMeasuring()
方法允许手动设置测试的入口和终点,我尝试在输出模拟中调用后者:
let output = InteractorOutputMock()
output.onDisplay = { _ in
self.stopMeasuring()
}
let interactor = PremiumLocationsChooserInteractor()
interactor.output = output
measureMetrics([.wallClockTime], automaticallyStartMeasuring: false) {
self.startMeasuring()
interactor.loadCountries()
}
但是代码仍然在0秒内完成。我应该如何处理这个问题?
更新:
我也尝试过使用expectation,但是遇到了问题。我既不能在measure块内部调用它,也不能在measure块外部调用它。前者看起来如下所示,并导致代码在measure
的第一次迭代中等待:
let outputCalledExpectation = XCTestExpectation(description: "Output hasn't been called")
outputCalledExpectation.expectedFulfillmentCount = 10 // need to fullfill it 10 times since this is how much measure block is called
let output = InteractorOutputMock()
output.onDisplay = { _ in
self.stopMeasuring() // called only once
outputCalledExpectation.fulfill()
}
let interactor = PremiumLocationsChooserInteractor()
interactor.output = output
measureMetrics([.wallClockTime], automaticallyStartMeasuring: false) {
startMeasuring()
interactor.loadCountries()
wait(for: [outputCalledExpectation], timeout: 5.0) // then stack here
}
如果我尝试将wait方法移出块,就会得到异常-stopMeasuring is only supported from a block passed to -measure...Block:
measureMetrics([.wallClockTime], automaticallyStartMeasuring: false) {
startMeasuring()
interactor.loadCountries()
}
wait(for: [outputCalledExpectation], timeout: 5.0) // get exception here
1条答案
按热度按时间yyyllmsg1#
我用这个扩展来测量,对我来说就像一个魅力。