Module arc_sets

Module arc_sets 

Source
Expand description

§Type

  • ArcSet
    • ArcSet implements collections that can contain Rustics instances and other ArcSet instances.

    • Members of an ArcSet are kept as Arc<Mutex<...>> instances to support multithreaded applications.

§Example

    use std::rc::Rc;
    use std::cell::RefCell;
    use std::time::Instant;
    use rustics::arc_item_mut;
    use rustics::time::Timer;
    use rustics::time::DurationTimer;
    use rustics::arc_sets::ArcSet;
    use rustics::timer;
    use rustics::timer_mut;

    // Create a set.  By way of example, assume that we're expecting
    // 8 Rustics instances but no subsets, and set those hints
    // appropriately.  By default, the print output goes to stdout, and
    // that's fine for an example, so just give "None" to accept the
    // default output settings.

    let set = ArcSet::new_box("Main Statistics", 8, 0, &None);
    let set = arc_item_mut!(set);

    // Add an instance to record query latencies.  It's a time
    // statistic, so we need a timer.  Here we use an adapter for the
    // standard rust Duration timer.

    let timer = DurationTimer::new_box();

    // The add_running_timer() method is a helper method for creating
    // RunningTime instances.  Clone the timer so that we have a copy
    // to use.

    let mut query_latency =
        set.add_running_time("Query Latency", timer.clone());

    // Assume for this example that the queries recorded to this
    // RunningTime instance are from a single thread, so we can
    // use the record_event() method to query the timer and restart
    // it.
    //
    // The clock started running when we created the DurationTimer.
    // Applications also can restart the timer using the start() method
    // if more precision is needed.

    timer_mut!(timer).start();  // Show how to restart a timer.

    arc_item_mut!(query_latency).record_event();

    // Do more work, then record another time sample.

    // do_work();

    // The record_event() code restarted the timer, so we can just
    // invoke that routine again.

    arc_item_mut!(query_latency).record_event();

    // For the multithreaded case, you can use DurationTimer more
    // manually.  A timer in a box is required.

    let mut local_timer = DurationTimer::new_box();

    // Do our query.

    // do_work();

    // Now record the time spent.  The record_interval() method will
    // read the clock for us.

    let lock = arc_item_mut!(query_latency);

    lock.record_interval(&mut local_timer);

    drop(lock);

    // If you want to use a timer that can't fully implement the Timer
    // trait, you'll need to implement a hz method for Timer with
    // dummy functions for the test of the trait.
    //
    // Let's use Duration timer directly as an example.  Make a new
    // Timer instance for this example.  This timer is used only to
    // pass the clock hertz to the RunningTimer code.

    let timer = DurationTimer::new_box();

    // Create a new RunningTime instance.

    let mut query_latency =
        set.add_running_time("Custom Timer", timer.clone());

    // Start the Duration timer.

    let start = Instant::now();

    // Do our query.

    // do_query();

    // Now get the elapsed time as integer ticks.  DurationTimer
    // works in nanoseconds, so use the as_nanos() method.

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

    arc_item_mut!(query_latency).record_time(time_spent as i64);

    // Print our statistics.  This example has very little
    // recorded in it.

    let query_lock = arc_item_mut!(query_latency);

    query_lock.print();

    // Check the statistics.

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

Structs§

ArcSet
ArcSet is the implementation type for a set of Rustics instances that are wrapped as Arc<Mutex<dyn Rustics>>.
ArcSetConfig
This struct is passed to some constructors that create ArcSet instances.

Traits§

ArcTraverser
The ArcTraverser trait is used by the traverse() method to call a user-defined function for each member of an ArcSet and its subsets.

Type Aliases§

ArcSetBox
RusticsArc