| 1 | // Copyright 2014 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package driverscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 19 | "math/randscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/" |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 21 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 22 | |
| 23 | "github.com/google/pprof/internal/pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/" |
| 24 | "github.com/google/pprof/internal/proftestscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/" |
| 25 | "github.com/google/pprof/internal/reportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/" |
| 26 | "github.com/google/pprof/internal/transportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/" |
| 27 | "github.com/google/pprof/profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/" |
| 28 | ) |
| 29 | |
| 30 | func TestShellscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/TestShell().(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#) { |
| 31 | p := &profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#{} |
| 32 | generateReportWrapperscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/generateReportWrapper. = checkValuescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/checkValue(). |
| 33 | defer func() { generateReportWrapperscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/generateReportWrapper. = generateReportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/generateReport(). }() |
| 34 | |
| 35 | // Use test commands and variables to exercise interactive processing |
| 36 | var savedCommands commandsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/commands# |
| 37 | savedCommands, pprofCommandsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofCommands. = pprofCommandsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofCommands., testCommandsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/testCommands. |
| 38 | defer func() { pprofCommandsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofCommands. = savedCommands }() |
| 39 | |
| 40 | savedConfig := currentConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/currentConfig().() |
| 41 | defer setCurrentConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/setCurrentConfig().(savedConfig) |
| 42 | |
| 43 | shortcuts1, scScript1 := makeShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/makeShortcuts().(interleavescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/interleave().(scriptscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/script., 2), 1) |
| 44 | shortcuts2, scScript2 := makeShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/makeShortcuts().(interleavescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/interleave().(scriptscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/script., 1), 2) |
| 45 | |
| 46 | var testcases = []struct { |
| 47 | name string |
| 48 | input []string |
| 49 | shortcuts shortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/shortcuts# |
| 50 | allowRx string |
| 51 | numAllowRxMatches int |
| 52 | propagateError bool |
| 53 | }{ |
| 54 | {"Random interleave of independent scripts 1", interleavescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/interleave().(scriptscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/script., 0), pprofShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofShortcuts., "", 0, false}, |
| 55 | {"Random interleave of independent scripts 2", interleavescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/interleave().(scriptscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/script., 1), pprofShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofShortcuts., "", 0, false}, |
| 56 | {"Random interleave of independent scripts with shortcuts 1", scScript1, shortcuts1, "", 0, false}, |
| 57 | {"Random interleave of independent scripts with shortcuts 2", scScript2, shortcuts2, "", 0, false}, |
| 58 | {"Group with invalid value", []string{"sort=this"}, pprofShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofShortcuts., `invalid "sort" value`, 1, false}, |
| 59 | {"No special value provided for the option", []string{"sample_index"}, pprofShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofShortcuts., `please specify a value, e.g. sample_index=<val>`, 1, false}, |
| 60 | {"No string value provided for the option", []string{"focus"}, pprofShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofShortcuts., `please specify a value, e.g. focus=<val>`, 1, false}, |
| 61 | {"No float value provided for the option", []string{"divide_by"}, pprofShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofShortcuts., `please specify a value, e.g. divide_by=<val>`, 1, false}, |
| 62 | {"Helpful input format reminder", []string{"sample_index 0"}, pprofShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofShortcuts., `did you mean: sample_index=0`, 1, false}, |
| 63 | {"Verify propagation of IO errors", []string{"**error**"}, pprofShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofShortcuts., "", 0, true}, |
| 64 | } |
| 65 | |
| 66 | o := setDefaultsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/setDefaults().(&pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Optionsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#{HTTPTransportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#HTTPTransport.: transportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/.Newscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/transport`/New().(nil)}) |
| 67 | for _, tc := range testcases { |
| 68 | t.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(tc.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#) { |
| 69 | setCurrentConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/setCurrentConfig().(savedConfig) |
| 70 | pprofShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofShortcuts. = tc.shortcuts |
| 71 | ui := &proftestscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/.TestUIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#{ |
| 72 | Tscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#T.: t, |
| 73 | Inputscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#Input.: tc.input, |
| 74 | AllowRxscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#AllowRx.: tc.allowRx, |
| 75 | } |
| 76 | o.UIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#UI. = ui |
| 77 | |
| 78 | err := interactivescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/interactive().(p, o) |
| 79 | if (tc.propagateError && err == nil) || (!tc.propagateError && err != nil) { |
| 80 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("%s: %v", tc.name, err) |
| 81 | } |
| 82 | |
| 83 | // Confirm error message written out once. |
| 84 | if tc.numAllowRxMatches != ui.NumAllowRxMatchesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#NumAllowRxMatches. { |
| 85 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("want error message to be printed %d time(s), got %d", |
| 86 | tc.numAllowRxMatches, ui.NumAllowRxMatchesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#NumAllowRxMatches.) |
| 87 | } |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | var testCommandsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/testCommands. = commandsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/commands#{ |
| 93 | "check": &commandscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/command#{reportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/.Rawscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/report`/Raw., nil, nil, true, "", ""}, |
| 94 | } |
| 95 | |
| 96 | // script contains sequences of commands to be executed for testing. Commands |
| 97 | // are split by semicolon and interleaved randomly, so they must be |
| 98 | // independent from each other. |
| 99 | var scriptscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/script. = []string{ |
| 100 | "call_tree=true;call_tree=false;check call_tree=false;call_tree=yes;check call_tree=true", |
| 101 | "mean=1;check mean=true;mean=n;check mean=false", |
| 102 | "nodecount=-1;nodecount=-2;check nodecount=-2;nodecount=999999;check nodecount=999999", |
| 103 | "nodefraction=-1;nodefraction=-2.5;check nodefraction=-2.5;nodefraction=0.0001;check nodefraction=0.0001", |
| 104 | "focus=one;focus=two;check focus=two", |
| 105 | "flat=true;check sort=flat;cum=1;check sort=cum", |
| 106 | } |
| 107 | |
| 108 | func makeShortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/makeShortcuts().(input []string, seed int64) (shortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/shortcuts#, []string) { |
| 109 | rand := randscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/.Newscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/New().(randscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/.NewSourcescip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/NewSource().(seed)) |
| 110 | |
| 111 | s := shortcutsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/shortcuts#{} |
| 112 | var output, chunk []string |
| 113 | for _, l := range input { |
| 114 | chunk = append(chunk, l) |
| 115 | switch rand.Intnscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/Rand#Intn().(3) { |
| 116 | case 0: |
| 117 | // Create a macro for commands in 'chunk'. |
| 118 | macro := fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("alias%d", len(s)) |
| 119 | s[macro] = chunk |
| 120 | output = append(output, macro) |
| 121 | chunk = nil |
| 122 | case 1: |
| 123 | // Append commands in 'chunk' by themselves. |
| 124 | output = append(output, chunk...) |
| 125 | chunk = nil |
| 126 | case 2: |
| 127 | // Accumulate commands into 'chunk' |
| 128 | } |
| 129 | } |
| 130 | output = append(output, chunk...) |
| 131 | return s, output |
| 132 | } |
| 133 | |
| 134 | func checkValuescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/checkValue().(p *profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#, cmd []string, cfg configscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/config#, o *pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Optionsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#) error { |
| 135 | if len(cmd) != 2 { |
| 136 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("expected len(cmd)==2, got %v", cmd) |
| 137 | } |
| 138 | |
| 139 | input := cmd[1] |
| 140 | args := stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.SplitNscip-go gomod github.com/golang/go/src go1.24.0 strings/SplitN().(input, "=", 2) |
| 141 | if len(args) == 0 { |
| 142 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("unexpected empty input") |
| 143 | } |
| 144 | name, value := args[0], "" |
| 145 | if len(args) == 2 { |
| 146 | value = args[1] |
| 147 | } |
| 148 | |
| 149 | f, ok := configFieldMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configFieldMap.[name] |
| 150 | if !ok { |
| 151 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("Could not find variable named %s", name) |
| 152 | } |
| 153 | |
| 154 | if got := cfg.getscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/config#get().(f); got != value { |
| 155 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("Variable %s, want %s, got %s", name, value, got) |
| 156 | } |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | func interleavescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/interleave().(input []string, seed int64) []string { |
| 161 | var inputs [][]string |
| 162 | for _, s := range input { |
| 163 | inputs = append(inputs, stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.24.0 strings/Split().(s, ";")) |
| 164 | } |
| 165 | rand := randscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/.Newscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/New().(randscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/.NewSourcescip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/NewSource().(seed)) |
| 166 | var output []string |
| 167 | for len(inputs) > 0 { |
| 168 | next := rand.Intnscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/Rand#Intn().(len(inputs)) |
| 169 | output = append(output, inputs[next][0]) |
| 170 | if tail := inputs[next][1:]; len(tail) > 0 { |
| 171 | inputs[next] = tail |
| 172 | } else { |
| 173 | inputs = append(inputs[:next], inputs[next+1:]...) |
| 174 | } |
| 175 | } |
| 176 | return output |
| 177 | } |
| 178 | |
| 179 | func TestInteractiveCommandsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/TestInteractiveCommands().(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#) { |
| 180 | type interactiveTestcase struct { |
| 181 | input string |
| 182 | want map[string]string |
| 183 | } |
| 184 | |
| 185 | testcases := []interactiveTestcase{ |
| 186 | { |
| 187 | "top 10 --cum focus1 -ignore focus2", |
| 188 | map[string]string{ |
| 189 | "nodecount": "10", |
| 190 | "sort": "cum", |
| 191 | "focus": "focus1|focus2", |
| 192 | "ignore": "ignore", |
| 193 | }, |
| 194 | }, |
| 195 | { |
| 196 | "top10 --cum focus1 -ignore focus2", |
| 197 | map[string]string{ |
| 198 | "nodecount": "10", |
| 199 | "sort": "cum", |
| 200 | "focus": "focus1|focus2", |
| 201 | "ignore": "ignore", |
| 202 | }, |
| 203 | }, |
| 204 | { |
| 205 | "dot", |
| 206 | map[string]string{ |
| 207 | "nodecount": "80", |
| 208 | "sort": "flat", |
| 209 | }, |
| 210 | }, |
| 211 | { |
| 212 | "tags -ignore1 -ignore2 focus1 >out", |
| 213 | map[string]string{ |
| 214 | "nodecount": "80", |
| 215 | "sort": "flat", |
| 216 | "output": "out", |
| 217 | "tagfocus": "focus1", |
| 218 | "tagignore": "ignore1|ignore2", |
| 219 | }, |
| 220 | }, |
| 221 | { |
| 222 | "weblist find -test", |
| 223 | map[string]string{ |
| 224 | "granularity": "addresses", |
| 225 | "noinlines": "false", |
| 226 | "nodecount": "0", |
| 227 | "sort": "flat", |
| 228 | "ignore": "test", |
| 229 | }, |
| 230 | }, |
| 231 | { |
| 232 | "callgrind fun -ignore >out", |
| 233 | map[string]string{ |
| 234 | "granularity": "addresses", |
| 235 | "nodecount": "0", |
| 236 | "sort": "flat", |
| 237 | "output": "out", |
| 238 | }, |
| 239 | }, |
| 240 | { |
| 241 | "999", |
| 242 | nil, // Error |
| 243 | }, |
| 244 | } |
| 245 | |
| 246 | for _, tc := range testcases { |
| 247 | cmd, cfg, err := parseCommandLinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/parseCommandLine().(stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.24.0 strings/Fields().(tc.input)) |
| 248 | if tc.want == nil && err != nil { |
| 249 | // Error expected |
| 250 | continue |
| 251 | } |
| 252 | if err != nil { |
| 253 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("failed on %q: %v", tc.input, err) |
| 254 | continue |
| 255 | } |
| 256 | |
| 257 | // Get report output format |
| 258 | c := pprofCommandsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/pprofCommands.[cmd[0]] |
| 259 | if c == nil { |
| 260 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("unexpected nil command") |
| 261 | } |
| 262 | cfg = applyCommandOverridesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/applyCommandOverrides().(cmd[0], c.formatscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/command#format., cfg) |
| 263 | |
| 264 | for n, want := range tc.want { |
| 265 | if got := cfg.getscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/config#get().(configFieldMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configFieldMap.[n]); got != want { |
| 266 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("failed on %q, cmd=%q, %s got %s, want %s", tc.input, cmd, n, got, want) |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |