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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! A native rust implementation of a histogram and percentiles which can offer
//! specified precision across the full range of stored values. This library is
//! inspired by the HdrHistogram project.
//!
//!
//! # Goals
//! * maintain precision across full value range
//! * provide percentile metrics for stored data
//! * pre-allocated datastructure
//!
//! # Future work
//! * unknown
//!
//! # Usage
//!
//! Create a new histogram, call increment for every value, retrieve percentile
//! stats.
//!
//! # Example
//!
//! Create a histogram, increment values, show percentiles
//!
//! ```
//!
//! use histogram::*;
//!
//! let mut histogram = Histogram::new(
//!     HistogramConfig {
//!         precision: 4,       // maintain > 4 sigfigs (max error .01%)
//!         max_value: 1000000, // max storable value. fewer, less ram needed
//!         max_memory: 0,      // optional memory bound in Bytes. 0 = unlimited
//!     }
//! ).unwrap();
//!
//! let mut value = 0;
//!
//! for i in 1..100 {
//!     histogram.increment(i);
//! }
//!
//! // print percentiles from the histogram
//! println!("Percentiles: p50: {} ns p90: {} ns p99: {} ns p999: {}",
//!     histogram.percentile(50.0).unwrap(),
//!     histogram.percentile(90.0).unwrap(),
//!     histogram.percentile(99.0).unwrap(),
//!     histogram.percentile(99.9).unwrap(),
//! );

#![crate_type = "lib"]

#![crate_name = "histogram"]

use std::fmt;

#[derive(Clone, Copy, Default)]
pub struct HistogramConfig {
    pub precision: u32,
    pub max_memory: u32,
    pub max_value: u64,
}

#[derive(Clone, Copy, Default)]
pub struct HistogramCounters {
    entries_total: u64,
    missed_unknown: u64,
    missed_small: u64,
    missed_large: u64,
}

#[derive(Clone)]
pub struct HistogramData {
    data: Vec<u64>,
    counters: HistogramCounters,
    iterator: usize,
}

#[derive(Clone, Copy)]
pub struct HistogramProperties {
    buckets_inner: u32,
    buckets_outer: u32,
    buckets_total: u32,
    memory_used: u32,
    linear_max: u64,
    linear_power: u32,
}

#[derive(Clone)]
pub struct Histogram {
    config: HistogramConfig,
    data: HistogramData,
    properties: HistogramProperties,
}

#[derive(Clone, Copy)]
pub struct HistogramBucket {
    pub value: u64,
    pub count: u64,
    pub id: u64,
}

impl Iterator for Histogram {
    type Item = HistogramBucket;

    fn next(&mut self) -> Option<HistogramBucket> {
        let current = self.data.iterator;
        self.data.iterator += 1;

        if current == (self.properties.buckets_total as usize) {
            self.data.iterator = 0;
            None
        } else {
            Some(HistogramBucket {
                id: current as u64,
                value: self.index_value(current),
                count: self.data.data[current],
            })
        }
    }
}

impl fmt::Debug for Histogram {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "({} total)", self.data.counters.entries_total)
    }
}

impl Histogram {

    /// create a new Histogram
    ///
    /// # Example
    /// ```
    /// # use histogram::{Histogram,HistogramConfig};
    ///
    /// let mut h = Histogram::new(
    ///     HistogramConfig{
    ///         max_value: 1000000,
    ///         precision: 3,
    ///         max_memory: 0,
    /// }).unwrap();
    pub fn new(config: HistogramConfig) -> Option<Histogram> {

        let radix = 10_u32;

        let buckets_inner: u32 = radix.pow(config.precision);

        let linear_power: u32 = 32 - buckets_inner.leading_zeros();

        let linear_max: u64 = 2.0_f64.powi(linear_power as i32) as u64 - 1;

        let max_value_power: u32 = 64 - config.max_value.leading_zeros();

        let mut buckets_outer = 0;

        if max_value_power > linear_power {
            buckets_outer = max_value_power - linear_power;
        }

        let buckets_total = buckets_inner * buckets_outer + linear_max as u32;

        let memory_used = buckets_total * 8;

        if config.max_memory > 0 && config.max_memory < memory_used {
            return None;
        }

        let mut data = Vec::with_capacity(buckets_total as usize);

        // vector is already sized to fit, just set the length accordingly
        unsafe {
            data.set_len(buckets_total as usize);
        }

        let counters: HistogramCounters = Default::default();

        Some(Histogram {
            config: config,
            data: HistogramData {
                data: data,
                counters: counters,
                iterator: 0,
            },
            properties: HistogramProperties {
                buckets_inner: buckets_inner,
                buckets_outer: buckets_outer,
                buckets_total: buckets_total,
                memory_used: memory_used,
                linear_max: linear_max,
                linear_power: linear_power,
            },
        })
    }

    /// clear the histogram data
    ///
    /// # Example
    /// ```
    /// # use histogram::{Histogram,HistogramConfig};
    ///
    /// let mut h = Histogram::new(
    ///     HistogramConfig{
    ///         max_memory: 0,
    ///         max_value: 1000000,
    ///         precision: 3,
    /// }).unwrap();
    ///
    /// h.increment(1);
    /// assert_eq!(h.entries(), 1);
    /// h.clear();
    /// assert_eq!(h.entries(), 0);
    pub fn clear(&mut self) {
        self.data.counters = Default::default();
        self.data.data = Vec::with_capacity(self.properties.buckets_total as usize);

        // vector is already sized to fit, just set the length accordingly
        unsafe {
            self.data.data.set_len(self.properties.buckets_total as usize);
        }
    }

    /// increment the count for a value
    ///
    /// # Example
    /// ```
    /// # use histogram::{Histogram,HistogramConfig};
    ///
    /// let mut h = Histogram::new(
    ///     HistogramConfig{
    ///         max_memory: 0,
    ///         max_value: 1000000,
    ///         precision: 3,
    /// }).unwrap();
    ///
    /// h.increment(1);
    /// assert_eq!(h.get(1).unwrap(), 1);
    pub fn increment(&mut self, value: u64) {
        self.data.counters.entries_total = self.data.counters.entries_total.saturating_add(1_u64);
        if value < 1 {
            self.data.counters.missed_small =
                self.data.counters.missed_small.saturating_add(1_u64);
        } else if value > self.config.max_value {
            self.data.counters.missed_large =
                self.data.counters.missed_large.saturating_add(1_u64);
        } else {
            match self.get_index(value) {
                Some(index) => {
                    self.data.data[index] = self.data.data[index].saturating_add(1_u64);
                },
                None => {
                    self.data.counters.missed_unknown =
                        self.data.counters.missed_unknown.saturating_add(1_u64);
                }
            }
        }
    }

    /// record additional counts for value
    ///
    /// # Example
    /// ```
    /// # use histogram::{Histogram,HistogramConfig};
    ///
    /// let mut h = Histogram::new(
    ///     HistogramConfig{
    ///         max_memory: 0,
    ///         max_value: 1000000,
    ///         precision: 3,
    /// }).unwrap();
    ///
    /// h.record(1, 1);
    /// assert_eq!(h.get(1).unwrap(), 1);
    ///
    /// h.record(2, 2);
    /// assert_eq!(h.get(2).unwrap(), 2);
    ///
    /// h.record(10, 10);
    /// assert_eq!(h.get(10).unwrap(), 10);
    pub fn record(&mut self, value: u64, count: u64) {
        self.data.counters.entries_total = self.data.counters.entries_total.saturating_add(count);
        if value < 1 {
            self.data.counters.missed_small =
                self.data.counters.missed_small.saturating_add(count);
        } else if value > self.config.max_value {
            self.data.counters.missed_large =
                self.data.counters.missed_large.saturating_add(count);
        } else {
            match self.get_index(value) {
                Some(index) => {
                    self.data.data[index] = self.data.data[index].saturating_add(count);
                },
                None => {
                    self.data.counters.missed_unknown =
                        self.data.counters.missed_unknown.saturating_add(count);
                }
            }
        }
    }

    /// get the count for a value
    ///
    /// # Example
    /// ```
    /// # use histogram::{Histogram,HistogramConfig};
    ///
    /// let mut h = Histogram::new(
    ///     HistogramConfig{
    ///         max_memory: 0,
    ///         max_value: 1000000,
    ///         precision: 3,
    /// }).unwrap();
    ///
    /// assert_eq!(h.get(1).unwrap(), 0);
    pub fn get(&mut self, value: u64) -> Option<u64> {
        match self.get_index(value) {
            Some(index) => {
                return Some(self.data.data[index]);
            },
            None => {
                None
            }
        }
    }

    // calculate the index for a given value
    fn get_index(&mut self, value: u64) -> Option<usize> {
        let result: Option<usize> = None;

        if value >= 1 {

            if value <= self.properties.linear_max {
                return Some((value - 1) as usize);
            }

            let l_max = self.properties.linear_max as u32;

            let outer = 63 - value.leading_zeros();

            let l_power = 64 - self.properties.linear_max.leading_zeros();

            let remain = value as f64 - 2.0_f64.powi(outer as i32);

            let inner = (self.properties.buckets_inner as f64 * remain as f64 /
                         2.0_f64.powi((outer) as i32)).floor() as u32;

            // this gives the shifted outer index
            let outer = outer as u32 - l_power;

            let index = l_max + self.properties.buckets_inner * outer + inner;

            return Some(index as usize);
        }
        result
    }

    // calculate the nominal value of the given index
    fn index_value(&mut self, index: usize) -> u64 {

        // in this case, the index is linear
        let index = index as u32;

        let linear_max = self.properties.linear_max as u32;

        if index < linear_max {
            return (index + 1) as u64;
        }

        let log_index = index - linear_max;

        let outer = (log_index as f64 / self.properties.buckets_inner as f64).floor() as u32;

        let inner = log_index - outer * self.properties.buckets_inner as u32;

        let mut value = 2.0_f64.powi((outer as u32 + self.properties.linear_power) as i32);
        value += inner as f64 * (value as f64 / self.properties.buckets_inner as f64);

        value.ceil() as u64
    }

    /// return the value for the given percentile
    ///
    /// # Example
    /// ```
    /// # use histogram::{Histogram,HistogramConfig};
    /// let mut h = Histogram::new(
    ///     HistogramConfig{
    ///         max_memory: 0,
    ///         max_value: 1000000,
    ///         precision: 3,
    /// }).unwrap();
    ///
    /// for value in 1..1000 {
    ///     h.increment(value);
    /// }
    ///
    /// assert_eq!(h.percentile(50.0).unwrap(), 501);
    /// assert_eq!(h.percentile(90.0).unwrap(), 901);
    /// assert_eq!(h.percentile(99.0).unwrap(), 991);
    /// assert_eq!(h.percentile(99.9).unwrap(), 999);
    pub fn percentile(&mut self, percentile: f64) -> Option<u64> {

        if self.entries() < 1 {
            return None;
        }

        if percentile <= 100.0 && percentile >= 0.0 {

            let total = self.data.counters.entries_total;

            let mut need = (total as f64 * (percentile / 100.0_f64)).ceil() as u64;

            if need > total {
                need = total;
            }

            need = total - need;

            let mut index: isize = (self.properties.buckets_total - 1) as isize;
            let mut step: isize = -1 as isize;
            let mut have: u64 = 0 as u64;

            if percentile < 50.0 {
                index = 0 as isize;
                step = 1 as isize;
                need = total - need;
            }

            if need == 0 {
                need = 1;
            }

            loop {
                have = have + self.data.data[index as usize];

                if have >= need {
                    return Some(self.index_value(index as usize) as u64);
                }

                index += step;

                if index > self.properties.buckets_total as isize {
                    break;
                }
                if index < 0 {
                    break;
                }
            }
        }
        None
    }

    /// merge one Histogram into another Histogram
    ///
    /// # Example
    /// ```
    /// # use histogram::{Histogram,HistogramConfig};
    ///
    /// let mut a = Histogram::new(
    ///     HistogramConfig{
    ///         max_memory: 0,
    ///         max_value: 1000000,
    ///         precision: 3,
    /// }).unwrap();
    ///
    /// let mut b = Histogram::new(
    ///     HistogramConfig{
    ///         max_memory: 0,
    ///         max_value: 1000000,
    ///         precision: 3,
    /// }).unwrap();
    ///
    /// assert_eq!(a.entries(), 0);
    /// assert_eq!(b.entries(), 0);
    ///
    /// a.increment(1);
    /// b.increment(2);
    ///
    /// assert_eq!(a.entries(), 1);
    /// assert_eq!(b.entries(), 1);
    ///
    /// a.merge(&mut b);
    ///
    /// assert_eq!(a.entries(), 2);
    /// assert_eq!(a.get(1).unwrap(), 1);
    /// assert_eq!(a.get(2).unwrap(), 1);
    pub fn merge(&mut self, other: &mut Histogram) {
        loop {
            match other.next() {
                Some(bucket) => {
                    self.record(bucket.value, bucket.count);
                }
                None => { break }
            }
        }
    }

