Skip to content

Commit

Permalink
Sarthak | Makes PayloadGenerator an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Jan 19, 2024
1 parent b70df98 commit 664acd1
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 44 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func setUpBlast(url string) blast.Blast {
*concurrency,
*connections,
*numberOfRequests,
payloadGenerator.Generate,
payloadGenerator,
url,
*requestsPerSecond,
*connectTimeout,
Expand Down
5 changes: 5 additions & 0 deletions payload/payload_generator.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package payload

// PayloadGenerator defines a function for generating the request payload
type PayloadGenerator interface {
Generate(requestId uint) []byte
}

// ConstantPayloadGenerator provides a constant payload to all the workers for sending the payload.
type ConstantPayloadGenerator struct {
payload []byte
Expand Down
14 changes: 7 additions & 7 deletions tests/blast_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestBlastWithLoadGeneration(t *testing.T) {
groupOptions := workers.NewGroupOptions(
concurrency,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:10001",
)
buffer := &bytes.Buffer{}
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestBlastWithLoadGenerationForMaximumDuration(t *testing.T) {
concurrency,
10,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:10002",
)
buffer := &bytes.Buffer{}
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestBlastWithLoadGenerationAndResponseReading(t *testing.T) {
groupOptions := workers.NewGroupOptions(
concurrency,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:10003",
)
responseOptions := blast.ResponseOptions{
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestBlastWithLoadGenerationAndResponseReadingForMaximumDuration(t *testing.
concurrency,
10,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:10004",
)
responseOptions := blast.ResponseOptions{
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestBlastWithResponseReadingGivenTheTargetServerFailsInSendingResponses(t *
groupOptions := workers.NewGroupOptions(
concurrency,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:10005",
)
responseOptions := blast.ResponseOptions{
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestBlastWithLoadGenerationAndAStopSignal(t *testing.T) {
groupOptions := workers.NewGroupOptions(
concurrency,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:10006",
)
buffer := &bytes.Buffer{}
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestBlastWithLoadGenerationAndResponseReadingWithStopSignal(t *testing.T) {
concurrency,
10,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:10007",
)
responseOptions := blast.ResponseOptions{
Expand Down
12 changes: 6 additions & 6 deletions tests/worker_group_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestSendsRequestsWithSingleConnection(t *testing.T) {
workerGroup := workers.NewWorkerGroup(workers.NewGroupOptions(
concurrency,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:8080",
))
loadGenerationResponseChannel := workerGroup.Run()
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestSendsRequestsWithMultipleConnections(t *testing.T) {
concurrency,
connections,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:8081",
))
loadGenerationResponseChannel := workerGroup.Run()
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestSendsARequestAndReadsResponseWithSingleConnection(t *testing.T) {
workers.NewGroupOptions(
concurrency,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:8082",
), report.NewResponseReader(responseSizeBytes, 100*time.Millisecond, responseChannel),
)
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestSendsAdditionalRequestsThanConfiguredWithSingleConnection(t *testing.T)
workerGroup := workers.NewWorkerGroup(workers.NewGroupOptions(
concurrency,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:8083",
))
loadGenerationResponseChannel := workerGroup.Run()
Expand All @@ -155,7 +155,7 @@ func TestSendsRequestsOnANonRunningServer(t *testing.T) {
workerGroup := workers.NewWorkerGroup(workers.NewGroupOptions(
concurrency,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:8090",
))
loadGenerationResponseChannel := workerGroup.Run()
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestSendsRequestsWithDialTimeout(t *testing.T) {
concurrency,
1,
totalRequests,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")).Generate,
payload.NewConstantPayloadGenerator([]byte("HelloWorld")),
"localhost:8098",
0.0,
1*time.Nanosecond,
Expand Down
44 changes: 21 additions & 23 deletions workers/options.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
package workers

import (
"blast/payload"
"time"

"blast/report"
)

const dialTimeout = 3 * time.Second

// PayloadGenerator defines a function for generating the request payload
type PayloadGenerator = func(requestId uint) []byte

// GroupOptions defines the configuration options for the WorkerGroup.
type GroupOptions struct {
concurrency uint
connections uint
totalRequests uint
payloadGenerationFn PayloadGenerator
targetAddress string
requestsPerSecond float64
dialTimeout time.Duration
concurrency uint
connections uint
totalRequests uint
payloadGenerator payload.PayloadGenerator
targetAddress string
requestsPerSecond float64
dialTimeout time.Duration
}

// WorkerOptions defines the configuration options for a running Worker.
type WorkerOptions struct {
totalRequests uint
payloadGenerationFn PayloadGenerator
payloadGenerator payload.PayloadGenerator
targetAddress string
requestsPerSecond float64
stopChannel chan struct{}
Expand All @@ -36,14 +34,14 @@ type WorkerOptions struct {
func NewGroupOptions(
concurrency uint,
totalRequests uint,
payloadGenerationFn PayloadGenerator,
payloadGenerator payload.PayloadGenerator,
targetAddress string,
) GroupOptions {
return NewGroupOptionsFullyLoaded(
concurrency,
1,
totalRequests,
payloadGenerationFn,
payloadGenerator,
targetAddress,
0.0,
dialTimeout,
Expand All @@ -55,14 +53,14 @@ func NewGroupOptionsWithConnections(
concurrency uint,
connections uint,
totalRequests uint,
payloadGenerationFn PayloadGenerator,
payloadGenerator payload.PayloadGenerator,
targetAddress string,
) GroupOptions {
return NewGroupOptionsFullyLoaded(
concurrency,
connections,
totalRequests,
payloadGenerationFn,
payloadGenerator,
targetAddress,
0.0,
dialTimeout,
Expand All @@ -74,19 +72,19 @@ func NewGroupOptionsFullyLoaded(
concurrency uint,
connections uint,
totalRequests uint,
payloadGenerationFn PayloadGenerator,
payloadGenerator payload.PayloadGenerator,
targetAddress string,
requestsPerSecond float64,
dialTimeout time.Duration,
) GroupOptions {
return GroupOptions{
concurrency: concurrency,
connections: connections,
totalRequests: totalRequests,
payloadGenerationFn: payloadGenerationFn,
targetAddress: targetAddress,
requestsPerSecond: requestsPerSecond,
dialTimeout: dialTimeout,
concurrency: concurrency,
connections: connections,
totalRequests: totalRequests,
payloadGenerator: payloadGenerator,
targetAddress: targetAddress,
requestsPerSecond: requestsPerSecond,
dialTimeout: dialTimeout,
}
}

Expand Down
2 changes: 1 addition & 1 deletion workers/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (worker Worker) sendRequest() {
_ = recover()
}()
if worker.connection != nil {
payload := worker.options.payloadGenerationFn(1) //TODO: Generate request id
payload := worker.options.payloadGenerator.Generate(1) //TODO: Generate request id
_, err := worker.connection.Write(payload)

worker.options.loadGenerationResponse <- report.LoadGenerationResponse{
Expand Down
2 changes: 1 addition & 1 deletion workers/worker_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (group *WorkerGroup) runWorker(
connectionId: connectionId,
options: WorkerOptions{
totalRequests: totalRequests / group.options.concurrency,
payloadGenerationFn: group.options.payloadGenerationFn,
payloadGenerator: group.options.payloadGenerator,
targetAddress: group.options.targetAddress,
requestsPerSecond: group.options.requestsPerSecond,
stopChannel: group.stopChannel,
Expand Down
10 changes: 5 additions & 5 deletions workers/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestWritesPayloadByWorker(t *testing.T) {
connection: &BytesWriteCloser{bufio.NewWriter(&buffer)},
options: WorkerOptions{
totalRequests: uint(1),
payloadGenerationFn: payload.NewConstantPayloadGenerator([]byte("payload")).Generate,
payloadGenerator: payload.NewConstantPayloadGenerator([]byte("payload")),
loadGenerationResponse: loadGenerationResponse,
},
}
Expand All @@ -55,7 +55,7 @@ func TestWritesMultiplePayloadsByWorker(t *testing.T) {
connection: &BytesWriteCloser{bufio.NewWriter(&buffer)},
options: WorkerOptions{
totalRequests: totalRequests,
payloadGenerationFn: payload.NewConstantPayloadGenerator([]byte("payload")).Generate,
payloadGenerator: payload.NewConstantPayloadGenerator([]byte("payload")),
loadGenerationResponse: loadGenerationResponse,
},
}
Expand All @@ -82,7 +82,7 @@ func TestWritesMultiplePayloadsByWorkerWithThrottle(t *testing.T) {
connection: &BytesWriteCloser{bufio.NewWriter(&buffer)},
options: WorkerOptions{
totalRequests: totalRequests,
payloadGenerationFn: payload.NewConstantPayloadGenerator([]byte("payload")).Generate,
payloadGenerator: payload.NewConstantPayloadGenerator([]byte("payload")),
loadGenerationResponse: loadGenerationResponse,
requestsPerSecond: float64(3),
},
Expand All @@ -109,7 +109,7 @@ func TestWritesOnANilConnectionWithConnectionId(t *testing.T) {
connection: nil,
options: WorkerOptions{
totalRequests: totalRequests,
payloadGenerationFn: payload.NewConstantPayloadGenerator([]byte("payload")).Generate,
payloadGenerator: payload.NewConstantPayloadGenerator([]byte("payload")),
loadGenerationResponse: loadGenerationResponse,
},
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestWritesPayloadByWorkerWithConnectionId(t *testing.T) {
connectionId: 10,
options: WorkerOptions{
totalRequests: uint(1),
payloadGenerationFn: payload.NewConstantPayloadGenerator([]byte("payload")).Generate,
payloadGenerator: payload.NewConstantPayloadGenerator([]byte("payload")),
loadGenerationResponse: loadGenerationResponse,
},
}
Expand Down

0 comments on commit 664acd1

Please sign in to comment.