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

Add Percentile support #86

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
77 changes: 67 additions & 10 deletions core/report/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package report

import (
"sort"
"strings"
"time"

Expand All @@ -34,14 +35,17 @@ func aggregate(result *Result, response *types.Response) {
scenarioDuration += float32(rr.Duration.Seconds())

if _, ok := result.ItemReports[rr.ScenarioItemID]; !ok {
result.ItemReports[rr.ScenarioItemID] = &ScenarioItemReport{
Name: rr.ScenarioItemName,
StatusCodeDist: make(map[int]int, 0),
ErrorDist: make(map[string]int),
Durations: map[string]float32{},
result.ItemReports[rr.ScenarioItemID] = &ScenarioResult{
Report: &ScenarioItemReport{
Name: rr.ScenarioItemName,
StatusCodeDist: make(map[int]int, 0),
ErrorDist: make(map[string]int),
Durations: map[string]float32{},
},
Durations: map[string]*ScenarioStats{},
}
}
item := result.ItemReports[rr.ScenarioItemID]
item := result.ItemReports[rr.ScenarioItemID].Report

if rr.Err.Type != "" {
errOccured = true
Expand All @@ -60,7 +64,17 @@ func aggregate(result *Result, response *types.Response) {
}
}
}
}

for _, report := range result.ItemReports {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this extra loop.

Copy link
Member

@kursataktas kursataktas Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add below code snippet at line 57 or anywhere in that else statement

if percentileReportEnabled {
   item.TotalDurations["duration"] = append(item.TotalDurations["duration"], float32(rr.Duration.Seconds())) 
}

I think the aggregator should store durations only if the percentage report is enabled.

for key, duration := range report.Report.Durations {
if report.Durations[key] == nil {
report.Durations[key] = &ScenarioStats{}
}

report.Durations[key].Durations = append(report.Durations[key].Durations, duration)
sort.Slice(report.Durations[key].Durations, func(i, j int) bool { return report.Durations[key].Durations[i] < report.Durations[key].Durations[j] })
}
}

// Don't change avg duration if there is a error
Expand All @@ -74,10 +88,10 @@ func aggregate(result *Result, response *types.Response) {
}

type Result struct {
SuccessCount int64 `json:"success_count"`
FailedCount int64 `json:"fail_count"`
AvgDuration float32 `json:"avg_duration"`
ItemReports map[int16]*ScenarioItemReport `json:"steps"`
SuccessCount int64 `json:"success_count"`
FailedCount int64 `json:"fail_count"`
AvgDuration float32 `json:"avg_duration"`
ItemReports map[int16]*ScenarioResult `json:"steps"`
zhiburt marked this conversation as resolved.
Show resolved Hide resolved
}

func (r *Result) successPercentage() int {
Expand All @@ -95,6 +109,49 @@ func (r *Result) failedPercentage() int {
return 100 - r.successPercentage()
}

func (r *Result) DurationPercentile(p int) float32 {
if p < 0 || p > 100 {
return 0
}

var durations []float32
for _, rr := range r.ItemReports {
if list, ok := rr.Durations["duration"]; ok {
durations = append(durations, list.Durations...)
}
}

sort.Slice(durations, func(i, j int) bool { return durations[i] < durations[j] })

d := ScenarioStats{Durations: durations}

return d.Percentile(p)
}

type ScenarioResult struct {
Report *ScenarioItemReport `json:"report"`
Durations map[string]*ScenarioStats `json:"durations"`
}

type ScenarioStats struct {
Durations []float32 `json:"durations"`
}

func (r *ScenarioStats) Percentile(p int) float32 {
if p < 0 || p > 100 {
return 0
}

// n = (P/100) x N
n := (p / 100) * len(r.Durations)
if n > 0 {
// I am not sure about the case where n == 0
n--
}

return r.Durations[n]
}

type ScenarioItemReport struct {
Name string `json:"name"`
StatusCodeDist map[int]int `json:"status_code_dist"`
Expand Down
19 changes: 11 additions & 8 deletions core/report/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ type stdout struct {
var blue = color.New(color.FgHiBlue).SprintFunc()
var green = color.New(color.FgHiGreen).SprintFunc()
var red = color.New(color.FgHiRed).SprintFunc()
var cyan = color.New(color.FgHiCyan).SprintFunc()
var realTimePrintInterval = time.Duration(1500) * time.Millisecond

func (s *stdout) Init() (err error) {
s.doneChan = make(chan struct{})
s.result = &Result{
ItemReports: make(map[int16]*ScenarioItemReport),
ItemReports: make(map[int16]*ScenarioResult),
}

color.Cyan("%s Initializing... \n", emoji.Gear)
Expand Down Expand Up @@ -107,12 +108,14 @@ func (s *stdout) realTimePrintStart() {
}

func (s *stdout) liveResultPrint() {
fmt.Fprintf(out, "%s %s %s\n",
green(fmt.Sprintf("%s Successful Run: %-6d %3d%% %5s",
emoji.CheckMark, s.result.SuccessCount, s.result.successPercentage(), "")),
red(fmt.Sprintf("%s Failed Run: %-6d %3d%% %5s",
emoji.CrossMark, s.result.FailedCount, s.result.failedPercentage(), "")),
blue(fmt.Sprintf("%s Avg. Duration: %.5fs", emoji.Stopwatch, s.result.AvgDuration)))
p := s.result.DurationPercentile(95)

fmt.Fprintf(out, "%s %s %s %s\n",
green(fmt.Sprintf("%s Successful Run: %-6d %3d%% %5s", emoji.CheckMark, s.result.SuccessCount, s.result.successPercentage(), "")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The max. line-length is limited to 120 chars as stated in the linter settings (at .golangci.yml). Let's follow the standards.

red(fmt.Sprintf("%s Failed Run: %-6d %3d%% %5s", emoji.CrossMark, s.result.FailedCount, s.result.failedPercentage(), "")),
blue(fmt.Sprintf("%s Avg. Duration: %.5fs", emoji.Stopwatch, s.result.AvgDuration)),
cyan(fmt.Sprintf("%s p(95): %.5fs", emoji.HourglassNotDone, p)),
)
}

func (s *stdout) realTimePrintStop() {
Expand Down Expand Up @@ -145,7 +148,7 @@ func (s *stdout) printDetails() {
sort.Ints(keys)

for _, k := range keys {
v := s.result.ItemReports[int16(k)]
v := s.result.ItemReports[int16(k)].Report

if len(keys) > 1 {
stepHeader := v.Name
Expand Down
5 changes: 3 additions & 2 deletions core/report/stdoutJson.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type stdoutJson struct {
func (s *stdoutJson) Init() (err error) {
s.doneChan = make(chan struct{})
s.result = &Result{
ItemReports: make(map[int16]*ScenarioItemReport),
ItemReports: make(map[int16]*ScenarioResult),
}
return
}
Expand All @@ -59,7 +59,8 @@ func (s *stdoutJson) Report() {

s.result.AvgDuration = float32(math.Round(float64(s.result.AvgDuration)*p) / p)

for _, itemReport := range s.result.ItemReports {
for _, item := range s.result.ItemReports {
itemReport := item.Report
durations := make(map[string]float32)
for d, s := range itemReport.Durations {
// Less precision for durations.
Expand Down
12 changes: 6 additions & 6 deletions core/report/stdoutJson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ func TestStdoutJsonStart(t *testing.T) {
SuccessCount: 1,
FailedCount: 1,
AvgDuration: 90,
ItemReports: map[int16]*ScenarioItemReport{
int16(1): itemReport1,
int16(2): itemReport2,
ItemReports: map[int16]*ScenarioResult{
int16(1): {Report: itemReport1},
int16(2): {Report: itemReport2},
},
}

Expand Down Expand Up @@ -186,9 +186,9 @@ func TestStdoutJsonOutput(t *testing.T) {
SuccessCount: 9,
FailedCount: 2,
AvgDuration: 0.25637,
ItemReports: map[int16]*ScenarioItemReport{
int16(1): itemReport1,
int16(2): itemReport2,
ItemReports: map[int16]*ScenarioResult{
int16(1): {Report: itemReport1},
int16(2): {Report: itemReport2},
},
}

Expand Down
6 changes: 3 additions & 3 deletions core/report/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ func TestStart(t *testing.T) {
SuccessCount: 1,
FailedCount: 1,
AvgDuration: 90,
ItemReports: map[int16]*ScenarioItemReport{
int16(1): itemReport1,
int16(2): itemReport2,
ItemReports: map[int16]*ScenarioResult{
int16(1): {Report: itemReport1},
int16(2): {Report: itemReport2},
},
}

Expand Down