    /// return the number of entries in the Histogram
    ///
    /// # Example
    /// ```
    /// # use histogram::{Histogram,HistogramConfig};
    ///
    /// let mut h = Histogram::new(
    ///     HistogramConfig{
    ///         max_memory: 0,
    ///         max_value: 1000000,
    ///         precision: 3,
    /// }).unwrap();
    ///
    /// assert_eq!(h.entries(), 0);
    /// h.increment(1);
    /// assert_eq!(h.entries(), 1);
    pub fn entries(&mut self) -> u64 {
        self.data.counters.entries_total
    }
}

#[cfg(test)]
mod tests {
    use super::{Histogram, HistogramConfig};

    #[test]
    fn test_new_0() {
        // this histogram has only a linear region which runs 1-15

        let h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 10,
            precision: 1,
        }).unwrap();

        assert_eq!(h.properties.buckets_inner, 10); // 10 ^ precision
        assert_eq!(h.properties.buckets_outer, 0); // max <= 2 * buckets_inner
        assert_eq!(h.properties.buckets_total, 15); // only linear region
    }

    #[test]
    fn test_new_1() {
        // this histogram has linear and log regios

        let h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 31,
            precision: 1,
        }).unwrap();

        assert_eq!(h.properties.buckets_inner, 10); // 10 ^ precision
        assert_eq!(h.properties.buckets_outer, 1); // max <= 2 * buckets_inner
        assert_eq!(h.properties.buckets_total, 25); // only linear region
    }

    #[test]
    fn test_new_2() {
        let h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 32,
            precision: 1,
        }).unwrap();

        assert_eq!(h.properties.buckets_inner, 10); // 10 ^ precision
        assert_eq!(h.properties.buckets_outer, 2); // max <= 2 * buckets_inner
        assert_eq!(h.properties.buckets_total, 35); // only linear region
    }

    #[test]
    fn test_new_3() {
        let h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 10000,
            precision: 3,
        }).unwrap();

        assert_eq!(h.properties.buckets_inner, 1000); // 10 ^ precision
        assert_eq!(h.properties.buckets_outer, 4); // max <= 2 * buckets_inner
        assert_eq!(h.properties.buckets_total, 5023); // only linear region
    }

    #[test]
    fn test_increment_0() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 10,
            precision: 3,
        }).unwrap();

        for op in 1..1000000 {
            h.increment(1);
            assert_eq!(h.entries(), op);
        }
    }

    #[test]
    fn test_increment_1() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 10,
            precision: 3,
        }).unwrap();

        // increment values across the entire range
        // including 0 and > max_value
        for v in 0..11 {
            h.increment(v);
            assert_eq!(h.entries(), (v + 1));
        }
    }

    #[test]
    fn test_get() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 10,
            precision: 1,
        }).unwrap();

        h.increment(1);
        assert_eq!(h.get(1), Some(1));

        h.increment(1);
        assert_eq!(h.get(1), Some(2));

        h.increment(2);
        assert_eq!(h.get(2), Some(1));

        assert_eq!(h.get(3), Some(0));
    }

    #[test]
    fn test_get_index_0() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 32,
            precision: 3,
        }).unwrap();

        // all values should index directly to (value - 1)
        // no estimated buckets are needed given the precision and max

        assert_eq!(h.get_index(1), Some(0));
        assert_eq!(h.index_value(0), 1);

        assert_eq!(h.get_index(2), Some(1));
        assert_eq!(h.index_value(1), 2);

        assert_eq!(h.get_index(3), Some(2));
        assert_eq!(h.index_value(2), 3);

        assert_eq!(h.get_index(4), Some(3));
        assert_eq!(h.index_value(3), 4);

        assert_eq!(h.get_index(5), Some(4));
        assert_eq!(h.index_value(4), 5);

        assert_eq!(h.get_index(16), Some(15));
        assert_eq!(h.index_value(15), 16);

        assert_eq!(h.get_index(32), Some(31));
        assert_eq!(h.index_value(31), 32);
    }

    #[test]
    fn test_get_index_1() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 100,
            precision: 1,
        }).unwrap();

        assert_eq!(h.get_index(1), Some(0));
        assert_eq!(h.get_index(2), Some(1));
        assert_eq!(h.get_index(15), Some(14));

        // powers of two are 10 apart from value = 16 and up
        assert_eq!(h.get_index(16), Some(15));
        assert_eq!(h.get_index(32), Some(25));
        assert_eq!(h.get_index(64), Some(35));

        // this tests that rounding up within inner bucket works
        assert_eq!(h.get_index(16), Some(15));
        assert_eq!(h.get_index(17), Some(15));
        assert_eq!(h.get_index(18), Some(16));
        assert_eq!(h.get_index(19), Some(16));

        // these values prove roll-up into next outer bucket works
        assert_eq!(h.get_index(62), Some(34));
        assert_eq!(h.get_index(63), Some(34));
        assert_eq!(h.get_index(64), Some(35));

        assert_eq!(h.get_index(65), Some(35));
    }

    #[test]
    fn test_get_index_2() {
        // extensive test from precomputed table
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 100,
            precision: 1,
        }).unwrap();

        let v = vec![
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 23, 24, 26, 28, 29,
            31, 32, 36, 39, 42, 45, 48, 52, 55, 58, 61, 64, 71, 77, 84, 90, 96, 103, 109, 116, 122
        ];

        for index in 0..45 {
            let got = h.get_index(v[index]).unwrap();
            assert!(got == index, "Value: {} Got: {} Want: {}", v[index], got, index);
        }

        for index in 0..45 {
            let got = h.index_value(index);
            assert!(got == v[index], "Index: {} Got: {} Want: {}", index, got, v[index]);
        }
    }

    #[test]
    fn test_get_index_3() {
        // extensive test from precomputed table
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 250,
            precision: 1,
        }).unwrap();

        let v = vec![
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 23, 24, 26, 28, 29,
            31, 32, 36, 39, 42, 45, 48, 52, 55, 58, 61, 64, 71, 77, 84, 90, 96, 103, 109, 116, 122,
            128, 141, 154, 167, 180, 192, 205, 218, 231, 244
        ];

        for index in 0..55 {
            let got = h.get_index(v[index]).unwrap();
            assert!(got == index, "Value: {} Got: {} Want: {}", v[index], got, index);
        }

        for index in 0..55 {
            let got = h.index_value(index);
            assert!(got == v[index], "Index: {} Got: {} Want: {}", index, got, v[index]);
        }
    }

    #[test]
    fn test_index_value_0() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 100,
            precision: 1,
        }).unwrap();

        assert_eq!(h.index_value(0), 1);
        assert_eq!(h.index_value(1), 2);
        assert_eq!(h.index_value(14), 15);

        assert_eq!(h.index_value(15), 16);
        assert_eq!(h.index_value(25), 32);
        assert_eq!(h.index_value(35), 64);
    }

    #[test]
    fn test_index_value_1() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 1000,
            precision: 2,
        }).unwrap();

        assert_eq!(h.index_value(0), 1);
        assert_eq!(h.index_value(1), 2);
        assert_eq!(h.index_value(126), 127);

        assert_eq!(h.index_value(127), 128);
        assert_eq!(h.index_value(227), 256);
        assert_eq!(h.index_value(327), 512);
    }

    #[test]
    fn test_index_value_2() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 10000,
            precision: 3,
        }).unwrap();

        assert_eq!(h.index_value(0), 1);
        assert_eq!(h.index_value(1), 2);
        assert_eq!(h.index_value(1022), 1023);

        assert_eq!(h.index_value(1023), 1024);
        assert_eq!(h.index_value(2023), 2048);
        assert_eq!(h.index_value(3023), 4096);
        assert_eq!(h.index_value(4023), 8192);
    }

    #[test]
    fn test_iterator() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 100,
            precision: 1,
        }).unwrap();

        loop {
            match h.next() {
                Some(bucket) => {
                    match h.get_index(bucket.value) {
                        Some(_) => {
                            h.increment(bucket.value);
                        },
                        None => {},
                    }
                },
                None => { break }
            }
        }
    }

    #[test]
    fn test_percentile() {
        let mut h = Histogram::new(HistogramConfig {
            max_memory: 0,
            max_value: 1000,
            precision: 4,
        }).unwrap();

        for i in 100..200 {
            h.increment(i);
        }

        assert_eq!(h.percentile(0.0).unwrap(), 100);
        assert_eq!(h.percentile(10.0).unwrap(), 109);
        assert_eq!(h.percentile(25.0).unwrap(), 124);
        assert_eq!(h.percentile(50.0).unwrap(), 150);
        assert_eq!(h.percentile(75.0).unwrap(), 175);
        assert_eq!(h.percentile(90.0).unwrap(), 190);
        assert_eq!(h.percentile(95.0).unwrap(), 195);
        assert_eq!(h.percentile(100.0).unwrap(), 199);
    }
}