-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
366 lines (324 loc) · 10.5 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main // import "github.com/chrstphlbr/pa"
import (
"context"
"flag"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/chrstphlbr/pa/pkg/bootstrap"
"github.com/chrstphlbr/pa/pkg/bench"
"github.com/chrstphlbr/pa/pkg/stat"
)
type cmd int
func (c cmd) String() string {
switch c {
case 0:
return "CI"
case 1:
return "Detection"
}
return "INVALID_COMMAND"
}
const (
cmdCI cmd = iota
cmdDet
)
type statisticFunc struct {
Name string
Func stat.StatisticFunc
}
const defaultRoundingPrecision = 5
func parseArgs() (c cmd, sim int, sigLevs []float64, statFunc statisticFunc, f1, f2 []string, invocationSamples int, transformer1, transformer2 *bench.NamedExecutionTransformer, outputMetric bool, printMem bool) {
sfStr := flag.String("st", "mean", "The statistic to be calculated")
s := flag.Int("bs", 10000, "Number of bootstrap simulations")
sls := flag.String("sl", "0.01", "Significance levels (multiple seperated by ',')")
is := flag.Int("is", 0, "Number of invocation samples (0 for mean across all invocations, -1 for all, > 0 for number of samples)")
m := flag.Int("m", 1, "Number of multiple files belongig to one group (test or control); e.g., 3 means 6 files in total, 3 test and 3 control")
om := flag.Bool("os", false, "Include statistic (e.g., mean) in output")
rm := flag.Bool("mem", false, "Print runtime memory to Stdout")
transformers := flag.String("tra", "id:id", "The transformer(s) applied to the execution file(s), in the form of 'transformer1:transformer2', where transformer1 is applied to the first (control) group and transformer2 is applied to the second (test) group. Transformers can be one of 'id' (identity, no transformation) or 'f0.0' ('f' for factor followed by a user-specified float64 value)")
flag.Parse()
args := flag.Args()
largs := len(args)
if largs == 1 {
// single file -> only report confidence intervals
c = cmdCI
f1 = []string{args[0]}
} else if largs == 2 {
// two files -> report performance changes
c = cmdDet
f1 = []string{args[0]}
f2 = []string{args[1]}
} else if largs < 1 {
fmt.Fprintf(os.Stdout, "Expected at least one file argument\n\n")
flag.Usage()
os.Exit(1)
} else {
c = cmdDet
// multiple files for test and control group
if largs / *m != 2 {
fmt.Fprintf(os.Stdout, "-m must be half of number of arguments\n\n")
flag.Usage()
os.Exit(1)
}
f1 = []string{}
for i := 0; i < *m; i++ {
f1 = append(f1, args[i])
}
f2 = []string{}
for i := *m; i < *m*2; i++ {
f2 = append(f2, args[i])
}
}
// significance levels
slsSplitted := strings.Split(*sls, ",")
slsFloat := make([]float64, 0, len(slsSplitted))
for _, sl := range slsSplitted {
slFloat, err := strconv.ParseFloat(sl, 64)
if err != nil {
fmt.Fprintf(os.Stdout, "Could not parse significance level '%s' into float64: %v\n\n", sl, err)
flag.Usage()
os.Exit(1)
}
slsFloat = append(slsFloat, slFloat)
}
if *is < -1 {
fmt.Fprint(os.Stdout, "Invalid number of invocation samples, must be 0 for mean across samples from iteration, > 0 for number of samples, or -1 for all\n\n")
flag.Usage()
os.Exit(1)
}
statisticFunction := *sfStr
var sf statisticFunc
switch statisticFunction {
case "mean":
sf = statisticFunc{
Name: "Mean",
Func: stat.Mean,
}
case "cov":
sf = statisticFunc{
Name: "COV",
Func: stat.COV,
}
case "median":
sf = statisticFunc{
Name: "Median",
Func: stat.Median,
}
default:
fmt.Fprintf(os.Stdout, "Unknown statistics function '%s'\n\n", statisticFunction)
flag.Usage()
os.Exit(1)
}
transformer1, transformer2, err := parseTransformers(*transformers)
if err != nil {
fmt.Fprintf(os.Stdout, "Could not parse transformers: %v\n", err)
flag.Usage()
os.Exit(1)
}
return c, *s, slsFloat, sf, f1, f2, *is, transformer1, transformer2, *om, *rm
}
func main() {
cmd, sim, sigLevels, sf, f1, f2, is, transformer1, transformer2, outputMetric, printMem := parseArgs()
maxNrWorkers := runtime.NumCPU()
var sampler bench.InvocationSampler
var samplingType string
if is == 0 {
sampler = bench.MeanInvocations
samplingType = "Mean"
} else if is == -1 {
sampler = bench.AllInvocations
samplingType = "All"
} else {
sampler = bench.SampleInvocations(is)
samplingType = fmt.Sprintf("%d invocations per iteration", is)
}
var outHeader strings.Builder
outHeader.WriteString("#Execute CIs:\n")
outHeader.WriteString(fmt.Sprintf("# cmd = %s\n", cmd))
outHeader.WriteString(fmt.Sprintf("# number of cores = %d\n", maxNrWorkers))
outHeader.WriteString(fmt.Sprintf("# bootstrap simulations = %d\n", sim))
outHeader.WriteString(fmt.Sprintf("# significance levels = %v\n", sigLevels))
outHeader.WriteString(fmt.Sprintf("# statistic = %s\n", sf.Name))
outHeader.WriteString(fmt.Sprintf("# include statistic in output = %t\n", outputMetric))
outHeader.WriteString(fmt.Sprintf("# invocation sampling = %s\n", samplingType))
outHeader.WriteString(fmt.Sprintf("# transformer 1 = %s\n", transformer1.Name))
outHeader.WriteString(fmt.Sprintf("# transformer 2 = %s\n", transformer2.Name))
outHeader.WriteString(fmt.Sprintf("# files 1 = %s\n", f1))
outHeader.WriteString(fmt.Sprintf("# files 2 = %s\n", f2))
fmt.Fprint(os.Stdout, outHeader.String())
fmt.Fprintln(os.Stdout, "")
ciFunc := bootstrap.CIFuncSetup(sim, maxNrWorkers, sf.Func, sigLevels, sampler)
ciRatioFunc := bootstrap.CIRatioFuncSetup(sim, maxNrWorkers, sf.Func, sigLevels, sampler)
var exec func()
switch cmd {
case cmdCI:
exec = func() {
ci(ciFunc, f1[0], transformer1.ExecutionTransformer, outputMetric, printMem)
}
case cmdDet:
exec = func() {
det(ciFunc, ciRatioFunc, f1, f2, transformer1.ExecutionTransformer, transformer2.ExecutionTransformer, outputMetric, printMem)
}
default:
fmt.Fprintf(os.Stdout, "Invalid command '%s' (available: 'ci' and 'det')\n\n", cmd)
flag.Usage()
os.Exit(1)
}
start := time.Now()
exec()
fmt.Fprintf(os.Stdout, "#Total execution took %v\n", time.Since(start))
}
func ci(ciFunc bootstrap.CIFunc, fp string, transformer bench.ExecutionTransformer, outputMetric, printMem bool) {
f, err := os.Open(fp)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open file '%s'\n", fp)
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := bench.FromCSV(ctx, f)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
if transformer != nil {
c = bench.TransformChan(transformer, c)
}
rc := bootstrap.CIs(c, ciFunc)
printMemStats(printMem)
for res := range rc {
if res.Err != nil {
fmt.Fprintf(os.Stderr, "Error while retrieving CI result: %v", res.Err)
continue
}
b := res.Benchmark
cis := res.CIs
for _, ci := range cis {
if outputMetric {
// include statistic/metric in output
fmt.Fprintf(os.Stdout, "%s;%s;%s;%e;%e;%e;%.2f\n", b.Name, b.FunctionParams, b.PerfParams, ci.Metric, ci.Lower, ci.Upper, ci.Level)
} else {
// only print CIs
fmt.Fprintf(os.Stdout, "%s;%s;%s;%e;%e;%.2f\n", b.Name, b.FunctionParams, b.PerfParams, ci.Lower, ci.Upper, ci.Level)
}
}
printMemStats(printMem)
}
}
func det(ciFunc bootstrap.CIFunc, ciRatioFunc bootstrap.CIRatioFunc, fp1, fp2 []string, transformer1, transformer2 bench.ExecutionTransformer, outputMetric, printMem bool) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c1, err := mergedInput(ctx, fp1)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
if transformer1 != nil {
c1 = bench.TransformChan(transformer1, c1)
}
c2, err := mergedInput(ctx, fp2)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
if transformer2 != nil {
c2 = bench.TransformChan(transformer2, c2)
}
rc := bootstrap.CIRatios(c1, c2, ciFunc, ciRatioFunc)
printMemStats(printMem)
for res := range rc {
if res.Err != nil {
fmt.Fprintf(os.Stderr, "Error while retrieving CI result: %v\n", res.Err)
continue
}
b := res.Benchmark
cirs := res.CIRatios
for _, cir := range cirs {
if outputMetric {
// include statistic/metric in output
fmt.Fprintf(
os.Stdout,
"%s;%s;%s;%e;%e;%e;%.2f;%e;%e;%e;%.2f;%e;%e;%e;%.2f\n",
b.Name, b.FunctionParams, b.PerfParams,
cir.CIA.Metric, cir.CIA.Lower, cir.CIA.Upper, cir.CIA.Level,
cir.CIB.Metric, cir.CIB.Lower, cir.CIB.Upper, cir.CIB.Level,
cir.CIRatio.Metric, cir.CIRatio.Lower, cir.CIRatio.Upper, cir.CIRatio.Level,
)
} else {
// only print CIs
fmt.Fprintf(
os.Stdout,
"%s;%s;%s;%e;%e;%.2f;%e;%e;%.2f;%e;%e;%.2f\n",
b.Name, b.FunctionParams, b.PerfParams,
cir.CIA.Lower, cir.CIA.Upper, cir.CIA.Level,
cir.CIB.Lower, cir.CIB.Upper, cir.CIB.Level,
cir.CIRatio.Lower, cir.CIRatio.Upper, cir.CIRatio.Level,
)
}
}
printMemStats(printMem)
}
}
func mergedInput(ctx context.Context, fs []string) (bench.Chan, error) {
var chans []bench.Chan
for _, fn := range fs {
f, err := os.Open(fn)
if err != nil {
return nil, fmt.Errorf("could not open file1 '%s'", fn)
}
c1, err := bench.FromCSV(ctx, f)
if err != nil {
return nil, fmt.Errorf("could not read from CSV for file '%s': %v", fn, err)
}
chans = append(chans, c1)
}
return bench.MergeChans(chans...), nil
}
func parseTransformers(str string) (transformer1, transformer2 *bench.NamedExecutionTransformer, err error) {
colonIdx := strings.Index(str, ":")
if colonIdx == -1 {
transformer1, err = parseTransformer(str)
return transformer1, nil, err
} else {
transformer1, err1 := parseTransformer(str[:colonIdx])
if err1 != nil {
return nil, nil, fmt.Errorf("error transformer1: %w", err1)
}
transformer2, err2 := parseTransformer(str[colonIdx+1:])
if err2 != nil {
return nil, nil, fmt.Errorf("error transformer2: %w", err2)
}
return transformer1, transformer2, nil
}
}
func parseTransformer(str string) (*bench.NamedExecutionTransformer, error) {
var t bench.NamedExecutionTransformer
switch {
case str == "id":
t = bench.NamedExecutionTransformer{
ExecutionTransformer: nil,
Name: "ID",
}
case strings.HasPrefix(str, "f"):
f, err := strconv.ParseFloat(str[1:], 64)
if err != nil {
return nil, fmt.Errorf("could not parse factor transformer: %w", err)
}
t = bench.NamedExecutionTransformer{
ExecutionTransformer: bench.ConstantFactorExecutionTransformerFunc(f, defaultRoundingPrecision),
Name: fmt.Sprintf("ConstantFactor(%g)", f),
}
}
return &t, nil
}
func printMemStats(print bool) {
if print {
ms := &runtime.MemStats{}
runtime.ReadMemStats(ms)
fmt.Fprintf(os.Stdout, "# current memory consumption: sys=%d, heapAlloc=%d, heapInuse=%d, stackInuse=%d, numGCs=%d\n", ms.Sys, ms.HeapAlloc, ms.HeapInuse, ms.StackInuse, ms.NumGC)
}
}