Skip to content

Commit

Permalink
fix: polycli loadtest does not stop immediately when one try to int…
Browse files Browse the repository at this point in the history
…errupt it with `Ctrl+C` (#147)

* fix: stop load test during execution

* fix: stop load test when producing summary

* chore: document and few nits

* chore: lint

* chore: nit

* chore: nit 2

* chore: initialize errors
  • Loading branch information
leovct authored Nov 3, 2023
1 parent eb26a3b commit fc6ff2c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
32 changes: 23 additions & 9 deletions cmd/loadtest/loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,13 @@ func completeLoadTest(ctx context.Context, c *ethclient.Client, rpc *ethrpc.Clie
return nil
}

// runLoadTest initiates and runs the entire load test process, including initialization,
// the main load test loop, and the completion steps. It takes a context for cancellation signals.
// The function returns an error if there are issues during the load test process.
func runLoadTest(ctx context.Context) error {
log.Info().Msg("Starting Load Test")

// Configure the overall time limit for the load test.
timeLimit := *inputLoadTestParams.TimeLimit
var overallTimer *time.Timer
if timeLimit > 0 {
Expand All @@ -347,32 +351,48 @@ func runLoadTest(ctx context.Context) error {
overallTimer = new(time.Timer)
}

// Dial the Ethereum RPC server.
rpc, err := ethrpc.DialContext(ctx, *inputLoadTestParams.RPCUrl)
if err != nil {
log.Error().Err(err).Msg("Unable to dial rpc")
return err
}
defer rpc.Close()
rpc.SetHeader("Accept-Encoding", "identity")
ec := ethclient.NewClient(rpc)

// Define the main loop function.
// Make sure to define any logic associated to the load test (initialization, main load test loop
// or completion steps) in this function in order to handle cancellation signals properly.
loopFunc := func() error {
err = initializeLoadTestParams(ctx, ec)
if err != nil {
if err = initializeLoadTestParams(ctx, ec); err != nil {
log.Error().Err(err).Msg("Error initializing load test parameters")
return err
}

return mainLoop(ctx, ec, rpc)
if err = mainLoop(ctx, ec, rpc); err != nil {
log.Error().Err(err).Msg("Error during the main load test loop")
return err
}

if err = completeLoadTest(ctx, ec, rpc); err != nil {
log.Error().Err(err).Msg("Encountered error while wrapping up loadtest")
}
return nil
}

// Set up signal handling for interrupts.
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)

// Initialize channels for handling errors and running the main loop.
loadTestResults = make([]loadTestSample, 0)
errCh := make(chan error)
go func() {
errCh <- loopFunc()
}()

// Wait for the load test to complete, either due to time limit, interrupt signal, or completion.
select {
case <-overallTimer.C:
log.Info().Msg("Time's up")
Expand All @@ -383,12 +403,6 @@ func runLoadTest(ctx context.Context) error {
log.Fatal().Err(err).Msg("Received critical error while running load test")
}
}

err = completeLoadTest(ctx, ec, rpc)
if err != nil {
log.Error().Err(err).Msg("Encountered error while wrapping up loadtest")
}

log.Info().Msg("Finished")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
cloud.google.com/go/datastore v1.14.0
github.com/btcsuite/btcutil v1.0.2
github.com/cenkalti/backoff/v4 v4.2.1
github.com/ethereum/go-ethereum v1.13.2
github.com/gizak/termui/v3 v3.1.0
github.com/google/gofuzz v1.2.0
Expand Down Expand Up @@ -45,7 +46,6 @@ require (
github.com/bits-and-blooms/bitset v1.7.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.8.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVa
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down

0 comments on commit fc6ff2c

Please sign in to comment.