Skip to content

Instantly share code, notes, and snippets.

@keks
Created November 27, 2017 13:12
Show Gist options
  • Save keks/a3bd10097c9c1d20ef5a2de3b5f794e3 to your computer and use it in GitHub Desktop.
Save keks/a3bd10097c9c1d20ef5a2de3b5f794e3 to your computer and use it in GitHub Desktop.
simulator for finding usable parameters
package main
import (
"fmt"
"github.com/agoussia/godes"
"os"
"strconv"
)
// the arrival and service are two random number generators for the uniform distribution
var arrival *godes.UniformDistr = godes.NewUniformDistr(true)
type Message struct {
*godes.Runner
number int
}
func (m *Message) Run() { // Any runner should have the Run method
}
type params struct {
msgsPerSec float64
timeout float64
}
func main() {
// let's pretend these are microseconds and we run 1sec
var shutdown_time float64 = 1000000
if len(os.Args) != 3 {
fmt.Printf("Usage: %s <msgs per sec> <validator timeout in ms>\n", os.Args[0])
return
}
msgsPerSec, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println("error:", err)
}
timeout, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("error:", err)
}
par := params{float64(msgsPerSec), float64(timeout) * 1000}
godes.Run()
var timeouts map[float64]struct{}
var maxConcurrent int
hist := make(map[int]int)
for {
if godes.GetSystemTime() >= shutdown_time {
break
}
// time until next message arrival
// between 0us and 200us, so the mean is 100us, e.g. 10000k msgs/s
t := arrival.Get(0, 2*1000000/par.msgsPerSec)
newTimeouts := make(map[float64]struct{})
for k := range timeouts {
if k > t {
newTimeouts[k-t] = struct{}{}
}
}
timeouts = newTimeouts
// add new validator's timeout
timeouts[par.timeout] = struct{}{}
//the function acivates the Runner
godes.AddRunner(&Message{&godes.Runner{}, len(timeouts)})
hist[(len(timeouts)/5)*5]++
if len(timeouts) > maxConcurrent {
maxConcurrent = len(timeouts)
}
//this advance the system time
godes.Advance(t)
}
// waits for all the runners to finish the Run()
godes.WaitUntilDone()
fmt.Println("max:", maxConcurrent)
fmt.Println("hist:", hist)
}

The hist map is a histogram, where entry i contains the number of steps in which there were between i and i+5 validators running concurrently. max is the maximum number of concurrently running validators.

keks@keks-x220 /tmp/sim$ go build
keks@keks-x220 /tmp/sim$ ./sim
Usage: ./sim <msgs per sec> <validator timeout in ms>
keks@keks-x220 /tmp/sim$ ./sim 10000 10
max: 124
hist: map[40:5 70:5 75:5 100:2740 105:1917 120:46 0:4 25:5 80:29 95:2701 15:5 20:5 30:5 50:5 90:1149 85:317 5:5 10:5 35:5 45:5 55:5 60:5 65:5 110:853 115:218]
keks@keks-x220 /tmp/sim$ ./sim 5000 20
max: 122
hist: map[100:1502 110:364 120:16 10:5 45:5 20:5 50:5 30:5 40:5 55:5 60:5 70:5 85:62 0:4 25:5 90:516 105:988 35:5 65:5 75:5 80:27 95:1370 115:120 5:5 15:5]
keks@keks-x220 /tmp/sim$ ./sim 500 200
max: 109
hist: map[20:5 60:5 90:30 5:5 25:5 50:5 65:5 75:5 95:168 15:5 35:5 70:5 85:5 100:177 105:31 10:5 30:5 40:5 45:5 55:5 80:5 0:4]
keks@keks-x220 /tmp/sim$ ./sim 100 333
max: 37
hist: map[10:5 15:5 20:5 25:5 30:49 35:22 0:4 5:5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment