Quantum Pulse
A lightweight, customizable profiling library for Rust applications with support for custom categories and percentile statistics.
Features
- 🚀 True Zero-Cost Abstraction - Stub implementation compiles to nothing when disabled
- 📊 Percentile Statistics - Automatic calculation of p50, p95, p99, and p99.9 percentiles using HDR histograms
- 🏷️ Type-Safe Categories - Define your own operation categories with compile-time guarantees
- 📈 Multiple Output Formats - Console and CSV export options
- ⏸️ Pausable Timers - Exclude specific periods from measurements
- 🔧 Clean API - Same interface whether profiling is enabled or disabled
- 🌐 Async Support - Full support for async/await patterns
- 🎯 No Conditionals Required - Use the same code for both production and development
Installation
Add this to your Cargo.toml
:
[]
# For production builds (zero overhead)
= { = "0.1.0", = false }
# For development builds (with profiling)
= { = "0.1.0", = ["full"] }
Or use feature flags in your application:
[]
= { = "0.1.0", = false }
[]
= ["quantum-pulse/full"]
Quick Start
Basic Usage
use ;
// Define your operations enum
// Implement the Operation trait
Type-Safe Operations with Categories
Define specific operations with automatic categorization:
use ;
type WebProfiler = ;
async
Async Support
use ;
async
async
Manual Recording
For precise control over timing measurements:
use ;
use Instant;
Type-Safe Operation Tracking
Define your operations as Debug-derived enums for compile-time safety:
use ;
// Use with type-safe enum operations
Advanced Features
Report Configuration
Customize report generation with various options:
use ;
let report = new
.include_percentiles
.group_by_category
.time_format
.build;
println!;
Export Formats
use Profiler;
type AppProfiler = ;
// Console output (default)
let report = report;
println!;
// Simple CSV-like export
let stats = get_all_stats;
let mut csv_content = String from;
for in stats
write.unwrap;
Operation Categories
Type-Safe Operations with Categories
Define operation categories with metadata for better organization:
use ;
// Define critical operations
// Custom category for critical path
;
// Use type-safe operations for consistent profiling
let result = profile!;
Zero-Cost Abstractions
Quantum Pulse implements true zero-cost abstractions through its innovative stub feature system:
How It Works
// Your code always looks the same
let result = profile!;
// With default features (stub mode):
// - profile! macro expands to just the code block
// - No timing, no allocations, no overhead
// - Compiler optimizes it to: let result = expensive_operation();
// With "full" feature enabled:
// - Full profiling with timing and statistics
// - HDR histograms for accurate percentiles
// - Comprehensive reporting
Performance Characteristics
Configuration | Overhead | Use Case |
---|---|---|
Stub (default) | Zero - methods are empty and inlined away | Production |
Full | ~200-300ns per operation | Development, debugging |
Implementation Details
The library provides two implementations:
- Stub: Empty trait implementations that compile to nothing
- Full: Complete profiling implementation with HDR histograms
Both expose the exact same API, ensuring your code never needs conditional compilation.
Performance Considerations
The library is designed with performance in mind:
- True Zero-Cost: Stub implementations are completely removed by the compiler
- Efficient Percentiles: Using HDR histograms for O(1) percentile calculations
- Lock-Free Operations: Using atomic operations and thread-local storage
- Smart Inlining: Critical paths marked with
#[inline(always)]
in stub mode - No Runtime Checks: Feature selection happens at compile time
Feature Flags
full
: Enable full profiling functionality with HDR histograms- Default (no features): Stub implementation with zero overhead
When no features are enabled, all profiling operations compile to no-ops that are completely eliminated by the optimizer.
Examples
Check out the examples/
directory for more comprehensive examples:
basic.rs
- Simple enum-based profiling examplecustom_categories.rs
- Using custom operation categoriesasync_profiling.rs
- Profiling async code with enumstrading_system.rs
- High-frequency trading system example
Run examples with:
Benchmarks
Run benchmarks with:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Migration from Conditional Compilation
If you're currently using #[cfg(feature = "...")]
for profiling, Quantum Pulse eliminates that need:
Before (with conditionals and strings)
let timer = start;
let result = do_work;
timer.stop;
After (with Quantum Pulse and type-safe enums)
let result = profile!;
The same clean code works in both production (zero-cost) and development (full profiling).
Acknowledgments
This library was designed for high-performance applications requiring microsecond-precision profiling, where traditional sampling profilers lack the necessary granularity and CPU performance counters provide excessive detail.