Write an XCTestCase measure block with XCTClockMetric, XCTCPUMetric and XCTMemoryMetric, record a baseline on the Mac that runs your CI, commit it, and let xcodebuild test fail when a metric drifts past the allowed deviation. Use one consistent Mac for this: a developer laptop drifts with background apps, OS updates and thermal throttling, so wall-clock times change from run to run.
Figment has not measured timing variance inside its VMs, so we cannot give you a noise figure. For tight budgets, run the suite on the Dedicated M4 Pro, which is a whole Mac rather than a shared VM. A VM (vCPUs are shared, not reserved cores) is a reasonable place to catch coarse regressions, and you should check its spread on your own suite before you trust a tight threshold.
How does XCTest measure work?
XCTestCase.measure(metrics:block:) runs the closure several times and reports aggregate statistics. Each XCTMetric collects data independently. Iteration count and manual control are set through XCTMeasureOptions, whose iterationCount and invocationOptions properties are in Apple's framework reference. Check the current default for your installed Xcode rather than assuming a fixed number.
After the first passing run in Xcode you can click Set Baseline in the test results. Xcode stores the baseline in the project (under the scheme's test action settings, or in a .xctestplan, depending on project structure) and ties it to the device or Simulator destination the measurement ran against. Later runs compare each metric against the baseline and report a regression when the configured maximum deviation is exceeded. Confirm the threshold in your own scheme or test plan instead of assuming a default.
| Metric class | What it measures | Unit |
|---|---|---|
XCTClockMetric | Elapsed monotonic time during the block | seconds |
XCTCPUMetric | CPU time (user + kernel) across all cores | seconds |
XCTMemoryMetric | Physical memory footprint during each iteration | bytes |
A fourth option, XCTStorageMetric, tracks logical bytes written. It suits caching layers but is rarely needed for parsing tests.
What does a measure block look like?
The snippet below measures a JSON parsing function. Replace FeedItem with your own Decodable type and provide a sample_feed.json fixture that matches your production payload size. It is an untested example, not a benchmark result.
import XCTest
@testable import MyApp
final class FeedParserPerformanceTests: XCTestCase {
private var sampleData: Data!
override func setUpWithError() throws {
let bundle = Bundle(for: type(of: self))
guard let url = bundle.url(forResource: "sample_feed", withExtension: "json") else {
throw XCTSkip("Missing sample_feed.json. Add a representative fixture to the test bundle")
}
sampleData = try Data(contentsOf: url)
}
func testFeedParsingPerformance() throws {
let data = try XCTUnwrap(sampleData, "sampleData must be loaded in setUp")
measure(metrics: [
XCTClockMetric(),
XCTCPUMetric(),
XCTMemoryMetric()
]) {
do {
_ = try JSONDecoder().decode([FeedItem].self, from: data)
} catch {
XCTFail("Decoder failed: \(error)")
}
}
}
// Manual start/stop lets you exclude fixture work from the metric.
func testFeedParsingPerformanceTight() throws {
let data = try XCTUnwrap(sampleData)
let options = XCTMeasureOptions()
options.iterationCount = 10
options.invocationOptions = [.manuallyStart, .manuallyStop]
measure(metrics: [XCTClockMetric()], options: options) {
self.startMeasuring()
do {
_ = try JSONDecoder().decode([FeedItem].self, from: data)
} catch {
XCTFail("Decoder failed: \(error)")
}
self.stopMeasuring()
}
}
}
The block catches decoder errors and calls XCTFail, so a failed build or a throwing decoder is never counted as a fast run.
How do I record a baseline on the remote Mac?
Open the project in Xcode on the Mac, run the test, and click Set Baseline. Remote desktop gives you the Xcode UI for this step.
- Connect to the Mac with the encrypted remote desktop or the browser session.
- Open your project in Xcode, select the scheme, and run
testFeedParsingPerformancewith Product → Test. - In the test results, expand the performance entry and click Set Baseline.
- Xcode writes the baseline into the scheme's test action (or the
.xctestplanif you use one) underxcshareddata. - Commit the updated scheme or test-plan file so CI reads the same baseline:
git add MyApp.xcodeproj/xcshareddata/xcschemes/MyApp.xcscheme MyApp.xctestplan 2>/dev/null
git commit -m "chore: record XCTest performance baseline on remote Mac"
Baselines are tied to the destination they were recorded on. If you later move from one plan to another, re-record the baseline on the new machine instead of scaling numbers by hand.
How do I run the test and read the results?
Run xcodebuild test with -only-testing and a result bundle path, and check the exit status:
xcodebuild test \
-project MyApp.xcodeproj \
-scheme MyApp \
-destination 'platform=iOS Simulator,name=iPhone 16' \
-only-testing:MyAppTests/FeedParserPerformanceTests \
-resultBundlePath ./PerfResults.xcresult \
| tee xcodebuild.log
# Fail the CI job explicitly on xcodebuild's exit status.
test ${PIPESTATUS[0]} -eq 0
Pick a destination that exists on the host. Run xcrun simctl list devices available to see the Simulators your Xcode ships.
When a measurement exceeds the configured maximum relative deviation, Xcode marks the test as failed and the result bundle records the measured average, standard deviation and baseline for each metric. The log line looks like this (exact wording varies by Xcode version):
Test Case '-[MyAppTests.FeedParserPerformanceTests testFeedParsingPerformance]'
measured [Clock Monotonic Time, s] average: 0.142, relative standard deviation: 3.2%
Inspect the bundle with xcresulttool:
# Xcode 16+: the legacy JSON "get" invocation still works with --legacy.
xcrun xcresulttool get --format json --path ./PerfResults.xcresult --legacy > PerfResults.json
# Preferred newer flow: export individual payloads by id.
# xcrun xcresulttool export --type file --id <id> --path ./PerfResults.xcresult --output-path ./payload
The structured JSON is not a documented stable contract, so grep for your test name and iterate from there, or open the .xcresult in Xcode for a graphical diff against the stored baseline.
Should I combine performance tests with sanitizers?
No. Thread Sanitizer and Address Sanitizer add large overhead and distort clock metrics, so run them in a separate job. Keep performance tests in their own test plan and invoke xcodebuild twice:
# 1. Performance baseline check (no sanitizers).
xcodebuild test -scheme MyApp \
-destination 'platform=iOS Simulator,name=iPhone 16' \
-only-testing:MyAppTests/FeedParserPerformanceTests \
-resultBundlePath ./PerfResults.xcresult
# 2. Correctness check with Thread Sanitizer on the rest of the suite.
# Verify the current sanitizer flag with `xcodebuild -help` on your Xcode;
# a dedicated test plan that toggles sanitizers via Scheme → Test → Diagnostics
# is the most portable way to express this.
xcodebuild test -scheme MyApp \
-destination 'platform=iOS Simulator,name=iPhone 16' \
-enableThreadSanitizer YES \
-skip-testing:MyAppTests/FeedParserPerformanceTests \
-resultBundlePath ./TSanResults.xcresult
A TSan slowdown then cannot contaminate your performance baseline, and a data race in the parsing path still shows up in the second job.
Do performance baselines need a whole Mac?
| Need | Plan |
|---|---|
| Benchmarks and tight baselines | Dedicated M4 Pro 48 GB ($349 a month, or $299 on a 12-month term): a whole Mac, M4 Pro, 48GB RAM, 512GB SSD |
| Coarse regression checks on current Xcode | Apple Silicon Mac VM ($39 a month, $6 a day): 4 shared vCPUs, 4GB RAM, 65GB SSD |
| Coarse checks on Xcode 26.3 or earlier | Intel Mac VM ($25 a month, $2 a day): 4 shared vCPUs, 4GB RAM, 80GB SSD, macOS Sequoia |
The Intel VM cannot run Xcode 26.4 or later (those need macOS Tahoe) or arm64-only binaries. Check the macOS version shown before purchase against Apple's Xcode table.
Related reading: