Bez popisu

source_test.go 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package report
  2. import (
  3. "bytes"
  4. "os"
  5. "path/filepath"
  6. "regexp"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "github.com/google/pprof/internal/binutils"
  11. "github.com/google/pprof/profile"
  12. )
  13. func TestWebList(t *testing.T) {
  14. if runtime.GOOS != "linux" {
  15. t.Skip("weblist only tested on linux")
  16. }
  17. cpu := readProfile(filepath.Join("testdata", "sample.cpu"), t)
  18. rpt := New(cpu, &Options{
  19. OutputFormat: WebList,
  20. Symbol: regexp.MustCompile("busyLoop"),
  21. SampleValue: func(v []int64) int64 { return v[1] },
  22. SampleUnit: cpu.SampleType[1].Unit,
  23. })
  24. buf := bytes.NewBuffer(nil)
  25. if err := Generate(buf, rpt, &binutils.Binutils{}); err != nil {
  26. t.Fatalf("could not generate weblist: %v", err)
  27. }
  28. output := buf.String()
  29. for _, expect := range []string{"func busyLoop", "callq", "math.Abs"} {
  30. if !strings.Contains(output, expect) {
  31. t.Errorf("weblist output does not contain '%s':\n%s", expect, output)
  32. }
  33. }
  34. }
  35. func TestIndentation(t *testing.T) {
  36. for _, c := range []struct {
  37. str string
  38. indent int
  39. }{
  40. {"", 0},
  41. {"foobar", 0},
  42. {" foo", 2},
  43. {"\tfoo", 8},
  44. {"\t foo", 9},
  45. {" \tfoo", 8},
  46. {" \tfoo", 8},
  47. {" \tfoo", 16},
  48. } {
  49. if n := indentation(c.str); n != c.indent {
  50. t.Errorf("indentation for %v: expect %d, got %d\n", c.str, c.indent, n)
  51. }
  52. }
  53. }
  54. func readProfile(fname string, t *testing.T) *profile.Profile {
  55. file, err := os.Open(fname)
  56. if err != nil {
  57. t.Fatalf("%s: could not open profile: %v", fname, err)
  58. }
  59. defer file.Close()
  60. p, err := profile.Parse(file)
  61. if err != nil {
  62. t.Fatalf("%s: could not parse profile: %v", fname, err)
  63. }
  64. // Fix file names so they do not include absolute path names.
  65. fix := func(s string) string {
  66. const testdir = "/internal/report/"
  67. pos := strings.Index(s, testdir)
  68. if pos == -1 {
  69. return s
  70. }
  71. return s[pos+len(testdir):]
  72. }
  73. for _, m := range p.Mapping {
  74. m.File = fix(m.File)
  75. }
  76. for _, f := range p.Function {
  77. f.Filename = fix(f.Filename)
  78. }
  79. return p
  80. }