-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.go
70 lines (64 loc) · 1.91 KB
/
cli.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
64
65
66
67
68
69
70
package main
import (
shellwords "github.com/mattn/go-shellwords"
"log"
"math"
"math/rand"
"strconv"
)
type arrayStringParameters []string
func (i *arrayStringParameters) String() string {
return "my string representation"
}
func (i *arrayStringParameters) Set(value string) error {
*i = append(*i, value)
return nil
}
func sample(cdf []float32) int {
r := rand.Float32()
bucket := 0
for r > cdf[bucket] {
bucket++
}
return bucket
}
func prepareCommandsDistribution(queries arrayStringParameters, cmds [][]string, cmdRates []float64) (totalDifferentCommands int, cdf []float32) {
totalDifferentCommands = len(cmds)
var totalRateSum = 0.0
var err error
if len(benchmarkCommandsRatios) == 0 {
for i, _ := range queries {
cmdRates[i] = 1.0 / float64(len(queries))
}
}
for i, rawCmdString := range queries {
cmds[i], _ = shellwords.Parse(rawCmdString)
if i < len(benchmarkCommandsRatios) {
cmdRates[i], err = strconv.ParseFloat(benchmarkCommandsRatios[i], 64)
if err != nil {
log.Fatalf("Error while converting query-rate param %s: %v", benchmarkCommandsRatios[i], err)
}
}
totalRateSum += cmdRates[i]
}
// probability density function
if math.Abs(1.0-totalRateSum) > 0.01 {
log.Fatalf("Total ratio should be 1.0 ( currently is %f )", totalRateSum)
}
// probability density function
if len(benchmarkCommandsRatios) > 0 && (len(benchmarkCommandsRatios) != (len(benchmarkCommands))) {
log.Fatalf("When specifiying -cmd-ratio parameter, you need to have the same number of -cmd and -cmd-ratio parameters. Number of time -cmd ( %d ) != Number of times -cmd-ratio ( %d )", len(benchmarkCommands), len(benchmarkCommandsRatios))
}
pdf := make([]float32, len(queries))
cdf = make([]float32, len(queries))
for i := 0; i < len(cmdRates); i++ {
pdf[i] = float32(cmdRates[i])
cdf[i] = 0
}
// get cdf
cdf[0] = pdf[0]
for i := 1; i < len(cmdRates); i++ {
cdf[i] = cdf[i-1] + pdf[i]
}
return
}