HdrHistogram.NET icon indicating copy to clipboard operation
HdrHistogram.NET copied to clipboard

GetPerecentileOfValue

Open hannasm opened this issue 3 years ago • 1 comments

How hard would it be to add a method that queries the histogram for a percentile given a sample?

If i have a measurement of 300ms what percentile does this sample fall under?

hannasm avatar Jul 01 '22 21:07 hannasm

I haven't actually compiled this but here's my 15-minute guess:

public double GetPercentileForValue(long value) {
  var bucketIndex = GetBucketIndex(value);
  var subBucketIndex = GetSubBucketIndex(value, bucketIndex);
  long runningCount = 0;
  for (var i = 0; i < BucketCount; i++) {
    var j = (i==0) ? 0 : (SubBucketCount / 2);
    for (;j < SubBucketCount;j++) {
      runningCount += GetCountAt(i,j);
      if (i == bucketIndex && j == subBucketIndex) { return (runningCount / TotalCount) * 100.0; }
    }
  }

  return 100;
}

hannasm avatar Jul 01 '22 21:07 hannasm