I'd like my test to fail if it runs slower than 0.5 seconds but the average time is merely printed in the console and I cannot find a way to access it. Is there a way to access this data?
我希望我的测试失败,如果它运行速度低于0.5秒,但平均时间只是打印在控制台,我无法找到访问它的方法。有办法访问这些数据吗?
Code
代码
//Measures the time it takes to parse the participant codes from the first 100 events in our test data.
func testParticipantCodeParsingPerformance()
{
var increment = 0
self.measureBlock
{
increment = 0
while increment < 100
{
Parser.parseParticipantCode(self.fields[increment], hostCodes: MasterCalendarArray.getHostCodeArray()[increment])
increment++
}
}
print("Events measured: \(increment)")
}
Test Data
测试数据
[Tests.ParserTest testParticipantCodeParsingPerformance]' measured [Time, seconds] average: 0.203, relative standard deviation: 19.951%, values: [0.186405, 0.182292, 0.179966, 0.177797, 0.175820, 0.205763, 0.315636, 0.223014, 0.200362, 0.178165]
(测试。实验测量[时间,秒]平均值:0.203,相对标准偏差:19.951%,值:[0.186405,0.182292,0.179966,0.177797,0.175820,0.205763,0.315636,0.223014,0.178165]
2 个解决方案
#1
8
You need to set a baseline for your performance test. Head to the Report Navigator:
您需要为性能测试设置一个基线。报告导航:
and select your recent test run. You'll see a list of all your tests, but the performance ones will have times associated with them. Click the time to bring up the Performance Result popover:
选择最近的测试运行。您将看到所有测试的列表,但是性能测试将有与它们相关的时间。点击时间,弹出性能结果:
The "Baseline" value is what you're looking for--set it to 0.5s and that will inform Xcode that this test should complete in half a second. If your test is more than 10% slower than the baseline, it'll fail!
“基线”值是您正在寻找的——将其设置为0.5,并将通知Xcode,这个测试应该在半秒内完成。如果您的测试比基线慢10%以上,它将失败!
#2
2
The only way to do something similar to what you describe is setting a time limit graphically like @andyvn22 recommends.
要做与您描述的类似的事情,惟一的方法是按@andyvn22建议的图形化方式设置时间限制。
But, if you want to do it completely in code, the only thing you can do is extend XCTestCase with a new method that measure the execution time of the closure and returns it to be used in an assertiong, here is an example of what you could do:
但是,如果你想在代码中完全实现它,你唯一能做的就是用一个新方法扩展XCTestCase,这个新方法可以测量闭包的执行时间并返回给assertiong,这里有一个例子可以说明你能做什么:
extension XCTestCase{
/// Executes the block and return the execution time in millis
public func timeBlock(closure: ()->()) -> Int{
var info = mach_timebase_info(numer: 0, denom: 0)
mach_timebase_info(&info)
let begin = mach_absolute_time()
closure()
let diff = Double(mach_absolute_time() - begin) * Double(info.numer) / Double(1_000_000 * info.denom)
return Int(diff)
}
}
And use it with:
并使用它:
func testExample() {
XCTAssertTrue( 500 < self.timeBlock{
doSomethingLong()
})
}
#1
8
You need to set a baseline for your performance test. Head to the Report Navigator:
您需要为性能测试设置一个基线。报告导航:
and select your recent test run. You'll see a list of all your tests, but the performance ones will have times associated with them. Click the time to bring up the Performance Result popover:
选择最近的测试运行。您将看到所有测试的列表,但是性能测试将有与它们相关的时间。点击时间,弹出性能结果:
The "Baseline" value is what you're looking for--set it to 0.5s and that will inform Xcode that this test should complete in half a second. If your test is more than 10% slower than the baseline, it'll fail!
“基线”值是您正在寻找的——将其设置为0.5,并将通知Xcode,这个测试应该在半秒内完成。如果您的测试比基线慢10%以上,它将失败!
#2
2
The only way to do something similar to what you describe is setting a time limit graphically like @andyvn22 recommends.
要做与您描述的类似的事情,惟一的方法是按@andyvn22建议的图形化方式设置时间限制。
But, if you want to do it completely in code, the only thing you can do is extend XCTestCase with a new method that measure the execution time of the closure and returns it to be used in an assertiong, here is an example of what you could do:
但是,如果你想在代码中完全实现它,你唯一能做的就是用一个新方法扩展XCTestCase,这个新方法可以测量闭包的执行时间并返回给assertiong,这里有一个例子可以说明你能做什么:
extension XCTestCase{
/// Executes the block and return the execution time in millis
public func timeBlock(closure: ()->()) -> Int{
var info = mach_timebase_info(numer: 0, denom: 0)
mach_timebase_info(&info)
let begin = mach_absolute_time()
closure()
let diff = Double(mach_absolute_time() - begin) * Double(info.numer) / Double(1_000_000 * info.denom)
return Int(diff)
}
}
And use it with:
并使用它:
func testExample() {
XCTAssertTrue( 500 < self.timeBlock{
doSomethingLong()
})
}