mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-17 10:20:44 +00:00
Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
parent
7d40ca6f35
commit
e524043a3b
6 changed files with 22 additions and 35 deletions
1
go.mod
1
go.mod
|
|
@ -55,6 +55,7 @@ require (
|
||||||
github.com/mattn/go-isatty v0.0.17
|
github.com/mattn/go-isatty v0.0.17
|
||||||
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
|
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
|
||||||
github.com/urfave/cli/v2 v2.27.5
|
github.com/urfave/cli/v2 v2.27.5
|
||||||
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -218,6 +218,8 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
|
||||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||||
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
|
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
|
||||||
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
|
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
|
||||||
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||||
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||||
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
|
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
|
||||||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,10 @@ 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
|
||||||
|
|
@ -182,7 +183,7 @@ func (t *ResettingTimerSnapshot) Mean() float64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ResettingTimerSnapshot) calc(percentiles []float64) {
|
func (t *ResettingTimerSnapshot) calc(percentiles []float64) {
|
||||||
sort.Sort(Int64Slice(t.values))
|
slices.Sort(t.values)
|
||||||
|
|
||||||
count := len(t.values)
|
count := len(t.values)
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
|
|
@ -228,10 +229,3 @@ 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] }
|
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,10 @@ 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
|
||||||
|
|
@ -282,17 +283,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 int64Slice, p float64) float64 {
|
func SamplePercentile(values []int64, 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 int64Slice, ps []float64) []float64 {
|
func SamplePercentiles(values []int64, 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 {
|
||||||
sort.Sort(values)
|
slices.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 {
|
||||||
|
|
@ -633,9 +634,3 @@ 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] }
|
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,10 @@ package metrics
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"sort"
|
"strings"
|
||||||
"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
|
||||||
|
|
@ -18,12 +20,11 @@ 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 namedMetricSlice
|
var namedMetrics []namedMetric
|
||||||
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.cmp)
|
||||||
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:
|
||||||
|
|
@ -91,13 +92,6 @@ type namedMetric struct {
|
||||||
m interface{}
|
m interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// namedMetricSlice is a slice of namedMetrics that implements sort.Interface.
|
func (m namedMetric) cmp(other namedMetric) int {
|
||||||
type namedMetricSlice []namedMetric
|
return strings.Compare(m.name, other.name)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,20 @@
|
||||||
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 = namedMetricSlice{
|
var namedMetrics = []namedMetric{
|
||||||
{name: "zzz"},
|
{name: "zzz"},
|
||||||
{name: "bbb"},
|
{name: "bbb"},
|
||||||
{name: "fff"},
|
{name: "fff"},
|
||||||
{name: "ggg"},
|
{name: "ggg"},
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(namedMetrics)
|
slices.SortFunc(namedMetrics, namedMetric.cmp)
|
||||||
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()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue