Skip to content

Commit 4194c3a

Browse files
committed
api_responsiveness: add mode to print (verb, resources) by their count.
1 parent 5646aff commit 4194c3a

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

api_responsiveness/main.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"fmt"
88
"io/ioutil"
99
"log"
10+
"sort"
11+
"strconv"
1012
"time"
1113
)
1214

@@ -18,6 +20,7 @@ const (
1820
var (
1921
threshold = flag.Float64("threshold", 0.1, "Failure threshold.")
2022
mode = flag.String("mode", "compare", "Mode")
23+
baseline = flag.String("baseline", "", "")
2124
)
2225

2326
type APIResponsiveness struct {
@@ -38,6 +41,7 @@ type Labels struct {
3841
Scope string `json:"Scope"`
3942
Subresource string `json:"Subresource"`
4043
Verb string `json:"Verb"`
44+
Count string `json:"Count"`
4145
}
4246

4347
func (l *Labels) asKey() string {
@@ -46,7 +50,14 @@ func (l *Labels) asKey() string {
4650
key = fmt.Sprintf("%s/%s", key, l.Subresource)
4751
}
4852
return key
53+
}
4954

55+
func (l *Labels) count() int {
56+
i, err := strconv.Atoi(l.Count)
57+
if err != nil {
58+
log.Fatalf("cannot convert count: %s", l.Count)
59+
}
60+
return i
5061
}
5162

5263
func (d *APIResponsiveness) asMap() map[string]time.Duration {
@@ -99,32 +110,41 @@ func compareResults(base, result *APIResponsiveness) {
99110
fmt.Printf("good: %d, bad %d\n", good, bad)
100111
}
101112

102-
func compare() error {
103-
if flag.NArg() != 2 {
104-
return errors.New("expected 2 positional arguments: path to baseline and result")
105-
}
106-
107-
path := flag.Args()[0]
108-
baseline, err := parseResults(path)
113+
func compare(result *APIResponsiveness) error {
114+
baseline, err := parseResults(*baseline)
109115
if err != nil {
110116
return err
111117
}
112-
path = flag.Args()[1]
113-
result, err := parseResults(path)
114-
if err != nil {
115-
return err
116-
117-
}
118118
compareResults(baseline, result)
119119
return nil
120120
}
121121

122+
func printSorted(result *APIResponsiveness) {
123+
sort.Slice(result.DataItems, func(i, j int) bool {
124+
return result.DataItems[i].Labels.count() > result.DataItems[j].Labels.count()
125+
})
126+
for _, i := range result.DataItems {
127+
fmt.Printf("%d %s\n", i.Labels.count(), i.Labels.asKey())
128+
}
129+
}
130+
122131
func main() {
123132
flag.Parse()
124-
var err error
133+
if flag.NArg() != 1 {
134+
log.Fatalf("expected 1 positional arguments: path to result, got: %v", flag.Args())
135+
}
136+
137+
result, err := parseResults(flag.Arg(0))
138+
if err != nil {
139+
log.Fatalf("error while parsing result: %v", err)
140+
141+
}
142+
125143
switch *mode {
126144
case "compare":
127-
err = compare()
145+
err = compare(result)
146+
case "sort":
147+
printSorted(result)
128148
default:
129149
err = errors.New("unknown mode")
130150
}

0 commit comments

Comments
 (0)