Skip to content
Snippets Groups Projects
Unverified Commit 32ed993a authored by Angelo Capossele's avatar Angelo Capossele Committed by GitHub
Browse files

Add time unit to spammer and bootstrap (#568)

* :clock3: Add time unit to spammer and bootstrap

* :rotating_light: Fix linter warning
parent bf533b56
No related branches found
No related tags found
No related merge requests found
......@@ -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()
......
......@@ -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 {
......
......@@ -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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment