Skip to content

Commit 136e82d

Browse files
authored
feat: support to show the QPS in the report (#21)
1 parent e577436 commit 136e82d

File tree

7 files changed

+26
-7
lines changed

7 files changed

+26
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
bin/
22
.idea/
3+
coverage.out

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ build:
66
copy: build
77
sudo cp bin/atest /usr/local/bin/
88
test:
9-
go test ./... -cover
9+
go test ./... -cover -v -coverprofile=coverage.out
10+
go tool cover -func=coverage.out

pkg/runner/reporter_memory.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func NewMemoryTestReporter() TestReporter {
2020
type ReportResultWithTotal struct {
2121
ReportResult
2222
Total time.Duration
23+
First time.Time
24+
Last time.Time
2325
}
2426

2527
// PutRecord puts the record to memory
@@ -50,6 +52,13 @@ func (r *memoryTestReporter) ExportAllReportResults() (result ReportResultSlice,
5052
item.Error += record.ErrorCount()
5153
item.Total += duration
5254
item.Count += 1
55+
56+
if record.EndTime.After(item.Last) {
57+
item.Last = record.EndTime
58+
}
59+
if record.BeginTime.Before(item.First) {
60+
item.First = record.BeginTime
61+
}
5362
} else {
5463
resultWithTotal[api] = &ReportResultWithTotal{
5564
ReportResult: ReportResult{
@@ -59,13 +68,18 @@ func (r *memoryTestReporter) ExportAllReportResults() (result ReportResultSlice,
5968
Min: duration,
6069
Error: record.ErrorCount(),
6170
},
71+
First: record.BeginTime,
72+
Last: record.EndTime,
6273
Total: duration,
6374
}
6475
}
6576
}
6677

6778
for _, r := range resultWithTotal {
6879
r.Average = r.Total / time.Duration(r.Count)
80+
if duration := int(r.Last.Sub(r.First).Seconds()); duration > 0 {
81+
r.QPS = r.Count / duration
82+
}
6983
result = append(result, r.ReportResult)
7084
}
7185

pkg/runner/reporter_memory_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func TestExportAllReportResults(t *testing.T) {
6969
Average: time.Second,
7070
Max: time.Second,
7171
Min: time.Second,
72+
QPS: 1,
7273
Count: 1,
7374
Error: 0,
7475
}},

pkg/runner/simple.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type ReportResult struct {
6464
Average time.Duration
6565
Max time.Duration
6666
Min time.Duration
67+
QPS int
6768
Error int
6869
}
6970

pkg/runner/writer_std.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ func NewDiscardResultWriter() ReportResultWriter {
2424

2525
// Output writer the report to target writer
2626
func (w *stdResultWriter) Output(result []ReportResult) error {
27-
fmt.Fprintf(w.writer, "API Average Max Min Count Error\n")
27+
fmt.Fprintf(w.writer, "API Average Max Min QPS Count Error\n")
2828
for _, r := range result {
29-
fmt.Fprintf(w.writer, "%s %v %v %v %d %d\n", r.API, r.Average, r.Max,
30-
r.Min, r.Count, r.Error)
29+
fmt.Fprintf(w.writer, "%s %v %v %v %d %d %d\n", r.API, r.Average, r.Max,
30+
r.Min, r.QPS, r.Count, r.Error)
3131
}
3232
return nil
3333
}

pkg/runner/writer_std_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestNewStdResultWriter(t *testing.T) {
4545
name: "result is nil",
4646
buf: new(bytes.Buffer),
4747
results: nil,
48-
expect: `API Average Max Min Count Error
48+
expect: `API Average Max Min QPS Count Error
4949
`,
5050
}, {
5151
name: "have one item",
@@ -55,11 +55,12 @@ func TestNewStdResultWriter(t *testing.T) {
5555
Average: 1,
5656
Max: 1,
5757
Min: 1,
58+
QPS: 10,
5859
Count: 1,
5960
Error: 0,
6061
}},
61-
expect: `API Average Max Min Count Error
62-
api 1ns 1ns 1ns 1 0
62+
expect: `API Average Max Min QPS Count Error
63+
api 1ns 1ns 1ns 10 1 0
6364
`,
6465
}}
6566
for _, tt := range tests {

0 commit comments

Comments
 (0)