From 78f7e5967a23c3cad56ac28db4f740a80f30a138 Mon Sep 17 00:00:00 2001 From: MSarthak Date: Fri, 18 Aug 2023 16:09:36 +0530 Subject: [PATCH] Sarthak | Humanizes the payload size in the report --- go.mod | 1 + go.sum | 2 ++ report/reporter.go | 15 +++++++-------- report/reporter_test.go | 4 ++-- report/template.go | 28 +++++++++++++++------------- report/template_test.go | 12 ++++++------ 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/go.mod b/go.mod index 111a694..58d8ab4 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e3dbef4..1ba53c6 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/report/reporter.go b/report/reporter.go index 631b51b..f5a801d 100644 --- a/report/reporter.go +++ b/report/reporter.go @@ -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 @@ -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 @@ -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, ) @@ -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( diff --git a/report/reporter_test.go b/report/reporter_test.go index a9f705b..b8d45df 100644 --- a/report/reporter_test.go +++ b/report/reporter_test.go @@ -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) { @@ -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, ) } diff --git a/report/template.go b/report/template.go index 9ff883d..528f2d0 100644 --- a/report/template.go +++ b/report/template.go @@ -5,6 +5,8 @@ import ( "io" "text/template" "time" + + "github.com/dustin/go-humanize" ) var templateText = ` @@ -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 }} @@ -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 }} @@ -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) } diff --git a/report/template_test.go b/report/template_test.go index ae53445..c8d8e22 100644 --- a/report/template_test.go +++ b/report/template_test.go @@ -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 @@ -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 @@ -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