Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2021 Developer Tools

WWDC21 · 24 min · Developer Tools

Ultimate application performance survival guide

Performance optimization can seem like a daunting task — with many metrics to track and tools to use. Fear not: Our survival guide to app performance is here to help you understand tooling, metrics, and paradigms that can help smooth your development process and contribute to a great experience for people using your app.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 5 snippets

Using MetricKit swift · at 5:46 ↗
class AppMetrics: MXMetricManagerSubscriber {
	init() {
		let shared = MXMetricManager.shared
		shared.add(self)
	}

	deinit {
		let shared = MXMetricManager.shared
		shared.remove(self)
	}

	// Receive daily metrics
	func didReceive(_ payloads: [MXMetricPayload]) {
		// Process metrics
	}

	// Receive diagnostics
	func didReceive(_ payloads: [MXDiagnosticPayload]) {
		// Process metrics
	}
}
Testing Scroll performance swift · at 10:29 ↗
func testScrollingAnimationPerformance() throws {
        
    app.launch()
    app.staticTexts["Meal Planner"].tap()
    let foodCollection = app.collectionViews.firstMatch

    let measureOptions = XCTMeasureOptions()
    measureOptions.invocationOptions = [.manuallyStop]
        
    measure(metrics: [XCTOSSignpostMetric.scrollDecelerationMetric],
    options: measureOptions) {
        foodCollection.swipeUp(velocity: .fast)
        stopMeasuring()
        foodCollection.swipeDown(velocity: .fast)
    }
}
Using mxSignpostAnimationIntervalBegin swift · at 11:53 ↗
func startAnimating() {
	// Mark the beginning of animations
	mxSignpostAnimationIntervalBegin(
		log: MXMetricManager.makeLogHandle(category: "animation_telemetry"), 
		name: "custom_animation”)
	}

	func animationDidComplete() {
	// Mark the end of the animation to receive the collected hitch rate telemetry
	mxSignpost(OSSignpostType.end, 
		log: MXMetricManager.makeLogHandle(category: "animation_telemetry"), 
		name: "custom_animation")
}
Using XCTest to Measure Disk Usage swift · at 13:51 ↗
// Example performance XCTest

func testSaveMeal() {
	let app = XCUIApplication()
	let options = XCTMeasureOptions()
	options.invocationOptions = [.manuallyStart]

	measure(metrics: [XCTStorageMetric(application: app)], options: options) {
		app.launch()
		startMeasuring()

		let firstCell = app.cells.firstMatch
		firstCell.buttons["Save meal"].firstMatch.tap()

		let savedButton = firstCell.buttons["Saved"].firstMatch
		XCTAssertTrue(savedButton.waitForExistence(timeout: 2))
	}
}
Collect memory telemetry swift · at 21:19 ↗
// Collect memory telemetry

func saveAppAssets() {
	mxSignpost(OSSignpostType.begin, 
		log: MXMetricManager.makeLogHandle(category: "memory_telemetry"), 
		name: "custom_memory")

	// save app metadata

	mxSignpost(OSSignpostType.end, 
		log: MXMetricManager.makeLogHandle(category: "memory_telemetry"), 
		name: "custom_memory")
}

Resources