github.com/lab47/exprcore@v0.0.0-20210525052339-fb7d6bd9331e/exprcore/profile_test.go (about) 1 // Copyright 2019 The Bazel Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package exprcore_test 6 7 import ( 8 "bytes" 9 "fmt" 10 "io/ioutil" 11 "os" 12 "os/exec" 13 "strings" 14 "testing" 15 16 "github.com/lab47/exprcore/exprcore" 17 ) 18 19 // TestProfile is a simple integration test that the profiler 20 // emits minimally plausible pprof-compatible output. 21 func TestProfile(t *testing.T) { 22 prof, err := ioutil.TempFile("", "profile_test") 23 if err != nil { 24 t.Fatal(err) 25 } 26 defer prof.Close() 27 defer os.Remove(prof.Name()) 28 if err := exprcore.StartProfile(prof); err != nil { 29 t.Fatal(err) 30 } 31 32 const src = ` 33 def fibonacci(n) { 34 res = list(range(n)) 35 for i in res[2:] { 36 res[i] = res[i-2] + res[i-1] 37 } 38 return res 39 } 40 fibonacci(100000) 41 ` 42 43 thread := new(exprcore.Thread) 44 if _, err := exprcore.ExecFile(thread, "foo.star", src, nil); err != nil { 45 _ = exprcore.StopProfile() 46 t.Fatal(err) 47 } 48 if err := exprcore.StopProfile(); err != nil { 49 t.Fatal(err) 50 } 51 prof.Sync() 52 cmd := exec.Command("go", "tool", "pprof", "-top", prof.Name()) 53 cmd.Stderr = new(bytes.Buffer) 54 cmd.Stdout = new(bytes.Buffer) 55 if err := cmd.Run(); err != nil { 56 t.Fatalf("pprof failed: %v; output=<<%s>>", err, cmd.Stderr) 57 } 58 59 // Typical output (may vary by go release): 60 // 61 // Type: wall 62 // Time: Apr 4, 2019 at 11:10am (EDT) 63 // Duration: 251.62ms, Total samples = 250ms (99.36%) 64 // Showing nodes accounting for 250ms, 100% of 250ms total 65 // flat flat% sum% cum cum% 66 // 320ms 100% 100% 320ms 100% fibonacci 67 // 0 0% 100% 320ms 100% foo.star 68 // 69 // We'll assert a few key substrings are present. 70 got := fmt.Sprint(cmd.Stdout) 71 for _, want := range []string{ 72 "flat%", 73 "fibonacci", 74 "foo.star", 75 } { 76 if !strings.Contains(got, want) { 77 t.Errorf("output did not contain %q", want) 78 } 79 } 80 if t.Failed() { 81 t.Logf("stderr=%v", cmd.Stderr) 82 t.Logf("stdout=%v", cmd.Stdout) 83 } 84 }