rustics 1.0.3

simple statistic library for performance analysis
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//
//  Copyright 2024 Jonathan L Bertoni
//
//  This code is available under the Berkeley 2-Clause, Berkeley 3-clause,
//  and MIT licenses.
//

//! ## 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);
//!```

use std::any::Any;

use super::Rustics;
use super::Units;
use super::Histogram;
use super::Printer;
use super::ExportStats;
use super::PrinterBox;
use super::PrinterOption;
use super::PrintOption;
use super::LogHistogramBox;
use super::FloatHistogramBox;
use super::parse_print_opts;
use super::TimerBox;
use super::printer_mut;
use super::timer_mut;
use super::timer_box_hz;
use super::running_integer::RunningInteger;
use super::merge::Export;

/// 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.
///
/// See the module comments for a sample program.

#[derive(Clone)]
pub struct RunningTime {
    running_integer:    Box<RunningInteger>,
    timer:              TimerBox,
    hz:                 i64,

    printer:            PrinterBox,
}

impl RunningTime {
    /// Creates a new RunningTime instance.

    pub fn new(name: &str, timer: TimerBox, print_opts: &PrintOption) -> RunningTime {
        let hz = timer_box_hz(&timer);

        let (printer, _title, _units, _histo_opts) = parse_print_opts(print_opts, name);

        if hz > i64::MAX as u128 {
            panic!("Rustics::RunningTime:  The timer hz value is too large.");
        }

        let hz              = hz as i64;
        let running_integer = Box::new(RunningInteger::new(name, print_opts));

        RunningTime { printer, running_integer, timer, hz }
    }

    /// Creates a RunningTime instance from a RunningInteger.  This function
    /// is used internally to support the Hier code.

    pub fn from_integer(timer: TimerBox, print_opts: &PrintOption, mut running: RunningInteger)
            -> RunningTime {
        let (printer, title, _units, _histo_opts) = parse_print_opts(print_opts, &running.name());

        running.set_title(&title);
        running.set_units(Units::empty());

        let hz              = timer_box_hz(&timer) as i64;
        let running_integer = Box::new(running);

        RunningTime { running_integer, timer, hz, printer }
    }

    /// Exports the statistics for this instance.

    pub fn export(&self) -> Export {
        self.running_integer.export_data()
    }
}

impl Rustics for RunningTime {
    fn record_i64(&mut self, _sample: i64) {
        panic!("Rustics::RunningTime:  i64 events are not permitted.");
    }

    fn record_f64(&mut self, _sample: f64) {
        panic!("Rustics::RunningTime:  f64 events are not permitted.");
    }

    fn record_event(&mut self) {
        let _ = self.record_event_report();
    }

    fn record_event_report(&mut self) -> i64 {
        let timer    = timer_mut!(*self.timer);
        let interval = timer.finish();  // read and restart the timer

        self.running_integer.record_i64(interval);
        interval
    }

    fn record_time(&mut self, sample: i64) {
        assert!(sample >= 0);
        self.running_integer.record_i64(sample);
    }

    fn record_interval(&mut self, timer: &mut TimerBox) {
        let timer    = timer_mut!(*timer);
        let interval = timer.finish();

        self.running_integer.record_i64(interval);
    }

    fn name(&self) -> String {
        self.running_integer.name()
    }

    fn title(&self) -> String {
        self.running_integer.title()
    }

    fn class(&self) -> &str {
        "time"
    }

    fn count(&self) ->u64 {
        self.running_integer.count()
    }

    fn log_mode(&self) -> isize {
        self.running_integer.log_mode()
    }

    fn mean(&self) ->f64 {
        self.running_integer.mean()
    }

    fn standard_deviation(&self) ->f64 {
        self.running_integer.standard_deviation()
    }

    fn variance(&self) ->f64 {
        self.running_integer.variance()
    }

    fn skewness(&self) ->f64 {
        self.running_integer.skewness()
    }

    fn kurtosis(&self) ->f64 {
        self.running_integer.kurtosis()
    }

    fn int_extremes(&self) -> bool {
        self.running_integer.int_extremes()
    }

    fn float_extremes(&self) -> bool {
        self.running_integer.float_extremes()
    }

    fn min_i64(&self) -> i64 {
        self.running_integer.min_i64()
    }

    fn min_f64(&self) -> f64 {
        self.running_integer.min_f64()
    }

    fn max_i64(&self) -> i64 {
        self.running_integer.max_i64()
    }

