latency 0.1.0

Ultra-low-latency timing library with nanosecond precision for performance-critical applications
Documentation
  • Coverage
  • 80.82%
    59 out of 73 items documented2 out of 54 items with examples
  • Size
  • Source code size: 48.06 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.44 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 1m 2s Average build duration of successful builds.
  • all releases: 40s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ax-x2

latency

low-latency timing library for performance-critical applications. provides nanosecond-precision measurements using x86_64 tsc (time-stamp counter) with minimal overhead.

features

  • cpu cycle counting via rdtsc/rdtscp on x86_64
  • lock-free histogram with logarithmic bucketing for latency distributions
  • multiple timer apis manual, raii guards, scoped timers
  • zero-cost when disabled via feature flag
  • fast statistics percentiles (p50, p90, p99, p999, p9999) with o(1) bucket access

quick start

use latency::{TimePoint, Histogram, Timer, ScopedTimer};
use std::sync::Arc;

fn main() {
    latency::init();

    // method 1: simple time point
    let start = TimePoint::now();
    do_work();
    println!("elapsed: {}ns", start.elapsed_ns());

    // method 2: manual timer
    let timer = Timer::start();
    do_work();
    println!("elapsed: {}μs", timer.elapsed_micros());

    // method 3: RAII guard
    let histogram = Arc::new(Histogram::new());
    {
        let _guard = latency::TimerGuard::new(Arc::clone(&histogram));
        do_work();
    }
    println!("{}", histogram.stats().format_micros());

    // method 4: scoped timer with closures
    let timer = ScopedTimer::new(Arc::clone(&histogram));
    let result = timer.time(|| {
        do_work()
    });
}

fn do_work() {}

api

timing primitives

  • TimePoint::now() - snapshot of current tsc value
  • TimePoint::elapsed_ns() / elapsed_cycles() - elapsed time since snapshot
  • time_block! macro - time a code block and get result + elapsed time
  • time_if_enabled! macro - conditional timing based on feature flag

histograms

  • Histogram::record(value_ns) - lock-free atomic recording
  • histogram.stats() - get latencystats with percentiles
  • histogram.percentiles(&[p1, p2, ...]) - arbitrary percentile queries

timers

  • Timer - manual start/stop with optional histogram recording
  • TimerGuard - raii guard that records on drop
  • ScopedTimer - closures with threshold-based logging

global registry (optional)

let hist = timer::global::histogram("my_op");
timer::global::report_all();  // print all histograms

configuration

add to Cargo.toml:

[dependencies]
latency = { path = "." }

[features]
default = ["enabled"]

disable timing overhead:

default = []  # timing disabled by default

performance

  • rdtsc() – ~20 cycles (inline)
  • histogram.record() – ~50 cycles atomic update (lock-free)
  • when disabled – zero runtime cost via feature flag

x86_64 only

requires x86_64 for tsc access. other platforms return 0 with feature checks.