| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package zpages |
| 5 |
|
| 6 |
import ( |
| 7 |
"slices" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
zeroDuration = time.Duration(0) |
| 13 |
maxDuration = time.Duration(1<<63 - 1) |
| 14 |
) |
| 15 |
|
| 16 |
var defaultBoundaries = newBoundaries([]time.Duration{ |
| 17 |
10 * time.Microsecond, |
| 18 |
100 * time.Microsecond, |
| 19 |
time.Millisecond, |
| 20 |
10 * time.Millisecond, |
| 21 |
100 * time.Millisecond, |
| 22 |
time.Second, |
| 23 |
10 * time.Second, |
| 24 |
100 * time.Second, |
| 25 |
}) |
| 26 |
|
| 27 |
|
| 28 |
type boundaries struct { |
| 29 |
durations []time.Duration |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
func newBoundaries(durations []time.Duration) *boundaries { |
| 34 |
slices.Sort(durations) |
| 35 |
return &boundaries{durations: durations} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func (lb boundaries) numBuckets() int { |
| 40 |
return len(lb.durations) + 1 |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func (lb boundaries) getBucketIndex(latency time.Duration) int { |
| 45 |
i := 0 |
| 46 |
for i < len(lb.durations) && latency >= lb.durations[i] { |
| 47 |
i++ |
| 48 |
} |
| 49 |
return i |
| 50 |
} |