Skip to content

Commit

Permalink
Sarthak | Humanizes the payload size in the report
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Aug 18, 2023
1 parent 2838715 commit 78f7e59
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
require (
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dimiro1/banner v1.1.0 h1:TSfy+FsPIIGLzaMPOt52KrEed/omwFO1P15VA8PMUh0=
github.com/dimiro1/banner v1.1.0/go.mod h1:tbL318TJiUaHxOUNN+jnlvFSgsh/RX7iJaQrGgOiTco=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/ionrock/procs v0.0.0-20230108235056-4ba188ce3ead h1:HymphmYgW0oFMQctBj375exWgT0Ke+5EMPKpIYVWGkk=
Expand Down
15 changes: 7 additions & 8 deletions report/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type LoadMetrics struct {
ErrorCount uint
ErrorCountByType map[string]uint
TotalPayloadLengthBytes int64
AveragePayloadLengthBytes float64
AveragePayloadLengthBytes int64
EarliestLoadSendTime time.Time
LatestLoadSendTime time.Time
TotalTime time.Duration
Expand All @@ -30,7 +30,7 @@ type ResponseMetrics struct {
ErrorCount uint
ErrorCountByType map[string]uint
TotalResponsePayloadLengthBytes int64
AverageResponsePayloadLengthBytes float64
AverageResponsePayloadLengthBytes int64
EarliestResponseReceivedTime time.Time
LatestResponseReceivedTime time.Time
IsAvailableForReporting bool
Expand Down Expand Up @@ -109,9 +109,7 @@ func (reporter *Reporter) collectLoadMetrics() {
reporter.report.Load.SuccessCount++
}
reporter.report.Load.TotalPayloadLengthBytes += load.PayloadLengthBytes
reporter.report.Load.AveragePayloadLengthBytes = float64(
reporter.report.Load.TotalPayloadLengthBytes,
) / float64(
reporter.report.Load.AveragePayloadLengthBytes = reporter.report.Load.TotalPayloadLengthBytes / int64(
totalGeneratedLoad,
)

Expand Down Expand Up @@ -146,9 +144,10 @@ func (reporter *Reporter) collectResponseMetrics() {
reporter.report.Response.SuccessCount++
}
reporter.report.Response.TotalResponsePayloadLengthBytes += response.PayloadLengthBytes
reporter.report.Response.AverageResponsePayloadLengthBytes = float64(
reporter.report.Response.TotalResponsePayloadLengthBytes,
) / float64(totalResponses)
reporter.report.Response.AverageResponsePayloadLengthBytes = reporter.report.Response.TotalResponsePayloadLengthBytes /
int64(
totalResponses,
)

if reporter.report.Response.EarliestResponseReceivedTime.IsZero() ||
response.ResponseTime.Before(
Expand Down
4 changes: 2 additions & 2 deletions report/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestReportWithPayloadLengthInGeneratingLoad(t *testing.T) {
close(loadGenerationChannel)

assert.Equal(t, int64(20), reporter.report.Load.TotalPayloadLengthBytes)
assert.Equal(t, float64(10.0), reporter.report.Load.AveragePayloadLengthBytes)
assert.Equal(t, int64(10), reporter.report.Load.AveragePayloadLengthBytes)
}

func TestReportWithLoadTimeInGeneratingLoad(t *testing.T) {
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestReportWithResponsePayloadLengthInReceivingResponse(t *testing.T) {
assert.Equal(t, int64(20), reporter.report.Response.TotalResponsePayloadLengthBytes)
assert.Equal(
t,
float64(10.0),
int64(10),
reporter.report.Response.AverageResponsePayloadLengthBytes,
)
}
Expand Down
28 changes: 15 additions & 13 deletions report/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io"
"text/template"
"time"

"github.com/dustin/go-humanize"
)

var templateText = `
Expand All @@ -13,8 +15,8 @@ Summary:
TotalRequests: {{ formatNumberUint .Load.TotalRequests }}
SuccessCount: {{ formatNumberUint .Load.SuccessCount }}
ErrorCount: {{ formatNumberUint .Load.ErrorCount }}
TotalPayloadSize: {{ formatNumberInt64 .Load.TotalPayloadLengthBytes }} bytes
AveragePayloadSize: {{ formatNumberFloat .Load.AveragePayloadLengthBytes }} bytes
TotalPayloadSize: {{ humanizePayloadSize .Load.TotalPayloadLengthBytes }}
AveragePayloadSize: {{ humanizePayloadSize .Load.AveragePayloadLengthBytes }}
EarliestLoadSendTime: {{ formatTime .Load.EarliestLoadSendTime}}
LatestLoadSendTime: {{ formatTime .Load.LatestLoadSendTime}}
TimeToCompleteLoad: {{ formatDuration .Load.TotalTime }}
Expand All @@ -26,8 +28,8 @@ Summary:
ResponseMetrics:
SuccessCount: {{ formatNumberUint .Response.SuccessCount }}
ErrorCount: {{ formatNumberUint .Response.ErrorCount }}
TotalResponsePayloadSize: {{ formatNumberInt64 .Response.TotalResponsePayloadLengthBytes }} bytes
AverageResponsePayloadSize: {{ formatNumberFloat .Response.AverageResponsePayloadLengthBytes }} bytes
TotalResponsePayloadSize: {{ humanizePayloadSize .Response.TotalResponsePayloadLengthBytes }}
AverageResponsePayloadSize: {{ humanizePayloadSize .Response.AverageResponsePayloadLengthBytes }}
EarliestResponseReceivedTime: {{ formatTime .Response.EarliestResponseReceivedTime }}
LatestResponseReceivedTime: {{ formatTime .Response.LatestResponseReceivedTime }}
TimeToGetResponses: {{ formatDuration .Response.TotalTime }}
Expand All @@ -38,23 +40,23 @@ Summary:
`

var functions = template.FuncMap{
"formatNumberFloat": formatNumberFloat,
"formatNumberUint": formatNumberUint,
"formatNumberInt64": formatNumberInt64,
"formatTime": formatTime,
"formatDuration": formatDuration,
"formatNumberUint": formatNumberUint,
"formatNumberInt64": formatNumberInt64,
"formatTime": formatTime,
"formatDuration": formatDuration,
"humanizePayloadSize": humanizePayloadSize,
}

const timeFormat = "January 02, 2006 15:04:05 MST"

func formatNumberFloat(value float64) string {
return fmt.Sprintf("%4.4f", value)
}

func formatNumberUint(value uint) string {
return fmt.Sprintf("%d", value)
}

func humanizePayloadSize(size int64) string {
return humanize.Bytes(uint64(size))
}

func formatNumberInt64(value int64) string {
return fmt.Sprintf("%d", value)
}
Expand Down
12 changes: 6 additions & 6 deletions report/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Summary:
TotalRequests: 1000
SuccessCount: 999
ErrorCount: 1
TotalPayloadSize: 2000 bytes
AveragePayloadSize: 20.0000 bytes
TotalPayloadSize: 2.0 kB
AveragePayloadSize: 20 B
EarliestLoadSendTime: August 21, 2023 04:14:00 IST
LatestLoadSendTime: August 21, 2023 04:14:10 IST
TimeToCompleteLoad: 10s
Expand All @@ -29,8 +29,8 @@ Summary:
ResponseMetrics:
SuccessCount: 1000
ErrorCount: 1
TotalResponsePayloadSize: 1800 bytes
AverageResponsePayloadSize: 18.0000 bytes
TotalResponsePayloadSize: 1.8 kB
AverageResponsePayloadSize: 18 B
EarliestResponseReceivedTime: August 21, 2023 04:14:00 IST
LatestResponseReceivedTime: August 21, 2023 04:14:10 IST
TimeToGetResponses: 10s
Expand Down Expand Up @@ -83,8 +83,8 @@ Summary:
TotalRequests: 1000
SuccessCount: 999
ErrorCount: 1
TotalPayloadSize: 2000 bytes
AveragePayloadSize: 20.0000 bytes
TotalPayloadSize: 2.0 kB
AveragePayloadSize: 20 B
EarliestLoadSendTime: August 21, 2023 04:14:00 IST
LatestLoadSendTime: August 21, 2023 04:14:00 IST
TimeToCompleteLoad: 0s
Expand Down

0 comments on commit 78f7e59

Please sign in to comment.