Skip to content
Snippets Groups Projects
Commit 91f3dd0f authored by RUANO RINCON Santiago's avatar RUANO RINCON Santiago
Browse files

Write spamming time inforation to disk

parent 170dd15e
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ package spammer
import (
"math/rand"
"os"
"strconv"
"sync/atomic"
"time"
......@@ -52,9 +53,17 @@ func (spammer *Spammer) run(rate int, timeUnit time.Duration, processID int64) {
log.Fatal(err)
}
start := time.Now()
_, err = fileIntervals.WriteString(
"startTimestamp," +
"payloadTime," +
"endTimestamp," +
"remainingTime," +
"msgInterval\n")
for {
start := time.Now()
startNano := time.Now().UnixNano()
if atomic.LoadInt64(&spammer.processID) != processID {
return
}
......@@ -65,19 +74,29 @@ func (spammer *Spammer) run(rate int, timeUnit time.Duration, processID int64) {
// can't issue msg because node not in sync
return
}
end := time.Now().UnixNano()
currentInterval := time.Since(start)
remainingTime := time.Duration(0)
// emit messages by intervals whose random duration is exponentially distributed
msgInterval := time.Duration(float64(timeUnit.Nanoseconds()) * rand.ExpFloat64() / float64(rate))
if currentInterval < msgInterval {
remainingTime = msgInterval - currentInterval
//there is still time, sleep until msgInterval
time.Sleep(msgInterval - currentInterval)
fileIntervals.WriteString((msgInterval - currentInterval).String() + "\n")
} else {
fileIntervals.WriteString("0\n")
}
// when currentInterval > msgInterval, the node can't issue msgs as fast as requested, will do as fast as it can
start = time.Now()
_, err = fileIntervals.WriteString(
strconv.FormatInt(startNano, 10) + "," + // start timestamp
currentInterval.String() + "," + // time needed to send the payload
strconv.FormatInt(end, 10) + "," + // time after sending the payload
remainingTime.String() + "," + // remaining time
msgInterval.String() + "\n") // interval between two spam messages
if err != nil {
log.Fatalf("Error while writing to a file. Err: %s", err.Error())
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment