Hello! In our last lesson, we configured your Xcode project to create a controlled environment for profiling. Now that your "laboratory" is set up, it's time to introduce a powerful tool for running specific "experiments": the os_signpost API.
While instruments like Time Profiler give you a broad overview of your app's performance, os_signpost allows you to inject custom markers into your code. This lets you measure the precise duration and context of logical operations that are meaningful to your application—such as a network fetch, a data processing routine, or a complex UI update. For your app, this could mean timing a WeatherKit request or a location update. By the end of this lesson, you will be able to use os_signpost to label and measure custom tasks, making it significantly easier to pinpoint performance bottlenecks.
The Anatomy of a Signpost
At its core, os_signpost is an API within Apple's Unified Logging system that lets you emit custom telemetry visible within Instruments. Think of it as leaving a trail of breadcrumbs through your code that Instruments can then visualize on a timeline.
There are two primary ways you'll use signposts:
- Intervals: These mark the beginning and end of a specific task. They are perfect for measuring the duration of operations like file I/O, network requests, or computation.
- Events: These mark a single point in time. They are useful for flagging instantaneous occurrences, like a user tapping a button or a cache being invalidated.
To keep your signposts organized, you define a subsystem (typically your app's bundle identifier) and a category (a descriptive name for a group of related signposts, like "Networking" or "DataProcessing").
A crucial concept, especially for modern concurrent applications, is the Signpost ID. If your app performs multiple instances of the same task concurrently—for example, downloading several images at once—you need a unique ID for each one. This allows Instruments to correctly pair the begin and end markers for each individual task, preventing a confusing jumble of overlapping intervals.
The Two Faces of the API: os_signpost and OSSignposter
There are two primary APIs for creating signposts. As an experienced developer, it's useful to recognize both.
The Traditional API: os_signpost()
The original API is a C-style function, os_signpost(), which is powerful but can feel a bit verbose in a modern Swift codebase. It requires you to create an OSLog handle and then call the function with a specific type: .begin, .end, or .event.
The following article provides a clear, concise overview of this traditional API.
Measuring performance with os_signpost – Donny Wals
This article by Donny Wals breaks down the foundational os_signpost API. It's a great primer on the underlying concepts that the newer Swift API builds upon.
Please read the first half of the article, from the beginning down to the section titled "Reading signposts with instruments." Focus on the following points: How to create an OSLog handle with a subsystem and category. The different signpost types: .begin, .event, and .end. The use of format strings and privacy specifiers like %{public}s. The two ways to create a SignpostID to track concurrent operations.
The Modern Swift API: OSSignposter
With the introduction of Swift concurrency, Apple released OSSignposter, a Swift-native wrapper that simplifies signposting, particularly for async/await code. It's the recommended approach for modern projects. Instead of managing separate begin/end calls manually, you can use a more idiomatic pattern that fits naturally around asynchronous work.
Practical Implementation in a SwiftUI App
Let's see OSSignposter in action. The best way to understand it is to see it in a real-world context. The following article provides an excellent, self-contained SwiftUI example that simulates fetching data for rows in a list.
✈️ Beyond Firebase: Using MetricKit, os_signpost, and Instruments in a Modern SwiftUI App
This article by Wesley Matlock provides a practical demonstration of OSSignposter in a SwiftUI app with intentional performance problems, making it a perfect case study.
Please read the code examples in the sections "The Setup" and "os_signpost 101". Pay close attention to: The creation of the static let signposter. Using a static constant is a common and efficient pattern. The sequence inside the .onAppear task: makeSignpostID(), beginInterval(), and endInterval(). How the state object returned by beginInterval is passed to endInterval to link the interval.
As you can see from the example, the OSSignposter API integrates seamlessly with Swift's async/await syntax. Wrapping the await viewModel.fetchWeather(for: flight) call between beginInterval and endInterval is all that's needed to measure that specific asynchronous operation.
Visualizing Signposts in Instruments
Once you've instrumented your code, you can profile your app (using the "Profiling" scheme we created in the last lesson). The Time Profiler template includes the necessary os_signpost instrument by default.
When you run a trace, you will see a new track dedicated to your signposts.

The os_signpost track provides two key views:
- Timeline View: This gives you a visual representation of your intervals and events over time. You can immediately see how long each operation took and whether multiple operations were running in parallel (i.e., if intervals overlap).
- Detail Pane: When you select the
os_signposttrack, the bottom pane lists all the signpost events. For intervals, it provides precise statistics like duration, start time, and end time. Instruments will also automatically aggregate statistics for intervals with the same name, calculating the count, minimum, maximum, and average duration, which is incredibly useful for spotting outliers.

From Data to Insight: A Practical Workflow
Now, let's tie this all together into a repeatable workflow you can apply to your own app.
- Identify & Instrument: Isolate a logical operation you want to measure. For your app, this might be
func fetchWeatherData(). Wrap this operation withos_signpostintervals. - Profile: Run your app using Instruments (
Cmd+I) with your dedicated profiling scheme. - Analyze:
- Find the
os_signposttrack (it may be nested under your process name). - Examine the timeline. Are the intervals longer than you expect? Are they overlapping in unexpected ways? For example, seeing multiple "fetch location" intervals running concurrently when you only expected one might indicate a bug.
- Select the track and inspect the detail pane. Sort by duration to find the most expensive operations.
- Find the
- Optimize & Validate: After applying a potential fix, re-run the same profiling session. The signpost intervals provide definitive proof of your success. If the "WeatherFetch" interval duration drops from 500ms to 50ms, you know your optimization worked. This before-and-after comparison is a cornerstone of professional performance tuning.
The article from Wesley Matlock has a great section on this validation process.
✈️ Beyond Firebase: Using MetricKit, os_signpost, and Instruments in a Modern SwiftUI App
This section reinforces the importance of using signposts to validate your optimizations.
Read the short subsection "Before and After". The bullet points highlight exactly what to look for when comparing traces.
Conclusion
You've now moved beyond general system-level profiling and into the realm of targeted, application-specific measurement. The os_signpost API is a fundamental tool for any serious performance investigation, giving you the power to ask precise questions about your code's behavior.
Key Takeaways:
os_signpostlets you create custom intervals and events that are visualized directly in Instruments.- The modern
OSSignposterAPI is the recommended, Swift-friendly way to instrument your code, especially withasync/await. - Always use a
SignpostIDto correctly track concurrent or overlapping tasks. - The primary value of signposts lies in measuring the duration of specific logical operations and validating the impact of your optimizations through before-and-after analysis.
In our next lesson, we will build directly on this. Now that you can populate a trace with custom, meaningful markers, we'll explore how to efficiently navigate and filter the potentially vast amount of data in a trace file. You'll learn to use time range filtering and track-specific inspection to instantly zoom in on the exact events you've just learned how to create.