/
interarrival.go
63 lines (55 loc) · 1.53 KB
/
interarrival.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package loadspec
import (
"fmt"
"math"
"strconv"
"strings"
)
const (
loadDefSep = ":"
constLoadDef = "const"
poissonLoadDef = "poisson"
)
type interArrival interface {
Next() int64
}
func newInterArrival(def string) (interArrival, error) {
p := strings.Split(def, loadDefSep)
if len(p) < 1 {
return nil, fmt.Errorf("invalid inter arrival definition:%s", def)
}
switch p[0] {
case constLoadDef:
qps, err := strconv.ParseFloat(p[1], 64)
if err != nil {
return nil, fmt.Errorf("invalid inter arrival definition:%q", err)
}
return &Const{qps}, nil
case poissonLoadDef:
lambda, err := strconv.ParseFloat(p[1], 64)
if err != nil {
return nil, fmt.Errorf("invalid inter arrival definition:%q", err)
}
return &Poisson{lambda}, nil
default:
return nil, fmt.Errorf("invalid load type:%s", p[0])
}
}
// Generates a stream of inter-arrival times following a constant number of queries per second.
type Const struct {
qps float64
}
func (g *Const) Next() int64 {
return int64(float64(1e9) / g.qps)
}
// Generates an sequence of inter-arrival times following the Poisson distribution.
type Poisson struct {
// The rate parameter λ is a measure of frequency: the average rate of events (in this case, messages sent)
// per unit of time (in this case, seconds).
lambda float64
}
func (p *Poisson) Next() int64 {
// NOTE: Implementation follows:
// http://preshing.com/20111007/how-to-generate-random-timings-for-a-poisson-process/
return int64(-math.Log(1.0-randGen.Float64()) / float64(p.lambda) * float64(1e9))
}