Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
4367ab49
Unverified
Commit
4367ab49
authored
Jun 19, 2023
by
Dan Laine
Committed by
GitHub
Jun 19, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
metrics: use slices package for sorting (#27493)
Co-authored-by:
Felix Lange
<
fjl@twurst.com
>
parent
760fd0c7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
18 additions
and
34 deletions
+18
-34
resetting_timer.go
metrics/resetting_timer.go
+3
-9
sample.go
metrics/sample.go
+5
-10
writer.go
metrics/writer.go
+6
-12
writer_test.go
metrics/writer_test.go
+4
-3
No files found.
metrics/resetting_timer.go
View file @
4367ab49
...
...
@@ -2,9 +2,10 @@ package metrics
import
(
"math"
"sort"
"sync"
"time"
"golang.org/x/exp/slices"
)
// Initial slice capacity for the values stored in a ResettingTimer
...
...
@@ -186,7 +187,7 @@ func (t *ResettingTimerSnapshot) Mean() float64 {
}
func
(
t
*
ResettingTimerSnapshot
)
calc
(
percentiles
[]
float64
)
{
s
ort
.
Sort
(
Int64Slice
(
t
.
values
)
)
s
lices
.
Sort
(
t
.
values
)
count
:=
len
(
t
.
values
)
if
count
>
0
{
...
...
@@ -232,10 +233,3 @@ func (t *ResettingTimerSnapshot) calc(percentiles []float64) {
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
]
}
metrics/sample.go
View file @
4367ab49
...
...
@@ -3,9 +3,10 @@ package metrics
import
(
"math"
"math/rand"
"sort"
"sync"
"time"
"golang.org/x/exp/slices"
)
const
rescaleThreshold
=
time
.
Hour
...
...
@@ -282,17 +283,17 @@ func SampleMin(values []int64) 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
]
}
// SamplePercentiles returns a slice of arbitrary percentiles of the slice of
// int64.
func
SamplePercentiles
(
values
int64Slice
,
ps
[]
float64
)
[]
float64
{
func
SamplePercentiles
(
values
[]
int64
,
ps
[]
float64
)
[]
float64
{
scores
:=
make
([]
float64
,
len
(
ps
))
size
:=
len
(
values
)
if
size
>
0
{
s
ort
.
Sort
(
values
)
s
lices
.
Sort
(
values
)
for
i
,
p
:=
range
ps
{
pos
:=
p
*
float64
(
size
+
1
)
if
pos
<
1.0
{
...
...
@@ -633,9 +634,3 @@ func (h *expDecaySampleHeap) down(i, n int) {
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
]
}
metrics/writer.go
View file @
4367ab49
...
...
@@ -3,8 +3,9 @@ package metrics
import
(
"fmt"
"io"
"sort"
"time"
"golang.org/x/exp/slices"
)
// Write sorts writes each metric in the given registry periodically to the
...
...
@@ -18,12 +19,12 @@ func Write(r Registry, d time.Duration, w io.Writer) {
// WriteOnce sorts and writes metrics in the given registry to the given
// io.Writer.
func
WriteOnce
(
r
Registry
,
w
io
.
Writer
)
{
var
namedMetrics
namedMetricSlice
var
namedMetrics
[]
namedMetric
r
.
Each
(
func
(
name
string
,
i
interface
{})
{
namedMetrics
=
append
(
namedMetrics
,
namedMetric
{
name
,
i
})
})
s
ort
.
Sort
(
namedMetric
s
)
s
lices
.
SortFunc
(
namedMetrics
,
namedMetric
.
les
s
)
for
_
,
namedMetric
:=
range
namedMetrics
{
switch
metric
:=
namedMetric
.
m
.
(
type
)
{
case
Counter
:
...
...
@@ -91,13 +92,6 @@ type namedMetric struct {
m
interface
{}
}
// namedMetricSlice is a slice of namedMetrics that implements sort.Interface.
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
func
(
m
namedMetric
)
less
(
other
namedMetric
)
bool
{
return
m
.
name
<
other
.
name
}
metrics/writer_test.go
View file @
4367ab49
package
metrics
import
(
"sort"
"testing"
"golang.org/x/exp/slices"
)
func
TestMetricsSorting
(
t
*
testing
.
T
)
{
var
namedMetrics
=
namedMetricSlice
{
var
namedMetrics
=
[]
namedMetric
{
{
name
:
"zzz"
},
{
name
:
"bbb"
},
{
name
:
"fff"
},
{
name
:
"ggg"
},
}
s
ort
.
Sort
(
namedMetric
s
)
s
lices
.
SortFunc
(
namedMetrics
,
namedMetric
.
les
s
)
for
i
,
name
:=
range
[]
string
{
"bbb"
,
"fff"
,
"ggg"
,
"zzz"
}
{
if
namedMetrics
[
i
]
.
name
!=
name
{
t
.
Fail
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment