Debian Codegraph @

internal/proftest/proftest.go

raw symbols SCIP
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 proftest provides some utility routines to test other
16 // packages related to profiles.
17 package proftestscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/
18
19 import (
20 "encoding/jsonscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/"
21 "flagscip-go gomod github.com/golang/go/src go1.24.0 flag/"
22 "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/"
23 "ioscip-go gomod github.com/golang/go/src go1.24.0 io/"
24 "osscip-go gomod github.com/golang/go/src go1.24.0 os/"
25 "os/execscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/"
26 "regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/"
27 "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/"
28
29 _ "embedscip-go gomod github.com/golang/go/src go1.24.0 embed/" // For embedding profiles needed by tests and benchmarks
30 )
31
32 var flagLargeProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/flagLargeProfile. = flagscip-go gomod github.com/golang/go/src go1.24.0 flag/.Stringscip-go gomod github.com/golang/go/src go1.24.0 flag/String().("large_profile", "", "The name of a file that contains a profile to use in benchmarks. If empty, a profile of a synthetic program is used.")
33
34 // Diff compares two byte arrays using the diff tool to highlight the
35 // differences. It is meant for testing purposes to display the
36 // differences between expected and actual output.
37 func Diffscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/Diff().(b1, b2 []byte) (data []byte, err error) {
38 f1, err := osscip-go gomod github.com/golang/go/src go1.24.0 os/.CreateTempscip-go gomod github.com/golang/go/src go1.24.0 os/CreateTemp().("", "proto_test")
39 if err != nil {
40 return nil, err
41 }
42 defer osscip-go gomod github.com/golang/go/src go1.24.0 os/.Removescip-go gomod github.com/golang/go/src go1.24.0 os/Remove().(f1.Namescip-go gomod github.com/golang/go/src go1.24.0 os/File#Name().())
43 defer f1.Closescip-go gomod github.com/golang/go/src go1.24.0 os/File#Close().()
44
45 f2, err := osscip-go gomod github.com/golang/go/src go1.24.0 os/.CreateTempscip-go gomod github.com/golang/go/src go1.24.0 os/CreateTemp().("", "proto_test")
46 if err != nil {
47 return nil, err
48 }
49 defer osscip-go gomod github.com/golang/go/src go1.24.0 os/.Removescip-go gomod github.com/golang/go/src go1.24.0 os/Remove().(f2.Namescip-go gomod github.com/golang/go/src go1.24.0 os/File#Name().())
50 defer f2.Closescip-go gomod github.com/golang/go/src go1.24.0 os/File#Close().()
51
52 f1.Writescip-go gomod github.com/golang/go/src go1.24.0 os/File#Write().(b1)
53 f2.Writescip-go gomod github.com/golang/go/src go1.24.0 os/File#Write().(b2)
54
55 data, err = execscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/Command().("diff", "-u", f1.Namescip-go gomod github.com/golang/go/src go1.24.0 os/File#Name().(), f2.Namescip-go gomod github.com/golang/go/src go1.24.0 os/File#Name().()).CombinedOutputscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/Cmd#CombinedOutput().()
56 if len(data) > 0 {
57 // diff exits with a non-zero status when the files don't match.
58 // Ignore that failure as long as we get output.
59 err = nil
60 }
61 if err != nil {
62 data = fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Appendfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Appendf().(nil, "diff failed: %v\nb1: %q\nb2: %q\n", err, b1, b2)
63 err = nil
64 }
65 return
66 }
67
68 // EncodeJSON encodes a value into a byte array. This is intended for
69 // testing purposes.
70 func EncodeJSONscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/EncodeJSON().(x interface{}) []byte {
71 data, err := jsonscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/.MarshalIndentscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/MarshalIndent().(x, "", " ")
72 if err != nil {
73 panic(err)
74 }
75 data = append(data, '\n')
76 return data
77 }
78
79 // TestUI implements the plugin.UI interface, triggering test failures
80 // if more than Ignore errors not matching AllowRx are printed.
81 // Also tracks the number of times the error matches AllowRx in
82 // NumAllowRxMatches.
83 type TestUIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI# struct {
84 Tscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#T. testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.TBscip-go gomod github.com/golang/go/src go1.24.0 testing/TB#
85 Ignorescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#Ignore. int
86 AllowRxscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#AllowRx. string
87 NumAllowRxMatchesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#NumAllowRxMatches. int
88 Inputscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#Input. []string
89 indexscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#index. int
90 }
91
92 // ReadLine returns no input, as no input is expected during testing.
93 func (ui *TestUIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#) ReadLinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#ReadLine().(_ string) (string, error) {
94 if ui.indexscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#index. >= len(ui.Inputscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#Input.) {
95 return "", ioscip-go gomod github.com/golang/go/src go1.24.0 io/.EOFscip-go gomod github.com/golang/go/src go1.24.0 io/EOF.
96 }
97 input := ui.Inputscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#Input.[ui.indexscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#index.]
98 ui.indexscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#index.++
99 if input == "**error**" {
100 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().("error: %s", input)
101 }
102 return input, nil
103 }
104
105 // Print messages are discarded by the test UI.
106 func (ui *TestUIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#) Printscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#Print().(args ...interface{}) {
107 }
108
109 // PrintErr messages may trigger an error failure. A fixed number of
110 // error messages are permitted when appropriate.
111 func (ui *TestUIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#) PrintErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#PrintErr().(args ...interface{}) {
112 if ui.AllowRxscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#AllowRx. != "" {
113 if matched, err := regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/MatchString().(ui.AllowRxscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#AllowRx., 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().(args...)); matched || err != nil {
114 if err != nil {
115 ui.Tscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#T..Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/TB#Errorf().("failed to match against regex %q: %v", ui.AllowRxscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#AllowRx., err)
116 }
117 ui.NumAllowRxMatchesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#NumAllowRxMatches.++
118 return
119 }
120 }
121 if ui.Ignorescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#Ignore. > 0 {
122 ui.Ignorescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#Ignore.--
123 return
124 }
125 // Stringify arguments with fmt.Sprint() to match what default UI
126 // implementation does. Without this Error() calls fmt.Sprintln() which
127 // _always_ adds spaces between arguments, unlike fmt.Sprint() which only
128 // adds them between arguments if neither is string.
129 ui.Tscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#T..Errorscip-go gomod github.com/golang/go/src go1.24.0 testing/TB#Error().("unexpected error: " + 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().(args...))
130 }
131
132 // IsTerminal indicates if the UI is an interactive terminal.
133 func (ui *TestUIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#) IsTerminalscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#IsTerminal().() bool {
134 return false
135 }
136
137 // WantBrowser indicates whether a browser should be opened with the -http option.
138 func (ui *TestUIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#) WantBrowserscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#WantBrowser().() bool {
139 return false
140 }
141
142 // SetAutoComplete is not supported by the test UI.
143 func (ui *TestUIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#) SetAutoCompletescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#SetAutoComplete().(_ func(string) string) {
144 }
145
146 // LargeProfile returns a large profile that may be useful in benchmarks.
147 //
148 // If the flag --large_profile is set, the contents of the file
149 // named by the flag are returned. Otherwise an embedded profile (~1.2MB)
150 // for a synthetic program is returned.
151 func LargeProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/LargeProfile().(tb testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.TBscip-go gomod github.com/golang/go/src go1.24.0 testing/TB#) []byte {
152 tb.Helperscip-go gomod github.com/golang/go/src go1.24.0 testing/TB#Helper().()
153 if f := *flagLargeProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/flagLargeProfile.; f != "" {
154 // Use custom profile.
155 data, err := osscip-go gomod github.com/golang/go/src go1.24.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.24.0 os/ReadFile().(f)
156 if err != nil {
157 tb.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/TB#Fatalf().("custom profile file: %v\n", err)
158 }
159 return data
160 }
161
162 return largeProfileDatascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/largeProfileData.
163 }
164
165 //go:embed testdata/large.cpu
166 var largeProfileDatascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/largeProfileData. []byte