diff --git a/report/reporter.go b/report/reporter.go index da0353b..cee7578 100644 --- a/report/reporter.go +++ b/report/reporter.go @@ -11,8 +11,10 @@ type Report struct { Response ResponseMetrics } -// TODO: Total connections, total requests, requests per second +// TODO: Total connections type LoadMetrics struct { + TotalRequests uint + RequestsPerSecond float64 SuccessCount uint ErrorCount uint ErrorCountByType map[string]uint @@ -64,7 +66,7 @@ func (reporter *Reporter) Run() { func (reporter *Reporter) collectLoadMetrics() { go func() { - totalGeneratedLoad := 0 + totalGeneratedLoad := uint(0) for load := range reporter.loadGenerationChannel { totalGeneratedLoad++ @@ -91,6 +93,15 @@ func (reporter *Reporter) collectLoadMetrics() { reporter.report.Load.LatestLoadSendTime = load.LoadGenerationTime } } + startTime := reporter.report.Load.EarliestLoadSendTime + timeToCompleteLoad := time.Now().Sub(startTime) + + reporter.report.Load.TotalRequests = totalGeneratedLoad + reporter.report.Load.RequestsPerSecond = float64( + totalGeneratedLoad, + ) / float64( + timeToCompleteLoad.Seconds(), + ) }() } diff --git a/report/reporter_test.go b/report/reporter_test.go index c883e67..6b4a57e 100644 --- a/report/reporter_test.go +++ b/report/reporter_test.go @@ -57,6 +57,20 @@ func TestReportWithAndWithoutErrorInGeneratingLoad(t *testing.T) { assert.Equal(t, uint(1), reporter.report.Load.ErrorCount) } +func TestReportWithTotalRequests(t *testing.T) { + loadGenerationChannel := make(chan workers.LoadGenerationResponse, 1) + reporter := NewReporter(loadGenerationChannel, nil) + reporter.Run() + + loadGenerationChannel <- workers.LoadGenerationResponse{ + Err: errors.New("test error"), + } + close(loadGenerationChannel) + time.Sleep(2 * time.Millisecond) + + assert.Equal(t, uint(1), reporter.report.Load.TotalRequests) +} + func TestReportWithPayloadLengthInGeneratingLoad(t *testing.T) { loadGenerationChannel := make(chan workers.LoadGenerationResponse, 1) reporter := NewReporter(loadGenerationChannel, nil) diff --git a/report/template.go b/report/template.go index a079d5e..8f7819e 100644 --- a/report/template.go +++ b/report/template.go @@ -10,6 +10,8 @@ import ( var templateText = ` Summary: LoadMetrics: + TotalRequests: {{ formatNumberUint .Load.TotalRequests }} + RequestsPerSecond: {{ formatNumberFloat .Load.RequestsPerSecond }} SuccessCount: {{ formatNumberUint .Load.SuccessCount }} ErrorCount: {{ formatNumberUint .Load.ErrorCount }} TotalPayloadSize: {{ formatNumberInt64 .Load.TotalPayloadLengthBytes }} bytes diff --git a/report/template_test.go b/report/template_test.go index dcc1de6..e93211c 100644 --- a/report/template_test.go +++ b/report/template_test.go @@ -12,9 +12,11 @@ import ( var expected = ` Summary: LoadMetrics: - SuccessCount: 1 + TotalRequests: 1000 + RequestsPerSecond: 55.3200 + SuccessCount: 999 ErrorCount: 1 - TotalPayloadSize: 20 bytes + TotalPayloadSize: 2000 bytes AveragePayloadSize: 20.0000 bytes EarliestLoadSendTime: August 21, 2023 04:14:00 IST LatestLoadSendTime: August 21, 2023 04:14:00 IST @@ -23,9 +25,9 @@ Summary: [1] load error ResponseMetrics: - SuccessCount: 1 + SuccessCount: 1000 ErrorCount: 1 - TotalResponsePayloadSize: 18 bytes + TotalResponsePayloadSize: 1800 bytes AverageResponsePayloadSize: 18.0000 bytes EarliestResponseReceivedTime: August 21, 2023 04:14:00 IST LatestResponseReceivedTime: August 21, 2023 04:14:00 IST @@ -40,19 +42,21 @@ func TestPrintsTheReport(t *testing.T) { report := &Report{ Load: LoadMetrics{ - SuccessCount: 1, + TotalRequests: 1000, + RequestsPerSecond: 55.32, + SuccessCount: 999, ErrorCount: 1, ErrorCountByType: map[string]uint{"load error": 1}, - TotalPayloadLengthBytes: 20, + TotalPayloadLengthBytes: 2000, AveragePayloadLengthBytes: 20.0, EarliestLoadSendTime: time, LatestLoadSendTime: time, }, Response: ResponseMetrics{ - SuccessCount: 1, + SuccessCount: 1000, ErrorCount: 1, ErrorCountByType: map[string]uint{"response error": 1}, - TotalResponsePayloadLengthBytes: 18, + TotalResponsePayloadLengthBytes: 1800, AverageResponsePayloadLengthBytes: 18.0, EarliestResponseReceivedTime: time, LatestResponseReceivedTime: time,