Skip to content

Commit

Permalink
Write throughput at fixed points
Browse files Browse the repository at this point in the history
  • Loading branch information
aminst committed Feb 15, 2024
1 parent eef7505 commit ab4335d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
8 changes: 2 additions & 6 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ func main() {
defer cancel()

go c.SendRequestsForever(ctx, readResponseChannel, writeResponseChannel)
startTime := time.Now()
readOperations, writeOperations := c.GetResponsesForever(ctx, readResponseChannel, writeResponseChannel)
elapsed := time.Since(startTime)
throughput := float64(readOperations+writeOperations) / elapsed.Seconds()
averageLatency := float64(elapsed.Milliseconds()) / float64((readOperations + writeOperations))
err = client.WriteOutputToFile(*outputFilePath, throughput, averageLatency)
responseCounts := c.GetResponsesForever(ctx, readResponseChannel, writeResponseChannel)
err = client.WriteOutputToFile(*outputFilePath, responseCounts)
if err != nil {
log.Fatal().Msgf("Failed to write output to file; %v", err)
}
Expand Down
18 changes: 15 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,25 @@ func (c *client) SendRequestsForever(ctx context.Context, readResponseChannel ch
}
}

type ResponseCount struct {
readOperations int
writeOperations int
}

// getResponsesForever cancels remaining operations and returns when the context is cancelled
func (c *client) GetResponsesForever(ctx context.Context, readResponseChannel chan ReadResponse, writeResponseChannel chan WriteResponse) (readOperations int, writeOperations int) {
readOperations, writeOperations = 0, 0
// returns the number of read and write operations over fixed intervals in the duration
func (c *client) GetResponsesForever(ctx context.Context, readResponseChannel chan ReadResponse, writeResponseChannel chan WriteResponse) []ResponseCount {
readOperations, writeOperations := 0, 0
var responseCounts []ResponseCount
timout := time.After(1 * time.Second)
for {
select {
case <-ctx.Done():
return readOperations, writeOperations
return responseCounts
case <-timout:
responseCounts = append(responseCounts, ResponseCount{readOperations, writeOperations})
readOperations, writeOperations = 0, 0
timout = time.After(1 * time.Second)
default:
}
select {
Expand Down
8 changes: 5 additions & 3 deletions pkg/client/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"os"
)

func WriteOutputToFile(outputFilePath string, throughput float64, latency float64) error {
func WriteOutputToFile(outputFilePath string, responseCount []ResponseCount) error {
file, err := os.Create(outputFilePath)
if err != nil {
return err
}
defer file.Close()
file.WriteString(fmt.Sprintf("Throughput: %f\n", throughput))
file.WriteString(fmt.Sprintf("Average latency in ms: %f\n", latency))
for _, count := range responseCount {
throughput := float64(count.readOperations + count.writeOperations)
file.WriteString(fmt.Sprintf("Throughput: %f\n", throughput))
}
return nil
}

0 comments on commit ab4335d

Please sign in to comment.