Skip to content

Commit

Permalink
Add test cases for redrawing metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Sep 24, 2020
1 parent c660e40 commit a86bd81
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
11 changes: 9 additions & 2 deletions gui/drawer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ Wait: %v
Requests: %d
Rate: %f
Throughput: %f
Success: %f
StatusCodes:`
Success: %f`
)

for {
Expand Down Expand Up @@ -144,9 +143,17 @@ StatusCodes:`
metrics.Success,
), text.WriteReplace())

if len(metrics.StatusCodes) > 0 {
d.widgets.othersText.Write(`
StatusCodes:`)
}
for code, n := range metrics.StatusCodes {
d.widgets.othersText.Write(fmt.Sprintf(`
%s: %d`, code, n))
}
if len(metrics.Errors) > 0 {
d.widgets.othersText.Write(`
Errors:`)
}
for i, e := range metrics.Errors {
d.widgets.othersText.Write(fmt.Sprintf(`
Expand Down
109 changes: 109 additions & 0 deletions gui/drawer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,115 @@ func TestRedrawGauge(t *testing.T) {
}
}

func TestRedrawMetrics(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

tests := []struct {
name string
metrics *attacker.Metrics
latenciesText Text
bytesText Text
othersText Text
}{
{
name: "with errors",
metrics: &attacker.Metrics{
Latencies: attacker.LatencyMetrics{
Total: 1,
Mean: 1,
P50: 1,
P90: 1,
P95: 1,
P99: 1,
Max: 1,
Min: 1,
},
BytesIn: attacker.ByteMetrics{
Total: 1,
Mean: 1,
},
BytesOut: attacker.ByteMetrics{
Total: 1,
Mean: 1,
},
Earliest: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
Latest: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
End: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
Duration: 1,
Wait: 1,
Requests: 1,
Rate: 1,
Throughput: 1,
Success: 1,
StatusCodes: map[string]int{"200": 2},
Errors: []string{"error1"},
},
latenciesText: func() Text {
t := NewMockText(ctrl)
t.EXPECT().Write(`Total: 1ns
Mean: 1ns
P50: 1ns
P90: 1ns
P95: 1ns
P99: 1ns
Max: 1ns
Min: 1ns`, gomock.Any())
return t
}(),
bytesText: func() Text {
t := NewMockText(ctrl)
t.EXPECT().Write(`In:
Total: 1
Mean: 1
Out:
Total: 1
Mean: 1`, gomock.Any())
return t
}(),
othersText: func() Text {
t := NewMockText(ctrl)
t.EXPECT().Write(`Earliest: 2009-11-10 23:00:00 +0000 UTC
Latest: 2009-11-10 23:00:00 +0000 UTC
End: 2009-11-10 23:00:00 +0000 UTC
Duration: 1ns
Wait: 1ns
Requests: 1
Rate: 1.000000
Throughput: 1.000000
Success: 1.000000`, gomock.Any())
t.EXPECT().Write(`
StatusCodes:`)
t.EXPECT().Write(`
200: 2`)
t.EXPECT().Write(`
Errors:`)
t.EXPECT().Write(`
0: error1`)
return t
}(),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &drawer{
widgets: &widgets{
latenciesText: tt.latenciesText,
bytesText: tt.bytesText,
othersText: tt.othersText,
},
metricsCh: make(chan *attacker.Metrics),
}
go d.redrawMetrics(ctx)
d.metricsCh <- tt.metrics
})
}
}

func TestRedrawMessage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down

0 comments on commit a86bd81

Please sign in to comment.