Module running_time

Module running_time 

Source
Expand description

§Type

  • RunningTime
    • RunningTime accumulates statistics on a stream of time intervals.

    • Internally, it uses RunningInteger instances to collect statistics. See that code for more information

    • The main.rs program contains a simple example of how to use this type.

§Example

    use std::rc::Rc;
    use std::cell::RefCell;
    use std::time::Instant;
    use rustics::Rustics;
    use rustics::timer;
    use rustics::time::Timer;
    use rustics::time::DurationTimer;
    use rustics::running_time::RunningTime;

    // Create an instance to record query latencies.  This is for a
    // time statistic, so we need a timer.  Use an adapter for the
    // Rust standard Duration timer.

    let timer = DurationTimer::new_box();

    // Accept the default print options. See the RunningInteger
    // comments for an example of how to set print options.

    let mut query_latency =
        RunningTime::new("Query Latency", timer, &None);

    // By way of example, we assume that the queries are single-
    // threaded, so we can use the record_time() method to query the
    // timer and restart it.
    //
    // So record one time sample for the single-threaded case.  The
    // timer started running when we created the Duration timer.

    query_latency.record_event();

    // For the multithreaded case, you can use DurationTimer manually.

    let mut local_timer = DurationTimer::new();

    // Do our query.
    // ...

    // You can use the finish() method if this RunningTime instance is
    // shared.

    query_latency.record_time(local_timer.finish() as i64);

    // record_interval() works, as well.  Create (and start) a timer,
    // then record the interval.

    let mut timer_box = DurationTimer::new_box();

    // do_work();

    query_latency.record_interval(&mut timer_box);

    // If you want to use your own timer, you'll need to implement the
    // Timer trait to initialize the RunningTime instance, but you can
    // use it directly to get data. Let's use Duration timer directly
    // as an example.  Make a new instance for this example.

    let timer = DurationTimer::new_box();

    let mut query_latency =
        RunningTime::new("Custom Timer", timer.clone(), &None);

    // Start the Duration timer.

    let start = Instant::now();

    // Do our query.

    // Now get the elapsed time.  DurationTimer works in nanoseconds,
    // so use as_nanos() to get the tick count.

    let time_spent = start.elapsed().as_nanos();
    assert!(timer!(timer).hz() == 1_000_000_000);

    query_latency.record_time(time_spent as i64);

    // Print our statistics.  This example has only one event recorded.

    query_latency.print();

    assert!(query_latency.count() == 1);
    assert!(query_latency.mean() == time_spent as f64);
    assert!(query_latency.standard_deviation() == 0.0);

Structs§

RunningTime
A RunningTime instance accumulates statistics on a stream of integer data samples representing time intervals. Underneath, it uses a RunningInteger instance, but most output is printed as time periods.