Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to show the QPS in the report #21

Merged
merged 1 commit into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bin/
.idea/
coverage.out
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ build:
copy: build
sudo cp bin/atest /usr/local/bin/
test:
go test ./... -cover
go test ./... -cover -v -coverprofile=coverage.out
go tool cover -func=coverage.out
14 changes: 14 additions & 0 deletions pkg/runner/reporter_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func NewMemoryTestReporter() TestReporter {
type ReportResultWithTotal struct {
ReportResult
Total time.Duration
First time.Time
Last time.Time
}

// PutRecord puts the record to memory
Expand Down Expand Up @@ -50,6 +52,13 @@ func (r *memoryTestReporter) ExportAllReportResults() (result ReportResultSlice,
item.Error += record.ErrorCount()
item.Total += duration
item.Count += 1

if record.EndTime.After(item.Last) {
item.Last = record.EndTime
}
if record.BeginTime.Before(item.First) {
item.First = record.BeginTime
}
} else {
resultWithTotal[api] = &ReportResultWithTotal{
ReportResult: ReportResult{
Expand All @@ -59,13 +68,18 @@ func (r *memoryTestReporter) ExportAllReportResults() (result ReportResultSlice,
Min: duration,
Error: record.ErrorCount(),
},
First: record.BeginTime,
Last: record.EndTime,
Total: duration,
}
}
}

for _, r := range resultWithTotal {
r.Average = r.Total / time.Duration(r.Count)
if duration := int(r.Last.Sub(r.First).Seconds()); duration > 0 {
r.QPS = r.Count / duration
}
result = append(result, r.ReportResult)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/runner/reporter_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func TestExportAllReportResults(t *testing.T) {
Average: time.Second,
Max: time.Second,
Min: time.Second,
QPS: 1,
Count: 1,
Error: 0,
}},
Expand Down
1 change: 1 addition & 0 deletions pkg/runner/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type ReportResult struct {
Average time.Duration
Max time.Duration
Min time.Duration
QPS int
Error int
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/runner/writer_std.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func NewDiscardResultWriter() ReportResultWriter {

// Output writer the report to target writer
func (w *stdResultWriter) Output(result []ReportResult) error {
fmt.Fprintf(w.writer, "API Average Max Min Count Error\n")
fmt.Fprintf(w.writer, "API Average Max Min QPS Count Error\n")
for _, r := range result {
fmt.Fprintf(w.writer, "%s %v %v %v %d %d\n", r.API, r.Average, r.Max,
r.Min, r.Count, r.Error)
fmt.Fprintf(w.writer, "%s %v %v %v %d %d %d\n", r.API, r.Average, r.Max,
r.Min, r.QPS, r.Count, r.Error)
}
return nil
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/runner/writer_std_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestNewStdResultWriter(t *testing.T) {
name: "result is nil",
buf: new(bytes.Buffer),
results: nil,
expect: `API Average Max Min Count Error
expect: `API Average Max Min QPS Count Error
`,
}, {
name: "have one item",
Expand All @@ -55,11 +55,12 @@ func TestNewStdResultWriter(t *testing.T) {
Average: 1,
Max: 1,
Min: 1,
QPS: 10,
Count: 1,
Error: 0,
}},
expect: `API Average Max Min Count Error
api 1ns 1ns 1ns 1 0
expect: `API Average Max Min QPS Count Error
api 1ns 1ns 1ns 10 1 0
`,
}}
for _, tt := range tests {
Expand Down