| 1 | // Copyright 2023 The Go 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 pprof_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "compress/gzip" |
| 10 | "io" |
| 11 | "log" |
| 12 | "os" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "golang.org/x/tools/internal/pprof" |
| 17 | ) |
| 18 | |
| 19 | func TestTotalTime(t *testing.T) { |
| 20 | // $ go tool pprof testdata/sample.pprof <&- 2>&1 | grep Total |
| 21 | // Duration: 11.10s, Total samples = 27.59s (248.65%) |
| 22 | const ( |
| 23 | filename = "testdata/sample.pprof" |
| 24 | want = time.Duration(27590003550) |
| 25 | ) |
| 26 | |
| 27 | profGz, err := os.ReadFile(filename) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | rd, err := gzip.NewReader(bytes.NewReader(profGz)) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | payload, err := io.ReadAll(rd) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | got, err := pprof.TotalTime(payload) |
| 40 | if err != nil { |
| 41 | log.Fatal(err) |
| 42 | } |
| 43 | if got != want { |
| 44 | t.Fatalf("TotalTime(%q): got %v (%d), want %v (%d)", filename, got, got, want, want) |
| 45 | } |
| 46 | } |