-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile_sorter.go
106 lines (94 loc) · 2.31 KB
/
profile_sorter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ghaprofiler
import (
"fmt"
"sort"
"strings"
)
var availableSortFields = []string{
"number",
"min",
"max",
"mean",
"median",
"p50",
"p90",
"p95",
"p99",
}
type taskStepProfileSorter struct {
taskStepProfiles []*TaskStepProfile
by func(t1, t2 *TaskStepProfile) bool
}
type taskStepProfileSortBy func(t1, t2 *TaskStepProfile) bool
func (by taskStepProfileSortBy) Sort(taskSteps []*TaskStepProfile) {
ts := &taskStepProfileSorter{
taskStepProfiles: taskSteps,
by: by,
}
sort.Sort(ts)
}
func (ts *taskStepProfileSorter) Len() int {
return len(ts.taskStepProfiles)
}
func (ts *taskStepProfileSorter) Swap(i, j int) {
ts.taskStepProfiles[i], ts.taskStepProfiles[j] = ts.taskStepProfiles[j], ts.taskStepProfiles[i]
}
func (ts *taskStepProfileSorter) Less(i, j int) bool {
return ts.by(ts.taskStepProfiles[i], ts.taskStepProfiles[j])
}
func SortProfileBy(profile TaskStepProfileResult, fieldName string) error {
var by taskStepProfileSortBy
switch fieldName {
case "number":
by = func(t1, t2 *TaskStepProfile) bool {
return t1.Number < t2.Number
}
case "min":
by = func(t1, t2 *TaskStepProfile) bool {
return t1.Min < t2.Min
}
case "max":
by = func(t1, t2 *TaskStepProfile) bool {
return t1.Max < t2.Max
}
case "mean":
by = func(t1, t2 *TaskStepProfile) bool {
return t1.Mean < t2.Mean
}
case "median":
by = func(t1, t2 *TaskStepProfile) bool {
return t1.Median < t2.Median
}
case "p50":
by = func(t1, t2 *TaskStepProfile) bool {
return t1.Percentiles[50].Value < t2.Percentiles[50].Value
}
case "p90":
by = func(t1, t2 *TaskStepProfile) bool {
return t1.Percentiles[90].Value < t2.Percentiles[90].Value
}
case "p95":
by = func(t1, t2 *TaskStepProfile) bool {
return t1.Percentiles[95].Value < t2.Percentiles[95].Value
}
case "p99":
by = func(t1, t2 *TaskStepProfile) bool {
return t1.Percentiles[99].Value < t2.Percentiles[99].Value
}
default:
return fmt.Errorf("Invalid field: %s", fieldName)
}
taskStepProfileSortBy(by).Sort(profile)
return nil
}
func AvailableSortFieldsForCLI() string {
return strings.Join(availableSortFields, ", ")
}
func IsValidSortFieldName(fieldName string) bool {
for _, availableName := range availableSortFields {
if fieldName == availableName {
return true
}
}
return false
}