Revert "metrics: use slices package for sorting (#27493)"

This reverts commit e39f48953a.
This commit is contained in:
devopsbo3 2023-11-10 12:27:53 -06:00 committed by GitHub
parent 17ed788c5b
commit a95c2d613a
4 changed files with 34 additions and 18 deletions

View file

@ -2,10 +2,9 @@ package metrics
import ( import (
"math" "math"
"sort"
"sync" "sync"
"time" "time"
"golang.org/x/exp/slices"
) )
// Initial slice capacity for the values stored in a ResettingTimer // Initial slice capacity for the values stored in a ResettingTimer
@ -187,7 +186,7 @@ func (t *ResettingTimerSnapshot) Mean() float64 {
} }
func (t *ResettingTimerSnapshot) calc(percentiles []float64) { func (t *ResettingTimerSnapshot) calc(percentiles []float64) {
slices.Sort(t.values) sort.Sort(Int64Slice(t.values))
count := len(t.values) count := len(t.values)
if count > 0 { if count > 0 {
@ -233,3 +232,10 @@ func (t *ResettingTimerSnapshot) calc(percentiles []float64) {
t.calculated = true t.calculated = true
} }
// Int64Slice attaches the methods of sort.Interface to []int64, sorting in increasing order.
type Int64Slice []int64
func (s Int64Slice) Len() int { return len(s) }
func (s Int64Slice) Less(i, j int) bool { return s[i] < s[j] }
func (s Int64Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

View file

@ -3,10 +3,9 @@ package metrics
import ( import (
"math" "math"
"math/rand" "math/rand"
"sort"
"sync" "sync"
"time" "time"
"golang.org/x/exp/slices"
) )
const rescaleThreshold = time.Hour const rescaleThreshold = time.Hour
@ -283,17 +282,17 @@ func SampleMin(values []int64) int64 {
} }
// SamplePercentiles returns an arbitrary percentile of the slice of int64. // SamplePercentiles returns an arbitrary percentile of the slice of int64.
func SamplePercentile(values []int64, p float64) float64 { func SamplePercentile(values int64Slice, p float64) float64 {
return SamplePercentiles(values, []float64{p})[0] return SamplePercentiles(values, []float64{p})[0]
} }
// SamplePercentiles returns a slice of arbitrary percentiles of the slice of // SamplePercentiles returns a slice of arbitrary percentiles of the slice of
// int64. // int64.
func SamplePercentiles(values []int64, ps []float64) []float64 { func SamplePercentiles(values int64Slice, ps []float64) []float64 {
scores := make([]float64, len(ps)) scores := make([]float64, len(ps))
size := len(values) size := len(values)
if size > 0 { if size > 0 {
slices.Sort(values) sort.Sort(values)
for i, p := range ps { for i, p := range ps {
pos := p * float64(size+1) pos := p * float64(size+1)
if pos < 1.0 { if pos < 1.0 {
@ -634,3 +633,9 @@ func (h *expDecaySampleHeap) down(i, n int) {
i = j i = j
} }
} }
type int64Slice []int64
func (p int64Slice) Len() int { return len(p) }
func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

View file

@ -3,9 +3,8 @@ package metrics
import ( import (
"fmt" "fmt"
"io" "io"
"sort"
"time" "time"
"golang.org/x/exp/slices"
) )
// Write sorts writes each metric in the given registry periodically to the // Write sorts writes each metric in the given registry periodically to the
@ -19,12 +18,12 @@ func Write(r Registry, d time.Duration, w io.Writer) {
// WriteOnce sorts and writes metrics in the given registry to the given // WriteOnce sorts and writes metrics in the given registry to the given
// io.Writer. // io.Writer.
func WriteOnce(r Registry, w io.Writer) { func WriteOnce(r Registry, w io.Writer) {
var namedMetrics []namedMetric var namedMetrics namedMetricSlice
r.Each(func(name string, i interface{}) { r.Each(func(name string, i interface{}) {
namedMetrics = append(namedMetrics, namedMetric{name, i}) namedMetrics = append(namedMetrics, namedMetric{name, i})
}) })
slices.SortFunc(namedMetrics, namedMetric.less) sort.Sort(namedMetrics)
for _, namedMetric := range namedMetrics { for _, namedMetric := range namedMetrics {
switch metric := namedMetric.m.(type) { switch metric := namedMetric.m.(type) {
case Counter: case Counter:
@ -92,6 +91,13 @@ type namedMetric struct {
m interface{} m interface{}
} }
func (m namedMetric) less(other namedMetric) bool { // namedMetricSlice is a slice of namedMetrics that implements sort.Interface.
return m.name < other.name type namedMetricSlice []namedMetric
func (nms namedMetricSlice) Len() int { return len(nms) }
func (nms namedMetricSlice) Swap(i, j int) { nms[i], nms[j] = nms[j], nms[i] }
func (nms namedMetricSlice) Less(i, j int) bool {
return nms[i].name < nms[j].name
} }

View file

@ -1,20 +1,19 @@
package metrics package metrics
import ( import (
"sort"
"testing" "testing"
"golang.org/x/exp/slices"
) )
func TestMetricsSorting(t *testing.T) { func TestMetricsSorting(t *testing.T) {
var namedMetrics = []namedMetric{ var namedMetrics = namedMetricSlice{
{name: "zzz"}, {name: "zzz"},
{name: "bbb"}, {name: "bbb"},
{name: "fff"}, {name: "fff"},
{name: "ggg"}, {name: "ggg"},
} }
slices.SortFunc(namedMetrics, namedMetric.less) sort.Sort(namedMetrics)
for i, name := range []string{"bbb", "fff", "ggg", "zzz"} { for i, name := range []string{"bbb", "fff", "ggg", "zzz"} {
if namedMetrics[i].name != name { if namedMetrics[i].name != name {
t.Fail() t.Fail()