computeLatencies computes percentile latencies based on durations stored in the stats object and updates the corresponding fields in the result object.
(result *BenchResults)
| 409 | // computeLatencies computes percentile latencies based on durations stored in |
| 410 | // the stats object and updates the corresponding fields in the result object. |
| 411 | func (s *Stats) computeLatencies(result *BenchResults) { |
| 412 | if len(s.hw.durations) == 0 { |
| 413 | return |
| 414 | } |
| 415 | sort.Sort(s.hw.durations) |
| 416 | minDuration := int64(s.hw.durations[0]) |
| 417 | maxDuration := int64(s.hw.durations[len(s.hw.durations)-1]) |
| 418 | |
| 419 | // Use the largest unit that can represent the minimum time duration. |
| 420 | s.hw.unit = time.Nanosecond |
| 421 | for _, u := range []time.Duration{time.Microsecond, time.Millisecond, time.Second} { |
| 422 | if minDuration <= int64(u) { |
| 423 | break |
| 424 | } |
| 425 | s.hw.unit = u |
| 426 | } |
| 427 | |
| 428 | numBuckets := s.numBuckets |
| 429 | if n := int(maxDuration - minDuration + 1); n < numBuckets { |
| 430 | numBuckets = n |
| 431 | } |
| 432 | s.hw.histogram = NewHistogram(HistogramOptions{ |
| 433 | NumBuckets: numBuckets, |
| 434 | // max-min(lower bound of last bucket) = (1 + growthFactor)^(numBuckets-2) * baseBucketSize. |
| 435 | GrowthFactor: math.Pow(float64(maxDuration-minDuration), 1/float64(numBuckets-2)) - 1, |
| 436 | BaseBucketSize: 1.0, |
| 437 | MinValue: minDuration, |
| 438 | }) |
| 439 | for _, d := range s.hw.durations { |
| 440 | s.hw.histogram.Add(int64(d)) |
| 441 | } |
| 442 | result.Data.Fiftieth = s.hw.durations[max(s.hw.histogram.Count*int64(50)/100-1, 0)] |
| 443 | result.Data.Ninetieth = s.hw.durations[max(s.hw.histogram.Count*int64(90)/100-1, 0)] |
| 444 | result.Data.NinetyNinth = s.hw.durations[max(s.hw.histogram.Count*int64(99)/100-1, 0)] |
| 445 | result.Data.Average = time.Duration(float64(s.hw.histogram.Sum) / float64(s.hw.histogram.Count)) |
| 446 | } |
| 447 | |
| 448 | // dump returns a printable version. |
| 449 | func (s *Stats) dump(result *BenchResults) { |
no test coverage detected