-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathprint_test.go
More file actions
140 lines (128 loc) · 3.71 KB
/
Copy pathprint_test.go
File metadata and controls
140 lines (128 loc) · 3.71 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bytes"
"encoding/json"
"strings"
"testing"
"time"
)
func testSnapshotReport() *SnapshotReport {
return &SnapshotReport{
Elapsed: 1500 * time.Millisecond,
Count: 3,
Codes: map[string]int64{"2xx": 2, "5xx": 1},
Errors: map[string]int64{"connection reset by peer": 1},
RPS: 2,
ReadThroughput: 1.25,
WriteThroughput: 0.5,
concurrencyCount: 2,
Stats: &struct {
Min time.Duration
Mean time.Duration
StdDev time.Duration
Max time.Duration
}{10 * time.Millisecond, 20 * time.Millisecond, 5 * time.Millisecond, 30 * time.Millisecond},
RpsStats: &struct {
Min float64
Mean float64
StdDev float64
Max float64
}{1.11, 2.22, 0.33, 3.33},
Percentiles: []*struct {
Percentile float64
Latency time.Duration
}{
{0.50, 15 * time.Millisecond},
{0.99, 29 * time.Millisecond},
},
Histograms: []*struct {
Mean time.Duration
Count int
}{
{10 * time.Millisecond, 1},
{30 * time.Millisecond, 2},
},
}
}
func TestPrinterFormatJSONReportsProducesValidJSON(t *testing.T) {
printer := NewPrinter(3, 0, false, false)
var buf bytes.Buffer
printer.formatJSONReports(&buf, testSnapshotReport(), true, false)
var got struct {
Summary struct {
Count int64 `json:"Count"`
Counts map[string]int64 `json:"Counts"`
} `json:"Summary"`
Error map[string]int64 `json:"Error"`
Statistics json.RawMessage `json:"Statistics"`
Percentiles map[string]string `json:"Percentiles"`
Histograms json.RawMessage `json:"Histograms"`
}
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatalf("formatJSONReports produced invalid JSON: %v\n%s", err, buf.String())
}
for _, tt := range []struct {
key string
ok bool
}{
{"Summary", got.Summary.Count != 0 || got.Summary.Counts != nil},
{"Error", got.Error != nil},
{"Statistics", len(got.Statistics) != 0},
{"Percentiles", got.Percentiles != nil},
{"Histograms", len(got.Histograms) != 0},
} {
if !tt.ok {
t.Fatalf("JSON output is missing %q: %s", tt.key, buf.String())
}
}
if got.Summary.Count != 3 {
t.Fatalf("Summary.Count = %v, want 3", got.Summary.Count)
}
if got.Summary.Counts["2xx"] != 2 || got.Summary.Counts["5xx"] != 1 {
t.Fatalf("Summary.Counts = %#v, want 2xx=2 and 5xx=1", got.Summary.Counts)
}
if got.Percentiles["P50"] != "15ms" || got.Percentiles["P99"] != "29ms" {
t.Fatalf("Percentiles = %#v, want P50=15ms and P99=29ms", got.Percentiles)
}
}
func TestPrinterFormatTableReportsContainsFriendlySections(t *testing.T) {
printer := NewPrinter(3, time.Second, false, false)
var buf bytes.Buffer
printer.formatTableReports(&buf, testSnapshotReport(), true, false)
out := buf.String()
for _, want := range []string{
"Summary:",
"Elapsed",
"Count",
"2xx",
"5xx",
"Error:",
"connection reset by peer",
"Statistics",
"Latency Percentile:",
"Latency Histogram:",
} {
if !strings.Contains(out, want) {
t.Fatalf("table output is missing %q:\n%s", want, out)
}
}
}
func TestDurationToString(t *testing.T) {
d := 1234567 * time.Microsecond
if got := durationToString(d, false); got != "1.234567s" {
t.Fatalf("durationToString(%v, false) = %q, want 1.234567s", d, got)
}
if got := durationToString(1500*time.Millisecond, true); got != "1.5" {
t.Fatalf("durationToString(1.5s, true) = %q, want 1.5", got)
}
}
func TestAlignBulkPadsColumns(t *testing.T) {
bulk := [][]string{{"left", "1"}, {"much longer", "22"}}
alignBulk(bulk, AlignLeft, AlignRight)
if bulk[0][0] != "left " {
t.Fatalf("first column = %q, want left padded on the right", bulk[0][0])
}
if bulk[0][1] != " 1" {
t.Fatalf("second column = %q, want right aligned", bulk[0][1])
}
}