| 1 | package reportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 5 | "reflectscip-go gomod github.com/golang/go/src go1.24.0 reflect/" |
| 6 | "slicesscip-go gomod github.com/golang/go/src go1.24.0 slices/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 8 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 9 | |
| 10 | "github.com/google/pprof/profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/" |
| 11 | ) |
| 12 | |
| 13 | // makeTestStacks generates a StackSet from a supplied list of samples. |
| 14 | func makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().(samples ...*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#) StackSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSet# { |
| 15 | prof := makeTestProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestProfile().(samples...) |
| 16 | rpt := NewDefaultscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/NewDefault().(prof, Optionsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/Options#{OutputFormatscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/Options#OutputFormat.: Treescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/Tree., CallTreescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/Options#CallTree.: true}) |
| 17 | return rpt.Stacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/Report#Stacks().() |
| 18 | } |
| 19 | |
| 20 | func TestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/TestStacks().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 21 | // See report_test.go for the functions available to use in tests. |
| 22 | locs := clearLineAndColumnscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/clearLineAndColumn().(testLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/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 := makeFileLocationscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeFileLocation().(main) |
| 27 | fileFoo := makeFileLocationscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeFileLocation().(foo) |
| 28 | fileBar := makeFileLocationscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/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 StackSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSet# |
| 42 | expect []stack |
| 43 | }{ |
| 44 | { |
| 45 | "simple", |
| 46 | makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().( |
| 47 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, bar, foo, main), |
| 48 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/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 | makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().( |
| 58 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, bar, foo, foo, foo, main), |
| 59 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/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 | makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().( |
| 70 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, fileFoo, fileMain), |
| 71 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/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.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(c.name, func(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 80 | var got []stack |
| 81 | for _, s := range c.stacks.Stacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSet#Stacks. { |
| 82 | stk := stack{ |
| 83 | value: s.Valuescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/Stack#Value., |
| 84 | names: make([]string, len(s.Sourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/Stack#Sources.)), |
| 85 | } |
| 86 | for i, src := range s.Sourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/Stack#Sources. { |
| 87 | stk.names[i] = fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprint().(src, ":", c.stacks.Sourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSet#Sources.[src].FullNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSource#FullName.) |
| 88 | } |
| 89 | got = append(got, stk) |
| 90 | } |
| 91 | if !reflectscip-go gomod github.com/golang/go/src go1.24.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.24.0 reflect/DeepEqual().(c.expect, got) { |
| 92 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("expecting source %+v, got %+v", c.expect, got) |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestStackSourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/TestStackSources().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 99 | // See report_test.go for the functions available to use in tests. |
| 100 | locs := clearLineAndColumnscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/clearLineAndColumn().(testLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/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 StackSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSet#, name string) srcInfo { |
| 110 | src := findSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/findSource().(stacks, name) |
| 111 | return srcInfo{src.FullNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSource#FullName., src.Selfscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSource#Self., src.Inlinedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSource#Inlined.} |
| 112 | } |
| 113 | |
| 114 | for _, c := range []struct { |
| 115 | name string |
| 116 | stacks StackSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSet# |
| 117 | srcs []srcInfo |
| 118 | }{ |
| 119 | { |
| 120 | "empty", |
| 121 | makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().(), |
| 122 | []srcInfo{}, |
| 123 | }, |
| 124 | { |
| 125 | "two-leaves", |
| 126 | makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().( |
| 127 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, bar, foo, main), |
| 128 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(200, tee, bar, foo, main), |
| 129 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/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 | makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().( |
| 141 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, inl), |
| 142 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(200, inl), |
| 143 | ), |
| 144 | []srcInfo{ |
| 145 | // inl has bar->tee |
| 146 | {"tee", 300, true}, |
| 147 | }, |
| 148 | }, |
| 149 | { |
| 150 | "recursion", |
| 151 | makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().( |
| 152 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, foo, foo, foo, main), |
| 153 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, foo, foo, main), |
| 154 | ), |
| 155 | []srcInfo{ |
| 156 | {"main", 0, false}, |
| 157 | {"foo", 200, false}, |
| 158 | }, |
| 159 | }, |
| 160 | { |
| 161 | "flat", |
| 162 | makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().( |
| 163 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, main), |
| 164 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, foo), |
| 165 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, bar), |
| 166 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/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.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(c.name, func(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 177 | for _, expect := range c.srcs { |
| 178 | got := source(c.stacks, expect.name) |
| 179 | if !reflectscip-go gomod github.com/golang/go/src go1.24.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.24.0 reflect/DeepEqual().(expect, got) { |
| 180 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("expecting source %+v, got %+v", expect, got) |
| 181 | } |
| 182 | } |
| 183 | }) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func TestLegendscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/TestLegend().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 188 | // See report_test.go for the functions available to use in tests. |
| 189 | main, foo, bar, tee := testLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testL.[0], testLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testL.[1], testLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testL.[2], testLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testL.[3] |
| 190 | stacks := makeTestStacksscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeTestStacks().( |
| 191 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(100, bar, foo, main), |
| 192 | testSamplescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/testSample().(200, tee, foo, main), |
| 193 | ) |
| 194 | got := stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.0 strings/Join().(stacks.Legendscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSet#Legend().(), "\n") |
| 195 | expectStrings := []string{"Type: samples", "Showing nodes", "100% of 300 total"} |
| 196 | for _, expect := range expectStrings { |
| 197 | if !stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.24.0 strings/Contains().(got, expect) { |
| 198 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("missing expected string %q in legend %q", expect, got) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func findSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/findSource().(stacks StackSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSet#, name string) StackSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSource# { |
| 204 | for _, src := range stacks.Sourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSet#Sources. { |
| 205 | if src.FullNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/StackSource#FullName. == name { |
| 206 | return src |
| 207 | } |
| 208 | } |
| 209 | return StackSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/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 clearLineAndColumnscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/clearLineAndColumn().(locs []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#) []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location# { |
| 215 | result := make([]*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#, len(locs)) |
| 216 | for i, loc := range locs { |
| 217 | newLoc := *loc |
| 218 | newLoc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. = slicesscip-go gomod github.com/golang/go/src go1.24.0 slices/.Clonescip-go gomod github.com/golang/go/src go1.24.0 slices/Clone().(loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) |
| 219 | for j := range newLoc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. { |
| 220 | newLoc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.[j].Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Line. = 0 |
| 221 | newLoc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.[j].Columnscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Column. = 0 |
| 222 | } |
| 223 | result[i] = &newLoc |
| 224 | } |
| 225 | return result |
| 226 | } |
| 227 | |
| 228 | // makeFileLocation switches loc from function to file-granularity. |
| 229 | func makeFileLocationscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/makeFileLocation().(loc *profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#) *profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location# { |
| 230 | result := *loc |
| 231 | result.IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID. += 1000 |
| 232 | result.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. = slicesscip-go gomod github.com/golang/go/src go1.24.0 slices/.Clonescip-go gomod github.com/golang/go/src go1.24.0 slices/Clone().(loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) |
| 233 | for i := range result.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. { |
| 234 | fn := *result.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.[i].Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function. |
| 235 | fn.Filenamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Filename. = "dir/" + fn.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Name. |
| 236 | fn.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Name. = "" |
| 237 | result.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.[i].Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function. = &fn |
| 238 | } |
| 239 | return &result |
| 240 | } |