dmaclach
dmaclach

Reputation: 3710

Why is XCTest with a predicate based expectation so slow?

Anybody know why XCTest expectations with predicates are so slow? It appears to me that the minimum execution time for both Swift and ObjC is 1 second even if the predicate should pass immediately. Wrapping my expectations with an if !pred.evaluate(with:obj) sped up test execution by several orders of magnitude.

import XCTest

struct TestObject {
  var value = true
}

class SwiftPredicateTestTests: XCTestCase {
  func testPerformanceExample() {
    let obj = TestObject()
    self.measure {
      let pred = NSPredicate{(evaluatedObject, _) in
        return (evaluatedObject as! TestObject).value
      }
      let expect = expectation(for: pred, evaluatedWith: obj, handler: nil)
      wait(for: [expect], timeout: 30)
    }
  }
}
measured [Time, seconds] average: 1.013, relative standard deviation: 2.464%, values: [1.087598, 1.002806, 1.003474, 1.001123, 1.004604, 1.007821, 1.004224, 1.005046, 1.005243, 1.007546],

Upvotes: 2

Views: 305

Answers (1)

Roman Zakharov
Roman Zakharov

Reputation: 2273

This effect is also noticeable in waitForExistence method. They probably use sleep(1) before condition checking in cycles.

Upvotes: 1

Related Questions