    fn max_f64(&self) -> f64 {
        self.running_integer.max_f64()
    }

    fn precompute(&mut self) {
        self.running_integer.precompute()
    }

    fn clear(&mut self) {
        self.running_integer.clear()
    }

    // Functions for printing

    fn print(&self) {
        self.print_opts(None, None);
    }

    fn print_opts(&self, printer: PrinterOption, title: Option<&str>) {
        let printer_box =
            if let Some(printer) = printer {
                printer
            } else {
                self.printer.clone()
            };

        let title =
            if let Some(title) = title {
                title
            } else {
                &self.running_integer.title()
            };

        let printable = self.running_integer.get_printable();
        let printer   = printer_mut!(printer_box);

        printer.print(title);
        printable.print_common_integer_times(self.hz, printer);
        printable.print_common_float_times(self.hz, printer);
        self.running_integer.print_histogram(printer);
        printer.print("");
    }

    fn set_title(&mut self, title: &str) {
        self.running_integer.set_title(title);
    }

    fn log_histogram(&self) -> Option<LogHistogramBox> {
        self.running_integer.log_histogram()
    }

    fn float_histogram(&self) -> Option<FloatHistogramBox> {
        self.running_integer.float_histogram()
    }

    // For internal use only.

    fn set_id(&mut self, id: usize) {
        self.running_integer.set_id(id)
    }

    fn id(&self) -> usize {
        self.running_integer.id()
    }

    fn equals(&self, other: &dyn Rustics) -> bool {
        if let Some(other) = <dyn Any>::downcast_ref::<RunningTime>(other.generic()) {
            std::ptr::eq(self, other)
        } else {
            false
        }
    }

    fn generic(&self) -> &dyn Any {
        self as &dyn Any
    }

    fn export_stats(&self) -> ExportStats {
        self.running_integer.export_stats()
    }
}

impl Histogram for RunningTime {
    fn print_histogram(&self, printer: &mut dyn Printer) {
        self.running_integer.print_histogram(printer)
    }

    fn clear_histogram(&mut self) {
        self.running_integer.clear_histogram();
    }

    fn to_log_histogram(&self) -> Option<LogHistogramBox> {
        self.running_integer.log_histogram()
    }

