From 514d3757e54f35edbea1f5f75b4870ea3d88608c Mon Sep 17 00:00:00 2001 From: Joao Ferreira Date: Thu, 7 Nov 2024 13:47:11 +0000 Subject: [PATCH] cadency --- cmd/tradeclient/loadtest/loadtest.go | 9 +++-- cmd/tradeclient/tradeclient.go | 52 +++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/cmd/tradeclient/loadtest/loadtest.go b/cmd/tradeclient/loadtest/loadtest.go index 5807e40..597fe38 100644 --- a/cmd/tradeclient/loadtest/loadtest.go +++ b/cmd/tradeclient/loadtest/loadtest.go @@ -16,11 +16,11 @@ type LoadTestConfig struct { TargetCompID string } -// RunLoadTest sends orders based on the provided configuration. +// sends orders based on the provided configuration. func RunLoadTest(cfg LoadTestConfig) { var wg sync.WaitGroup - // Launch goroutines to send orders at the specified rate + // send orders at the specified rate for i := 0; i < cfg.TotalOrders; i++ { wg.Add(1) go func(orderID int) { @@ -34,4 +34,9 @@ func RunLoadTest(cfg LoadTestConfig) { // Delay to maintain order rate time.Sleep(time.Second / time.Duration(cfg.OrdersPerSecond)) } + + // Wait for all goroutines to complete + wg.Wait() + + fmt.Println("Load test finished, all orders have been processed.") } diff --git a/cmd/tradeclient/tradeclient.go b/cmd/tradeclient/tradeclient.go index 5b8aa09..033956a 100644 --- a/cmd/tradeclient/tradeclient.go +++ b/cmd/tradeclient/tradeclient.go @@ -25,6 +25,7 @@ import ( "os" "path" "strconv" + "time" "github.com/quickfixgo/examples/cmd/readmetrics" "github.com/quickfixgo/examples/cmd/tradeclient/internal" @@ -233,10 +234,8 @@ Loop: err = internal.QueryMarketDataRequest("CUST2_Marketdata", "ANCHORAGE") case "4": - var ordersPerSecond int - var totalOrders int - // Prompt the user for orders per second + var ordersPerSecond int fmt.Print("Enter orders per second: ") _, err := fmt.Scanf("%d", &ordersPerSecond) if err != nil { @@ -245,6 +244,7 @@ Loop: } // Prompt the user for total number of orders + var totalOrders int fmt.Print("Enter total number of orders: ") _, err = fmt.Scanf("%d", &totalOrders) if err != nil { @@ -252,6 +252,15 @@ Loop: break } + // Ask if the user wants to set a cadency + var setCadency string + fmt.Print("Do you want to set a cadency for the load test? (yes/no): ") + _, err = fmt.Scanf("%s", &setCadency) + if err != nil { + utils.PrintBad("Invalid input for cadency choice") + break + } + // Create load test configuration loadTestConfig := loadtest.LoadTestConfig{ OrdersPerSecond: ordersPerSecond, @@ -260,8 +269,41 @@ Loop: TargetCompID: "ANCHORAGE", } - // Run the load test - loadtest.RunLoadTest(loadTestConfig) + if setCadency == "yes" { + // Prompt the user for cadency + var cadencyInput string + fmt.Print("Enter the cadency (e.g., '10m' for 10 minutes, '1d' for every day, '3d' for every 3 days): ") + _, err = fmt.Scanf("%s", &cadencyInput) + if err != nil { + utils.PrintBad("Invalid input for cadency") + break + } + + // Parse the cadency input into a time.Duration + interval, err := time.ParseDuration(cadencyInput) + if err != nil { + utils.PrintBad("Invalid cadency format") + break + } + + // Run the load test at the specified interval + fmt.Printf("Starting load test every %v...\n", interval) + + // Loop to run the load test at the specified interval + for { + // Run the load test + loadtest.RunLoadTest(loadTestConfig) + + // Wait for the next interval + fmt.Printf("Waiting for next load test after %v...\n", interval) + time.Sleep(interval) + } + } else if setCadency == "no" { + // Run once without cadency + loadtest.RunLoadTest(loadTestConfig) + } else { + utils.PrintBad("Invalid input for cadency choice") + } case "5": // Call readmetrics after the load test