MetricsKit: Swift Application Monitoring (iOS/macOS)
MetricsKit is a lightweight library for performance monitoring and metrics collection in Swift applications for iOS and macOS. It is designed for developers who need a simple and effective way to track operation execution time, function call counts, memory usage, and other key indicators without integrating heavy external services.
Why do you need MetricsKit?
During the development of mobile and desktop applications in Swift, it is important to understand how code behaves in real-world conditions. MetricsKit allows you to:
- Measure the execution time of critical code sections (e.g., data loading, UI rendering).
- Collect statistics on method call frequency.
- Track memory usage and warn about leaks.
- Log errors and exceptions with performance context.
- Export metrics for analysis (JSON, CSV).
This is especially useful when optimizing UI, working with network requests, or developing games using SpriteKit/SceneKit.
Installation
MetricsKit is available via Swift Package Manager (SPM). Add the dependency to your Package.swift:
dependencies: [ .package(url: "https://github.com/yourname/MetricsKit.git", from: "1.0.0")]Or in Xcode: File → Add Packages → paste the repository URL.
Main Features
- Timer — measure the execution time of code blocks.
- Counter — count the number of events.
- Gauge — track current values (e.g., cache size).
- MemoryMonitor — automatic collection of application memory data.
- Exporter — save metrics to a file or send them to a server.
Swift Code Example (iOS/macOS)
A simple example of using MetricsKit to measure image loading time:
import MetricsKit
class ImageLoader { let metrics = MetricsKit.shared func loadImage(from url: URL) async throws -> UIImage { let timer = metrics.startTimer(name: "image_load") defer { timer.stop() } let (data, _) = try await URLSession.shared.data(from: url) guard let image = UIImage(data: data) else { throw URLError(.badServerResponse) } metrics.incrementCounter(name: "images_loaded") metrics.recordGauge(name: "cache_size", value: Double(data.count)) return image }}After execution, you can export all metrics:
let report = metrics.export()print(report) // JSON string with all dataWhen to use MetricsKit?
The library is ideal for:
- Performance optimization before release.
- Creating internal monitoring tools for QA.
- Collecting anonymous statistics on feature usage.
- Teaching beginners — a visual representation of code behavior.
MetricsKit does not require a server-side component and works entirely locally, ensuring data privacy.
Try MetricsKit in your next Swift project and gain full control over your application's performance!
Also in library
Dependency Injection Container for Kotlin: Koin vs Kodein vs Dagger
Image Processing in C++: resize and crop with the OpenCV library
JWT Tokens in Go (Golang): Creation and Verification with Examples
Boost.Filesystem for C++: Complete Guide with Examples
Hibernate for Java: Complete Guide with Examples