| 1 | package report |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/google/pprof/profile" |
| 11 | ) |
| 12 | |
| 13 | // makeTestStacks generates a StackSet from a supplied list of samples. |
| 14 | func makeTestStacks(samples ...*profile.Sample) StackSet { |
| 15 | prof := makeTestProfile(samples...) |
| 16 | rpt := NewDefault(prof, Options{OutputFormat: Tree, CallTree: true}) |
| 17 | return rpt.Stacks() |
| 18 | } |
| 19 | |
| 20 | func TestStacks(t *testing.T) { |
| 21 | // See report_test.go for the functions available to use in tests. |
| 22 | locs := clearLineAndColumn(testL) |
| 23 | main, foo, bar, tee := locs[0], locs[1], locs[2], locs[3] |
| 24 | |
| 25 | // Also make some file-only locations to test file granularity. |
| 26 | fileMain := makeFileLocation(main) |
| 27 | fileFoo := makeFileLocation(foo) |
| 28 | fileBar := makeFileLocation(bar) |
| 29 | |
| 30 | // stack holds an expected stack value found in StackSet. |
| 31 | type stack struct { |
| 32 | value int64 |
| 33 | names []string |
| 34 | } |
| 35 | makeStack := func(value int64, names ...string) stack { |
| 36 | return stack{value, names} |
| 37 | } |
| 38 | |
| 39 | for _, c := range []struct { |
| 40 | name string |
| 41 | stacks StackSet |
| 42 | expect []stack |
| 43 | }{ |
| 44 | { |
| 45 | "simple", |
| 46 | makeTestStacks( |
| 47 | testSample(100, bar, foo, main), |
| 48 | testSample(200, tee, foo, main), |
| 49 | ), |
| 50 | []stack{ |
| 51 | makeStack(100, "0:root", "1:main", "2:foo", "3:bar"), |
| 52 | makeStack(200, "0:root", "1:main", "2:foo", "4:tee"), |
| 53 | }, |
| 54 | }, |
| 55 | { |
| 56 | "recursion", |
| 57 | makeTestStacks( |
| 58 | testSample(100, bar, foo, foo, foo, main), |
| 59 | testSample(200, bar, foo, foo, main), |
| 60 | ), |
| 61 | []stack{ |
| 62 | // Note: Recursive calls to foo have different source indices. |
| 63 | makeStack(100, "0:root", "1:main", "2:foo", "2:foo", "2:foo", "3:bar"), |
| 64 | makeStack(200, "0:root", "1:main", "2:foo", "2:foo", "3:bar"), |
| 65 | }, |
| 66 | }, |
| 67 | { |
| 68 | "files", |
| 69 | makeTestStacks( |
| 70 | testSample(100, fileFoo, fileMain), |
| 71 | testSample(200, fileBar, fileMain), |
| 72 | ), |
| 73 | []stack{ |
| 74 | makeStack(100, "0:root", "1:dir/main", "2:dir/foo"), |
| 75 | makeStack(200, "0:root", "1:dir/main", "3:dir/bar"), |
| 76 | }, |
| 77 | }, |
| 78 | } { |
| 79 | t.Run(c.name, func(t *testing.T) { |
| 80 | var got []stack |
| 81 | for _, s := range c.stacks.Stacks { |
| 82 | stk := stack{ |
| 83 | value: s.Value, |
| 84 | names: make([]string, len(s.Sources)), |
| 85 | } |
| 86 | for i, src := range s.Sources { |
| 87 | stk.names[i] = fmt.Sprint(src, ":", c.stacks.Sources[src].FullName) |
| 88 | } |
| 89 | got = append(got, stk) |
| 90 | } |
| 91 | if !reflect.DeepEqual(c.expect, got) { |
| 92 | t.Errorf("expecting source %+v, got %+v", c.expect, got) |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestStackSources(t *testing.T) { |
| 99 | // See report_test.go for the functions available to use in tests. |
| 100 | locs := clearLineAndColumn(testL) |
| 101 | main, foo, bar, tee, inl := locs[0], locs[1], locs[2], locs[3], locs[5] |
| 102 | |
| 103 | type srcInfo struct { |
| 104 | name string |
| 105 | self int64 |
| 106 | inlined bool |
| 107 | } |
| 108 | |
| 109 | source := func(stacks StackSet, name string) srcInfo { |
| 110 | src := findSource(stacks, name) |
| 111 | return srcInfo{src.FullName, src.Self, src.Inlined} |
| 112 | } |
| 113 | |
| 114 | for _, c := range []struct { |
| 115 | name string |
| 116 | stacks StackSet |
| 117 | srcs []srcInfo |
| 118 | }{ |
| 119 | { |
| 120 | "empty", |
| 121 | makeTestStacks(), |
| 122 | []srcInfo{}, |
| 123 | }, |
| 124 | { |
| 125 | "two-leaves", |
| 126 | makeTestStacks( |
| 127 | testSample(100, bar, foo, main), |
| 128 | testSample(200, tee, bar, foo, main), |
| 129 | testSample(1000, tee, main), |
| 130 | ), |
| 131 | []srcInfo{ |
| 132 | {"main", 0, false}, |
| 133 | {"bar", 100, false}, |
| 134 | {"foo", 0, false}, |
| 135 | {"tee", 1200, false}, |
| 136 | }, |
| 137 | }, |
| 138 | { |
| 139 | "inlined", |
| 140 | makeTestStacks( |
| 141 | testSample(100, inl), |
| 142 | testSample(200, inl), |
| 143 | ), |
| 144 | []srcInfo{ |
| 145 | // inl has bar->tee |
| 146 | {"tee", 300, true}, |
| 147 | }, |
| 148 | }, |
| 149 | { |
| 150 | "recursion", |
| 151 | makeTestStacks( |
| 152 | testSample(100, foo, foo, foo, main), |
| 153 | testSample(100, foo, foo, main), |
| 154 | ), |
| 155 | []srcInfo{ |
| 156 | {"main", 0, false}, |
| 157 | {"foo", 200, false}, |
| 158 | }, |
| 159 | }, |
| 160 | { |
| 161 | "flat", |
| 162 | makeTestStacks( |
| 163 | testSample(100, main), |
| 164 | testSample(100, foo), |
| 165 | testSample(100, bar), |
| 166 | testSample(100, tee), |
| 167 | ), |
| 168 | []srcInfo{ |
| 169 | {"main", 100, false}, |
| 170 | {"bar", 100, false}, |
| 171 | {"foo", 100, false}, |
| 172 | {"tee", 100, false}, |
| 173 | }, |
| 174 | }, |
| 175 | } { |
| 176 | t.Run(c.name, func(t *testing.T) { |
| 177 | for _, expect := range c.srcs { |
| 178 | got := source(c.stacks, expect.name) |
| 179 | if !reflect.DeepEqual(expect, got) { |
| 180 | t.Errorf("expecting source %+v, got %+v", expect, got) |
| 181 | } |
| 182 | } |
| 183 | }) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func TestLegend(t *testing.T) { |
| 188 | // See report_test.go for the functions available to use in tests. |
| 189 | main, foo, bar, tee := testL[0], testL[1], testL[2], testL[3] |
| 190 | stacks := makeTestStacks( |
| 191 | testSample(100, bar, foo, main), |
| 192 | testSample(200, tee, foo, main), |
| 193 | ) |
| 194 | got := strings.Join(stacks.Legend(), "\n") |
| 195 | expectStrings := []string{"Type: samples", "Showing nodes", "100% of 300 total"} |
| 196 | for _, expect := range expectStrings { |
| 197 | if !strings.Contains(got, expect) { |
| 198 | t.Errorf("missing expected string %q in legend %q", expect, got) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func findSource(stacks StackSet, name string) StackSource { |
| 204 | for _, src := range stacks.Sources { |
| 205 | if src.FullName == name { |
| 206 | return src |
| 207 | } |
| 208 | } |
| 209 | return StackSource{} |
| 210 | } |
| 211 | |
| 212 | // clearLineAndColumn drops line and column numbers to simplify tests that |
| 213 | // do not care about line and column numbers. |
| 214 | func clearLineAndColumn(locs []*profile.Location) []*profile.Location { |
| 215 | result := make([]*profile.Location, len(locs)) |
| 216 | for i, loc := range locs { |
| 217 | newLoc := *loc |
| 218 | newLoc.Line = slices.Clone(loc.Line) |
| 219 | for j := range newLoc.Line { |
| 220 | newLoc.Line[j].Line = 0 |
| 221 | newLoc.Line[j].Column = 0 |
| 222 | } |
| 223 | result[i] = &newLoc |
| 224 | } |
| 225 | return result |
| 226 | } |
| 227 | |
| 228 | // makeFileLocation switches loc from function to file-granularity. |
| 229 | func makeFileLocation(loc *profile.Location) *profile.Location { |
| 230 | result := *loc |
| 231 | result.ID += 1000 |
| 232 | result.Line = slices.Clone(loc.Line) |
| 233 | for i := range result.Line { |
| 234 | fn := *result.Line[i].Function |
| 235 | fn.Filename = "dir/" + fn.Name |
| 236 | fn.Name = "" |
| 237 | result.Line[i].Function = &fn |
| 238 | } |
| 239 | return &result |
| 240 | } |