Skip to content
Snippets Groups Projects
Unverified Commit 250f769a authored by Santiago Ruano Rincón's avatar Santiago Ruano Rincón
Browse files

Send spam messages in intervals of random length (rand.ExpFloat64)

parent e7a87fb1
No related branches found
No related tags found
No related merge requests found
package spammer
import (
"math/rand"
"sync/atomic"
"time"
......@@ -37,8 +38,6 @@ func (spammer *Spammer) Shutdown() {
}
func (spammer *Spammer) run(rate int, timeUnit time.Duration, processID int64) {
// emit messages every msgInterval interval
msgInterval := time.Duration(timeUnit.Nanoseconds() / int64(rate))
start := time.Now()
for {
......@@ -54,6 +53,9 @@ func (spammer *Spammer) run(rate int, timeUnit time.Duration, processID int64) {
}
currentInterval := time.Since(start)
// emit messages by intervals whose random duration is exponentially distributed
msgInterval := time.Duration(float64(timeUnit.Nanoseconds()) * rand.ExpFloat64() / float64(rate))
if currentInterval < msgInterval {
//there is still time, sleep until msgInterval
time.Sleep(msgInterval - currentInterval)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment