From a7eb4bf128b63c7f1d0bc4c3c58e0dea2ed2165f Mon Sep 17 00:00:00 2001 From: MSarthak Date: Wed, 16 Aug 2023 15:30:57 +0530 Subject: [PATCH] Sarthak | Moves LoadGenerationResponse in report package --- report/reporter.go | 6 ++---- report/reporter_test.go | 32 +++++++++++++++----------------- report/response_reader.go | 6 ++++++ workers/options.go | 12 ++++-------- workers/worker.go | 4 +++- workers/worker_group.go | 10 ++++++---- workers/worker_test.go | 8 +++++--- 7 files changed, 41 insertions(+), 37 deletions(-) diff --git a/report/reporter.go b/report/reporter.go index 5f93d3f..7e9ac73 100644 --- a/report/reporter.go +++ b/report/reporter.go @@ -2,8 +2,6 @@ package report import ( "time" - - "blast/workers" ) type Report struct { @@ -37,12 +35,12 @@ type ResponseMetrics struct { type Reporter struct { report *Report - loadGenerationChannel chan workers.LoadGenerationResponse + loadGenerationChannel chan LoadGenerationResponse responseChannel chan SubjectServerResponse } func NewReporter( - loadGenerationChannel chan workers.LoadGenerationResponse, + loadGenerationChannel chan LoadGenerationResponse, responseChannel chan SubjectServerResponse, ) Reporter { return Reporter{ diff --git a/report/reporter_test.go b/report/reporter_test.go index c035938..1c8a496 100644 --- a/report/reporter_test.go +++ b/report/reporter_test.go @@ -6,16 +6,14 @@ import ( "time" "github.com/stretchr/testify/assert" - - "blast/workers" ) func TestReportWithErrorInGeneratingLoad(t *testing.T) { - loadGenerationChannel := make(chan workers.LoadGenerationResponse, 1) + loadGenerationChannel := make(chan LoadGenerationResponse, 1) reporter := NewReporter(loadGenerationChannel, nil) reporter.Run() - loadGenerationChannel <- workers.LoadGenerationResponse{ + loadGenerationChannel <- LoadGenerationResponse{ Err: errors.New("test error"), } time.Sleep(2 * time.Millisecond) @@ -26,11 +24,11 @@ func TestReportWithErrorInGeneratingLoad(t *testing.T) { } func TestReportWithoutErrorInGeneratingLoad(t *testing.T) { - loadGenerationChannel := make(chan workers.LoadGenerationResponse, 1) + loadGenerationChannel := make(chan LoadGenerationResponse, 1) reporter := NewReporter(loadGenerationChannel, nil) reporter.Run() - loadGenerationChannel <- workers.LoadGenerationResponse{ + loadGenerationChannel <- LoadGenerationResponse{ PayloadLengthBytes: 15, } time.Sleep(2 * time.Millisecond) @@ -40,14 +38,14 @@ func TestReportWithoutErrorInGeneratingLoad(t *testing.T) { } func TestReportWithAndWithoutErrorInGeneratingLoad(t *testing.T) { - loadGenerationChannel := make(chan workers.LoadGenerationResponse, 1) + loadGenerationChannel := make(chan LoadGenerationResponse, 1) reporter := NewReporter(loadGenerationChannel, nil) reporter.Run() - loadGenerationChannel <- workers.LoadGenerationResponse{ + loadGenerationChannel <- LoadGenerationResponse{ PayloadLengthBytes: 15, } - loadGenerationChannel <- workers.LoadGenerationResponse{ + loadGenerationChannel <- LoadGenerationResponse{ Err: errors.New("test error"), } time.Sleep(2 * time.Millisecond) @@ -58,11 +56,11 @@ func TestReportWithAndWithoutErrorInGeneratingLoad(t *testing.T) { } func TestReportWithTotalRequests(t *testing.T) { - loadGenerationChannel := make(chan workers.LoadGenerationResponse, 1) + loadGenerationChannel := make(chan LoadGenerationResponse, 1) reporter := NewReporter(loadGenerationChannel, nil) reporter.Run() - loadGenerationChannel <- workers.LoadGenerationResponse{ + loadGenerationChannel <- LoadGenerationResponse{ Err: errors.New("test error"), } close(loadGenerationChannel) @@ -72,14 +70,14 @@ func TestReportWithTotalRequests(t *testing.T) { } func TestReportWithPayloadLengthInGeneratingLoad(t *testing.T) { - loadGenerationChannel := make(chan workers.LoadGenerationResponse, 1) + loadGenerationChannel := make(chan LoadGenerationResponse, 1) reporter := NewReporter(loadGenerationChannel, nil) reporter.Run() - loadGenerationChannel <- workers.LoadGenerationResponse{ + loadGenerationChannel <- LoadGenerationResponse{ PayloadLengthBytes: 10, } - loadGenerationChannel <- workers.LoadGenerationResponse{ + loadGenerationChannel <- LoadGenerationResponse{ PayloadLengthBytes: 10, } @@ -91,17 +89,17 @@ func TestReportWithPayloadLengthInGeneratingLoad(t *testing.T) { } func TestReportWithLoadTimeInGeneratingLoad(t *testing.T) { - loadGenerationChannel := make(chan workers.LoadGenerationResponse, 1) + loadGenerationChannel := make(chan LoadGenerationResponse, 1) reporter := NewReporter(loadGenerationChannel, nil) reporter.Run() now := time.Now() laterByTenSeconds := now.Add(10 * time.Second) - loadGenerationChannel <- workers.LoadGenerationResponse{ + loadGenerationChannel <- LoadGenerationResponse{ LoadGenerationTime: now, } - loadGenerationChannel <- workers.LoadGenerationResponse{ + loadGenerationChannel <- LoadGenerationResponse{ LoadGenerationTime: laterByTenSeconds, } diff --git a/report/response_reader.go b/report/response_reader.go index 531253d..7422ac8 100644 --- a/report/response_reader.go +++ b/report/response_reader.go @@ -7,6 +7,12 @@ import ( "time" ) +type LoadGenerationResponse struct { + Err error + PayloadLengthBytes int64 + LoadGenerationTime time.Time +} + type SubjectServerResponse struct { Err error ResponseTime time.Time diff --git a/workers/options.go b/workers/options.go index dbfbc19..9c48cd2 100644 --- a/workers/options.go +++ b/workers/options.go @@ -1,6 +1,8 @@ package workers -import "time" +import ( + "blast/report" +) type GroupOptions struct { concurrency uint @@ -17,13 +19,7 @@ type WorkerOptions struct { targetAddress string requestsPerSecond float64 stopChannel chan struct{} - loadGenerationResponse chan LoadGenerationResponse -} - -type LoadGenerationResponse struct { - Err error - PayloadLengthBytes int64 - LoadGenerationTime time.Time + loadGenerationResponse chan report.LoadGenerationResponse } func NewGroupOptions( diff --git a/workers/worker.go b/workers/worker.go index d37395b..3eafbdb 100644 --- a/workers/worker.go +++ b/workers/worker.go @@ -4,6 +4,8 @@ import ( "io" "sync" "time" + + "blast/report" ) type Worker struct { @@ -42,7 +44,7 @@ func (worker Worker) sendRequests() { func (worker Worker) sendRequest() { _, err := worker.connection.Write(worker.options.payload) - worker.options.loadGenerationResponse <- LoadGenerationResponse{ + worker.options.loadGenerationResponse <- report.LoadGenerationResponse{ Err: err, PayloadLengthBytes: int64(len(worker.options.payload)), LoadGenerationTime: time.Now(), diff --git a/workers/worker_group.go b/workers/worker_group.go index 8a72fbd..f49740a 100644 --- a/workers/worker_group.go +++ b/workers/worker_group.go @@ -3,6 +3,8 @@ package workers import ( "net" "sync" + + "blast/report" ) type WorkerGroup struct { @@ -14,15 +16,15 @@ func NewWorkerGroup(options GroupOptions) *WorkerGroup { return &WorkerGroup{options: options, stopChannel: make(chan struct{}, options.concurrency)} } -func (group *WorkerGroup) Run() chan LoadGenerationResponse { - loadGenerationResponse := make(chan LoadGenerationResponse, group.options.totalRequests) +func (group *WorkerGroup) Run() chan report.LoadGenerationResponse { + loadGenerationResponse := make(chan report.LoadGenerationResponse, group.options.totalRequests) group.runWorkers(loadGenerationResponse) group.finish(loadGenerationResponse) return loadGenerationResponse } -func (group *WorkerGroup) runWorkers(loadGenerationResponse chan LoadGenerationResponse) { +func (group *WorkerGroup) runWorkers(loadGenerationResponse chan report.LoadGenerationResponse) { var wg sync.WaitGroup wg.Add(int(group.options.concurrency)) @@ -57,6 +59,6 @@ func (group *WorkerGroup) runWorkers(loadGenerationResponse chan LoadGenerationR wg.Wait() } -func (group *WorkerGroup) finish(loadGenerationResponse chan LoadGenerationResponse) { +func (group *WorkerGroup) finish(loadGenerationResponse chan report.LoadGenerationResponse) { close(loadGenerationResponse) } diff --git a/workers/worker_test.go b/workers/worker_test.go index 2b7cb32..377229d 100644 --- a/workers/worker_test.go +++ b/workers/worker_test.go @@ -7,6 +7,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "blast/report" ) type BytesWriteCloser struct { @@ -18,7 +20,7 @@ func (writeCloser *BytesWriteCloser) Close() error { } func TestWritesPayloadByWorker(t *testing.T) { - loadGenerationResponse := make(chan LoadGenerationResponse, 1) + loadGenerationResponse := make(chan report.LoadGenerationResponse, 1) defer close(loadGenerationResponse) var buffer bytes.Buffer @@ -44,7 +46,7 @@ func TestWritesPayloadByWorker(t *testing.T) { func TestWritesMultiplePayloadsByWorker(t *testing.T) { totalRequests := uint(5) - loadGenerationResponse := make(chan LoadGenerationResponse, totalRequests) + loadGenerationResponse := make(chan report.LoadGenerationResponse, totalRequests) defer close(loadGenerationResponse) var buffer bytes.Buffer @@ -71,7 +73,7 @@ func TestWritesMultiplePayloadsByWorker(t *testing.T) { func TestWritesMultiplePayloadsByWorkerWithThrottle(t *testing.T) { totalRequests := uint(5) - loadGenerationResponse := make(chan LoadGenerationResponse, totalRequests) + loadGenerationResponse := make(chan report.LoadGenerationResponse, totalRequests) defer close(loadGenerationResponse) var buffer bytes.Buffer