|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + shellwords "github.com/mattn/go-shellwords" |
| 5 | + "log" |
| 6 | + "math" |
| 7 | + "math/rand" |
| 8 | + "strconv" |
| 9 | +) |
| 10 | + |
| 11 | +type arrayStringParameters []string |
| 12 | + |
| 13 | +func (i *arrayStringParameters) String() string { |
| 14 | + return "my string representation" |
| 15 | +} |
| 16 | + |
| 17 | +func (i *arrayStringParameters) Set(value string) error { |
| 18 | + *i = append(*i, value) |
| 19 | + return nil |
| 20 | +} |
| 21 | + |
| 22 | +func sample(cdf []float32) int { |
| 23 | + r := rand.Float32() |
| 24 | + bucket := 0 |
| 25 | + for r > cdf[bucket] { |
| 26 | + bucket++ |
| 27 | + } |
| 28 | + return bucket |
| 29 | +} |
| 30 | + |
| 31 | +func prepareCommandsDistribution(queries arrayStringParameters, cmds [][]string, cmdRates []float64) (int, []float32) { |
| 32 | + var totalDifferentCommands = len(cmds) |
| 33 | + var totalRateSum = 0.0 |
| 34 | + var err error |
| 35 | + for i, rawCmdString := range queries { |
| 36 | + cmds[i], _ = shellwords.Parse(rawCmdString) |
| 37 | + if i >= len(benchmarkCommandsRatios) { |
| 38 | + cmdRates[i] = 1 |
| 39 | + |
| 40 | + } else { |
| 41 | + cmdRates[i], err = strconv.ParseFloat(benchmarkCommandsRatios[i], 64) |
| 42 | + if err != nil { |
| 43 | + log.Fatalf("Error while converting query-rate param %s: %v", benchmarkCommandsRatios[i], err) |
| 44 | + } |
| 45 | + } |
| 46 | + totalRateSum += cmdRates[i] |
| 47 | + } |
| 48 | + // probability density function |
| 49 | + if math.Abs(1.0-totalRateSum) > 0.01 { |
| 50 | + log.Fatalf("Total ratio should be 1.0 ( currently is %f )", totalRateSum) |
| 51 | + } |
| 52 | + // probability density function |
| 53 | + if len(benchmarkCommandsRatios) > 0 && (len(benchmarkCommandsRatios) != (len(benchmarkCommands))) { |
| 54 | + 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)) |
| 55 | + } |
| 56 | + pdf := make([]float32, len(queries)) |
| 57 | + cdf := make([]float32, len(queries)) |
| 58 | + for i := 0; i < len(cmdRates); i++ { |
| 59 | + pdf[i] = float32(cmdRates[i]) |
| 60 | + cdf[i] = 0 |
| 61 | + } |
| 62 | + // get cdf |
| 63 | + cdf[0] = pdf[0] |
| 64 | + for i := 1; i < len(cmdRates); i++ { |
| 65 | + cdf[i] = cdf[i-1] + pdf[i] |
| 66 | + } |
| 67 | + return totalDifferentCommands, cdf |
| 68 | +} |
0 commit comments