forked from google/starlark-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_test.go
83 lines (75 loc) · 1.89 KB
/
profile_test.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
// Copyright 2019 The Bazel Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package starlark_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"go.starlark.net/starlark"
)
// TestProfile is a simple integration test that the profiler
// emits minimally plausible pprof-compatible output.
func TestProfile(t *testing.T) {
prof, err := ioutil.TempFile("", "profile_test")
if err != nil {
t.Fatal(err)
}
defer prof.Close()
defer os.Remove(prof.Name())
if err := starlark.StartProfile(prof); err != nil {
t.Fatal(err)
}
const src = `
def fibonacci(n):
x, y = 1, 1
for i in range(n):
x, y = y, x+y
return y
fibonacci(100000)
`
thread := new(starlark.Thread)
if _, err := starlark.ExecFile(thread, "foo.star", src, nil); err != nil {
_ = starlark.StopProfile()
t.Fatal(err)
}
if err := starlark.StopProfile(); err != nil {
t.Fatal(err)
}
prof.Sync()
cmd := exec.Command("go", "tool", "pprof", "-top", prof.Name())
cmd.Stderr = new(bytes.Buffer)
cmd.Stdout = new(bytes.Buffer)
if err := cmd.Run(); err != nil {
t.Fatalf("pprof failed: %v; output=<<%s>>", err, cmd.Stderr)
}
// Typical output (may vary by go release):
//
// Type: wall
// Time: Apr 4, 2019 at 11:10am (EDT)
// Duration: 251.62ms, Total samples = 250ms (99.36%)
// Showing nodes accounting for 250ms, 100% of 250ms total
// flat flat% sum% cum cum%
// 320ms 100% 100% 320ms 100% fibonacci
// 0 0% 100% 320ms 100% foo.star
//
// We'll assert a few key substrings are present.
got := fmt.Sprint(cmd.Stdout)
for _, want := range []string{
"flat%",
"fibonacci",
"foo.star",
} {
if !strings.Contains(got, want) {
t.Errorf("output did not contain %q", want)
}
}
if t.Failed() {
t.Logf("stderr=%v", cmd.Stderr)
t.Logf("stdout=%v", cmd.Stdout)
}
}