-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprofile.go
47 lines (41 loc) · 937 Bytes
/
profile.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
package main
import (
"io/ioutil"
"log"
"os"
"runtime/pprof"
"time"
"github.com/elves/elvish/daemon/api"
"github.com/elves/elvish/eval"
"github.com/elves/elvish/parse"
)
func assertOK(err error) {
if err != nil {
log.Fatal(err)
}
}
func profile(name string) {
sourceBytes, err := ioutil.ReadFile(name)
assertOK(err)
source := string(sourceBytes)
profileFile, err := os.Create(name + ".prof")
assertOK(err)
defer profileFile.Close()
ast, err := parse.Parse(name, source)
assertOK(err)
ev := eval.NewEvaler(api.NewClient("/invalid"), nil, "", nil)
op, err := ev.Compile(ast, name, source)
pprof.StartCPUProfile(profileFile)
log.Println("starting to profile", name)
start := time.Now()
defer pprof.StopCPUProfile()
defer func() {
log.Printf("finished profiling %s (%s)", name, time.Now().Sub(start))
}()
ev.Eval(op, name, source)
}
func main() {
for _, name := range os.Args[1:] {
profile(name)
}
}