-
Notifications
You must be signed in to change notification settings - Fork 9
/
profiling.go
92 lines (75 loc) · 1.87 KB
/
profiling.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
package common
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime/trace"
"testing"
"github.com/pkg/profile"
)
const GKR_MIMC string = "gkr-mimc"
// Helper to create to a file, and does a few checks
// Ensure it does not create a file from outside the repo
// The path should be specified assuming it is relative to the root of the project
func GetPath(p string) (string, error) {
base, _ := os.Getwd()
// Only keeps the part of the base, that leads to the project root
loop:
for {
switch path.Base(base) {
case ".":
return "", fmt.Errorf("the current directory `%v` is not included in `%v`. try running from another place", GKR_MIMC, base)
case GKR_MIMC:
// We stop truncating, we have the right absolute path
break loop
default:
// Truncate the base path : haven't found a better way than this
base = path.Clean(base + "/..")
}
}
// Get dir where we want to write
dir, file := filepath.Split(p)
dir = filepath.Join(base, dir)
// Ensure the folder was created prior running the test
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return "", err
}
// The create the file
p = filepath.Join(dir, file)
return p, nil
}
// ProfileTrace run the benchmark function with optionally, benchmarking and tracing
func ProfileTrace(b *testing.B, profiled, traced bool, fn func()) {
var f *os.File
var pprof interface{ Stop() }
if traced {
_path, err := GetPath(fmt.Sprintf("profiling/%v/trace.out", b.Name()))
if err != nil {
panic(err)
}
f, err = os.Create(_path)
if err != nil {
panic(err)
}
err = trace.Start(f)
if err != nil {
panic(err)
}
defer trace.Stop()
}
if profiled {
_path, err := GetPath(fmt.Sprintf("profiling/%v", b.Name()))
if err != nil {
panic(err)
}
pprof = profile.Start(
profile.ProfilePath(_path),
profile.Quiet,
)
defer pprof.Stop()
}
b.StartTimer()
defer b.StopTimer()
fn()
}