    fn to_float_histogram(&self) -> Option<FloatHistogramBox> {
        self.running_integer.float_histogram()
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::PrintOpts;
    use crate::stdout_printer;
    use crate::timer_box;
    use crate::tests::continuing_box;
    use crate::tests::compute_sum;
    use crate::tests::check_printer_box;
    use crate::hier::HierMember;
    use crate::counter::Counter;
    use crate::time::Timer;
    use std::rc::Rc;
    use std::cell::RefCell;

    fn simple_test() {
        println!("RunningTime::simple_test:  starting");

        let     timer        = continuing_box();
        let mut stat         = RunningTime::new("Query Latency", timer, &None);
        let     printer      = stdout_printer();
        let     printer      = printer_mut!(printer);
        let     sample_count = 200;

        for i in 1..=sample_count {
            stat.record_time(i);
        }

        assert!( stat.log_mode      () == 8);
        assert!( stat.int_extremes  ()     );
        assert!(!stat.float_extremes()     );

        assert!(stat.max_i64() == sample_count);
        assert!(stat.min_i64() == 1           );

        // precompute() should be a harmless nop

        stat.precompute();

        assert!( stat.log_mode      () == 8);
        assert!( stat.int_extremes  ()     );
        assert!(!stat.float_extremes()     );

        let histogram = stat.to_histogram();

        histogram.print_histogram(printer);

        {
            let histogram = histogram.to_log_histogram().unwrap();
            let histogram = histogram.borrow();
            let sum       = compute_sum(&histogram);

            assert!(sum == sample_count);
        }

        let hz = continuing_box().borrow().hz();

        assert!(stat.hz == hz as i64);

        let any      = stat.as_any();
        let any_stat = any.downcast_ref::<RunningTime>().unwrap();

        assert!(stat.equals(any_stat));

        // Now set_id() and id() to check equality.

        let expected = 12034; // Something unlikely.

        stat.set_id(expected);

        let any      = stat.as_any_mut();
        let any_stat = any.downcast_ref::<RunningTime>().unwrap();

        assert!(any_stat.id() == expected);
    }

    #[test]
    #[should_panic]
    fn test_record_i64() {
        let     timer = continuing_box();
        let mut stat  = RunningTime::new("Panic Test", timer, &None);
        let     _     = stat.record_i64(1);
    }

    #[test]
    #[should_panic]
    fn test_record_f64() {
        let     timer = continuing_box();
        let mut stat  = RunningTime::new("Panic Test", timer, &None);

        let _ = stat.record_f64(1.0);
    }

    #[test]
    #[should_panic]
    fn test_min_f64() {
        let timer = continuing_box();
        let stat  = RunningTime::new("Panic Test", timer, &None);
        let _     = stat.min_f64();
    }

    #[test]
    #[should_panic]
    fn test_max_f64() {
        let timer = continuing_box();
        let stat  = RunningTime::new("Panic Test", timer, &None);
        let _     = stat.max_f64();
    }

    #[test]
    #[should_panic]
    fn test_float_histogram() {
        let timer = continuing_box();
        let stat  = RunningTime::new("Panic Test", timer, &None);
        let _     = stat.float_histogram().unwrap();
    }

    #[test]
    #[should_panic]
    fn test_to_float() {
        let timer = continuing_box();
        let stat  = RunningTime::new("Panic Test", timer, &None);
        let _     = stat.to_float_histogram().unwrap();
    }

    fn test_histogram() {
        let     timer   = continuing_box();
        let mut stat    = RunningTime::new("Panic Test", timer, &None);
        let     samples = 200;

        for i in 1..=samples {
            stat.record_time(i as i64);
        }

        {
            let histogram = stat.to_log_histogram().unwrap();
            let histogram = histogram.borrow();

            let sum = compute_sum(&histogram);

            assert!(sum == samples)
        }

        stat.clear_histogram();

        {
            let histogram = stat.to_log_histogram().unwrap();
            let histogram = histogram.borrow();

            let sum = compute_sum(&histogram);

            assert!(sum == 0)
        }
    }

    fn test_equality() {
        let timer  = continuing_box();
        let stat_1 = RunningTime::new("Equality Test 1", timer, &None);

        let timer  = continuing_box();
        let stat_2 = RunningTime::new("Equality Test 2", timer, &None);

        let stat_3 = Counter::new("Equality Test 2", &None);

        assert!( stat_1.equals(&stat_1));
        assert!(!stat_1.equals(&stat_2));
        assert!(!stat_1.equals(&stat_3));

        let generic = stat_1.generic();
        let recast  = generic.downcast_ref::<RunningTime>().unwrap();

        assert!(stat_1.equals(recast));
    }

    pub struct LargeTimer {
    }

    impl Timer for LargeTimer {
        fn start(&mut self) {
        }

        fn finish(&mut self) -> i64 {
            1
        }

        fn hz(&self) -> u128 {
            u128::MAX
        }
    }

    fn test_large_timer() {
        let mut timer = LargeTimer { };

        timer.start();

        let _ = timer.finish();
    }

    #[test]
    #[should_panic]
    fn test_large_clock() {
        let timer = LargeTimer { };
        let timer = timer_box!(timer);
        let _     = RunningTime::new("Panic Test", timer, &None);
    }

    fn test_print_output() {
        let expected =
            [
                "Test Statistics",
                "    Count               1,000 ",
                "    Minimum             1.000 microsecond",
                "    Maximum             1.000 millisecond",
                "    Log Mode               20 ",
                "    Mode Value        786.432 microseconds",
                "    Mean              500.500 microseconds",
                "    Std Dev           288.819 microseconds",
                "    Variance         +8.34166 e+10 ",
                "    Skewness         -4.16336 e-11 ",
                "    Kurtosis         -1.19999 e+0  ",
                "  Log Histogram",
                "  -----------------------",
                "    0:                 0                 0                 0                 0",
                "    4:                 0                 0                 0                 0",
                "    8:                 0                 0                 1                 1",
                "   12:                 2                 4                 8                16",
                "   16:                33                66               131               262",
                "   20:               476                 0                 0                 0",
                ""
            ];

        let     timer      = continuing_box();
        let     printer    = Some(check_printer_box(&expected, true, false));
        let     title      = None;
        let     units      = None;
        let     histo_opts = None;
        let     print_opts = Some(PrintOpts { printer, title, units, histo_opts });

        let     name       = "Test Statistics";
        let mut stats      = RunningTime::new(&name, timer, &print_opts);
        let     samples    = 1000;

        for _i in 1..=samples {
            stats.record_event();
        }

        stats.print();
    }

    #[test]
    fn run_tests() {
        simple_test      ();
        test_equality    ();
        test_histogram   ();
        test_large_timer ();
        test_print_output();
    }
}