How to make a performance test fail if it's too slow? How to make a performance test fail if it's too slow? swift swift

How to make a performance test fail if it's too slow?


You need to set a baseline for your performance test. Head to the Report Navigator:

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:

Performance Result

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!


The only way to do something similar to what you describe is setting a time limit graphically like @andyvn22 recommends.

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:

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()    })}