Performance

๐Ÿ”ฌ CPU Profiling

Profile your Armature application to identify performance bottlenecks and generate interactive flamegraphs.

๐Ÿ”ฅ

Flamegraphs

Interactive CPU visualization

๐Ÿ“Š

Sampling Profiler

1000 Hz CPU sampling

๐ŸŽฏ

Hotspot Detection

Find slow functions

๐Ÿ“ˆ

Performance Analysis

Optimize critical paths

Overview

CPU profiling helps you understand where your application spends time. Armature includes built-in profiling support using pprof that generates interactive flamegraphs.

A flamegraph is a visualization where:

  • Each box represents a function in the call stack
  • The width of a box shows how much CPU time was spent in that function
  • Boxes are stacked to show the call hierarchy
  • Wider boxes = more time = potential optimization targets

Quick Start

Run the built-in profiling server example:

# Run the profiling server in release mode
cargo run --example profiling_server --release

# In another terminal, generate load
for i in {{1..1000}}; do
  curl -s http://localhost:PORT/tasks > /dev/null
done

# Press Ctrl+C to stop and generate flamegraph
# Open flamegraph-profile.svg in your browser

Adding Profiling to Your App

Add the profiling dependencies to your Cargo.toml:

[dev-dependencies]
pprof = { version = "0.14", features = ["flamegraph", "criterion", "prost-codec"] }
ctrlc = "3.4"

# Enable debug symbols in release for better stack traces
[profile.profiling]
inherits = "release"
debug = true

Reading Flamegraphs

Understanding the flamegraph output:

  • X-axis: Stack frames sorted alphabetically (not time order)
  • Y-axis: Stack depth (callers below, callees above)
  • Width: Percentage of total CPU time
  • Color: Random (for visual distinction)

Look for:

  • Wide plateaus โ€” Functions using lots of CPU
  • Deep stacks โ€” Complex call chains
  • Your code โ€” Search for your module names

Optimization Tips

Hotspot Optimization
serde_json Use simd-json or sonic-rs for faster JSON
String::clone Use &str or Arc<str> to avoid cloning
Vec::push Pre-allocate with Vec::with_capacity
HashMap Use hashbrown or indexmap

Best Practices

  • Profile in release mode โ€” Debug builds are not representative
  • Enable debug symbols โ€” Use [profile.profiling] for readable stack traces
  • Generate realistic load โ€” Use production-like request patterns
  • Profile for adequate duration โ€” At least 30 seconds for stable results
  • Compare before/after โ€” Save flamegraphs to track improvements