HdrHistogram / HdrHistogram_rust

A port of HdrHistogram to Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

iter_recorded() method fails to iterate if all the values are zero.

minghanc opened this issue · comments

iter_recorded() won't work when all the values recorded by the Histogram instance is 0.
Sample code to hit the issue:

let mut tracker = Histogram::<u64>::new(3).unwrap();
tracker += 0;
tracker += 0;
tracker += 0;

 // print 3 as expected.
 println!("{}", tracker.len());

 let mut res = String::new();
 for v in tracker.iter_recorded() {
     res.push_str(format!("value {} and count {}", v.value_iterated_to(), v.count_at_value()).as_str());
 }

 // nothing is printed. Expected: value 0 and count 3
 println!("{}", res);

We use the iterator to print out a certain format and emit to our log file. Is there a better way to do that for the logging purpose?

It's been a while since I was in the guts of the data structure, but IIRC the hdrhistogram structure squashes everything below the lowest trackable number into the 0th internal slot which is then ignored because it doesn't obey the same scaling rules as all the other slots. The lowest trackable number is at least 1, so 0 would fall into the void, in the same way that if you configured a histogram with the lowest trackable number of 10000 then 44 would also get lost.

If you do need to record zeroes for your use case, a slightly unpleasant workaround would be to simply add 1 to everything at record time, then subtract it off at iteration time.

Ok. But if I continue to record another value, for example 1, to the above tracker, then it will be able to print out the result correctly. (value 0 and count 3 value 1 and count 1), Could you clarify that? Since as you said, 1 is below the lowest trackable number. I just want to make sure there would be no other corner cases for the iterator.

Hm, that does seem unexpected. Will have to look into it.