diff --git a/packages/binary/spammer/spammer.go b/packages/binary/spammer/spammer.go index df8775505c4201ea4d0a3cad917bf9e54261c5ca..5758f898fd70c5f8e72b06dab1ad02d904270f34 100644 --- a/packages/binary/spammer/spammer.go +++ b/packages/binary/spammer/spammer.go @@ -29,9 +29,9 @@ func New(issuePayloadFunc IssuePayloadFunc) *Spammer { } } -// Start starts the spammer to spam with the given messages per second. -func (spammer *Spammer) Start(mps int) { - go spammer.run(mps, atomic.AddInt64(&spammer.processId, 1)) +// Start starts the spammer to spam with the given messages per time unit. +func (spammer *Spammer) Start(rate int, timeUnit time.Duration) { + go spammer.run(rate, timeUnit, atomic.AddInt64(&spammer.processId, 1)) } // Shutdown shuts down the spammer. @@ -39,12 +39,12 @@ func (spammer *Spammer) Shutdown() { atomic.AddInt64(&spammer.processId, 1) } -func (spammer *Spammer) run(mps int, processId int64) { +func (spammer *Spammer) run(rate int, timeUnit time.Duration, processID int64) { currentSentCounter := 0 start := time.Now() for { - if atomic.LoadInt64(&spammer.processId) != processId { + if atomic.LoadInt64(&spammer.processId) != processID { return } @@ -54,10 +54,10 @@ func (spammer *Spammer) run(mps int, processId int64) { currentSentCounter++ // rate limit to the specified MPS - if currentSentCounter >= mps { + if currentSentCounter >= rate { duration := time.Since(start) - if duration < time.Second { - time.Sleep(time.Second - duration) + if duration < timeUnit { + time.Sleep(timeUnit - duration) } start = time.Now() diff --git a/plugins/bootstrap/plugin.go b/plugins/bootstrap/plugin.go index c16ffe224b4f1c55ac89af0e279b867aef3ab12b..1fc856b4087d2656b58271148b0cd8ec1edb9228 100644 --- a/plugins/bootstrap/plugin.go +++ b/plugins/bootstrap/plugin.go @@ -21,8 +21,10 @@ const ( // CfgBootstrapInitialIssuanceTimePeriodSec defines the initial time period of how long the node should be // issuing messages when started in bootstrapping mode. If the value is set to -1, the issuance is continuous. CfgBootstrapInitialIssuanceTimePeriodSec = "bootstrap.initialIssuance.timePeriodSec" - // the messages per second to issue when in bootstrapping mode. - initialIssuanceMPS = 1 + // CfgBootstrapTimeUnit defines the time unit (in seconds) of the issuance rate (e.g., 3 messages per 12 seconds). + CfgBootstrapTimeUnit = "bootstrap.timeUnit" + // the messages per period to issue when in bootstrapping mode. + initialIssuanceRate = 1 // the value which determines a continuous issuance of messages from the bootstrap plugin. continuousIssuance = -1 ) @@ -30,6 +32,7 @@ const ( func init() { flag.Int(CfgBootstrapInitialIssuanceTimePeriodSec, -1, "the initial time period of how long the node should be issuing messages when started in bootstrapping mode. "+ "if the value is set to -1, the issuance is continuous.") + flag.Int(CfgBootstrapTimeUnit, 5, "the time unit (in seconds) of the issuance rate (e.g., 1 messages per 5 seconds).") } var ( @@ -58,11 +61,15 @@ func run(_ *node.Plugin) { messageSpammer := spammer.New(issuer.IssuePayload) issuancePeriodSec := config.Node().GetInt(CfgBootstrapInitialIssuanceTimePeriodSec) + timeUnit := config.Node().GetInt(CfgBootstrapTimeUnit) + if timeUnit <= 0 { + log.Panicf("Invalid Bootsrap time unit: %d seconds", timeUnit) + } issuancePeriod := time.Duration(issuancePeriodSec) * time.Second // issue messages on top of the genesis if err := daemon.BackgroundWorker("Bootstrapping-Issuer", func(shutdownSignal <-chan struct{}) { - messageSpammer.Start(initialIssuanceMPS) + messageSpammer.Start(initialIssuanceRate, time.Duration(timeUnit)*time.Second) defer messageSpammer.Shutdown() // don't stop issuing messages if in continuous mode if issuancePeriodSec == continuousIssuance { diff --git a/plugins/webapi/spammer/webapi.go b/plugins/webapi/spammer/webapi.go index 494cd40456f38e1afc2ea16b5d2391dfab82fcee..bc92a6053dc2e863ac8cdc936c8779f9cd9a71b8 100644 --- a/plugins/webapi/spammer/webapi.go +++ b/plugins/webapi/spammer/webapi.go @@ -2,6 +2,7 @@ package spammer import ( "net/http" + "time" "github.com/labstack/echo" ) @@ -19,7 +20,7 @@ func handleRequest(c echo.Context) error { } messageSpammer.Shutdown() - messageSpammer.Start(request.MPS) + messageSpammer.Start(request.MPS, time.Second) return c.JSON(http.StatusOK, Response{Message: "started spamming messages"}) case "stop": messageSpammer.Shutdown()