Skip to content

Commit

Permalink
Sarthak | Moves LoadGenerationResponse in report package
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Aug 16, 2023
1 parent 121264e commit a7eb4bf
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 37 deletions.
6 changes: 2 additions & 4 deletions report/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package report

import (
"time"

"blast/workers"
)

type Report struct {
Expand Down Expand Up @@ -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{
Expand Down
32 changes: 15 additions & 17 deletions report/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
}

Expand All @@ -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,
}

Expand Down
6 changes: 6 additions & 0 deletions report/response_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"time"
)

type LoadGenerationResponse struct {
Err error
PayloadLengthBytes int64
LoadGenerationTime time.Time
}

type SubjectServerResponse struct {
Err error
ResponseTime time.Time
Expand Down
12 changes: 4 additions & 8 deletions workers/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package workers

import "time"
import (
"blast/report"
)

type GroupOptions struct {
concurrency uint
Expand All @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion workers/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io"
"sync"
"time"

"blast/report"
)

type Worker struct {
Expand Down Expand Up @@ -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(),
Expand Down
10 changes: 6 additions & 4 deletions workers/worker_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package workers
import (
"net"
"sync"

"blast/report"
)

type WorkerGroup struct {
Expand All @@ -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))

Expand Down Expand Up @@ -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)
}
8 changes: 5 additions & 3 deletions workers/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"blast/report"
)

type BytesWriteCloser struct {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a7eb4bf

Please sign in to comment.