github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/net/http/pprof/pprof_test.go (about) 1 // Copyright 2018 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 6 7 import ( 8 "bytes" 9 "fmt" 10 "internal/profile" 11 "io" 12 "net/http" 13 "net/http/httptest" 14 "runtime" 15 "runtime/pprof" 16 "strings" 17 "sync" 18 "sync/atomic" 19 "testing" 20 "time" 21 ) 22 23 // TestDescriptions checks that the profile names under runtime/pprof package 24 // have a key in the description map. 25 func TestDescriptions(t *testing.T) { 26 for _, p := range pprof.Profiles() { 27 _, ok := profileDescriptions[p.Name()] 28 if ok != true { 29 t.Errorf("%s does not exist in profileDescriptions map\n", p.Name()) 30 } 31 } 32 } 33 34 func TestHandlers(t *testing.T) { 35 testCases := []struct { 36 path string 37 handler http.HandlerFunc 38 statusCode int 39 contentType string 40 contentDisposition string 41 resp []byte 42 }{ 43 {"/debug/pprof/<script>scripty<script>", Index, http.StatusNotFound, "text/plain; charset=utf-8", "", []byte("Unknown profile\n")}, 44 {"/debug/pprof/heap", Index, http.StatusOK, "application/octet-stream", `attachment; filename="heap"`, nil}, 45 {"/debug/pprof/heap?debug=1", Index, http.StatusOK, "text/plain; charset=utf-8", "", nil}, 46 {"/debug/pprof/cmdline", Cmdline, http.StatusOK, "text/plain; charset=utf-8", "", nil}, 47 {"/debug/pprof/profile?seconds=1", Profile, http.StatusOK, "application/octet-stream", `attachment; filename="profile"`, nil}, 48 {"/debug/pprof/symbol", Symbol, http.StatusOK, "text/plain; charset=utf-8", "", nil}, 49 {"/debug/pprof/trace", Trace, http.StatusOK, "application/octet-stream", `attachment; filename="trace"`, nil}, 50 {"/debug/pprof/mutex", Index, http.StatusOK, "application/octet-stream", `attachment; filename="mutex"`, nil}, 51 {"/debug/pprof/block?seconds=1", Index, http.StatusOK, "application/octet-stream", `attachment; filename="block-delta"`, nil}, 52 {"/debug/pprof/goroutine?seconds=1", Index, http.StatusOK, "application/octet-stream", `attachment; filename="goroutine-delta"`, nil}, 53 {"/debug/pprof/", Index, http.StatusOK, "text/html; charset=utf-8", "", []byte("Types of profiles available:")}, 54 } 55 for _, tc := range testCases { 56 t.Run(tc.path, func(t *testing.T) { 57 req := httptest.NewRequest("GET", "http://example.com"+tc.path, nil) 58 w := httptest.NewRecorder() 59 tc.handler(w, req) 60 61 resp := w.Result() 62 if got, want := resp.StatusCode, tc.statusCode; got != want { 63 t.Errorf("status code: got %d; want %d", got, want) 64 } 65 66 body, err := io.ReadAll(resp.Body) 67 if err != nil { 68 t.Errorf("when reading response body, expected non-nil err; got %v", err) 69 } 70 if got, want := resp.Header.Get("X-Content-Type-Options"), "nosniff"; got != want { 71 t.Errorf("X-Content-Type-Options: got %q; want %q", got, want) 72 } 73 if got, want := resp.Header.Get("Content-Type"), tc.contentType; got != want { 74 t.Errorf("Content-Type: got %q; want %q", got, want) 75 } 76 if got, want := resp.Header.Get("Content-Disposition"), tc.contentDisposition; got != want { 77 t.Errorf("Content-Disposition: got %q; want %q", got, want) 78 } 79 80 if resp.StatusCode == http.StatusOK { 81 return 82 } 83 if got, want := resp.Header.Get("X-Go-Pprof"), "1"; got != want { 84 t.Errorf("X-Go-Pprof: got %q; want %q", got, want) 85 } 86 if !bytes.Equal(body, tc.resp) { 87 t.Errorf("response: got %q; want %q", body, tc.resp) 88 } 89 }) 90 } 91 } 92 93 var Sink uint32 94 95 func mutexHog1(mu1, mu2 *sync.Mutex, start time.Time, dt time.Duration) { 96 atomic.AddUint32(&Sink, 1) 97 for time.Since(start) < dt { 98 // When using gccgo the loop of mutex operations is 99 // not preemptible. This can cause the loop to block a GC, 100 // causing the time limits in TestDeltaContentionz to fail. 101 // Since this loop is not very realistic, when using 102 // gccgo add preemption points 100 times a second. 103 t1 := time.Now() 104 for time.Since(start) < dt && time.Since(t1) < 10*time.Millisecond { 105 mu1.Lock() 106 mu2.Lock() 107 mu1.Unlock() 108 mu2.Unlock() 109 } 110 if runtime.Compiler == "gccgo" { 111 runtime.Gosched() 112 } 113 } 114 } 115 116 // mutexHog2 is almost identical to mutexHog but we keep them separate 117 // in order to distinguish them with function names in the stack trace. 118 // We make them slightly different, using Sink, because otherwise 119 // gccgo -c opt will merge them. 120 func mutexHog2(mu1, mu2 *sync.Mutex, start time.Time, dt time.Duration) { 121 atomic.AddUint32(&Sink, 2) 122 for time.Since(start) < dt { 123 // See comment in mutexHog. 124 t1 := time.Now() 125 for time.Since(start) < dt && time.Since(t1) < 10*time.Millisecond { 126 mu1.Lock() 127 mu2.Lock() 128 mu1.Unlock() 129 mu2.Unlock() 130 } 131 if runtime.Compiler == "gccgo" { 132 runtime.Gosched() 133 } 134 } 135 } 136 137 // mutexHog starts multiple goroutines that runs the given hogger function for the specified duration. 138 // The hogger function will be given two mutexes to lock & unlock. 139 func mutexHog(duration time.Duration, hogger func(mu1, mu2 *sync.Mutex, start time.Time, dt time.Duration)) { 140 start := time.Now() 141 mu1 := new(sync.Mutex) 142 mu2 := new(sync.Mutex) 143 var wg sync.WaitGroup 144 wg.Add(10) 145 for i := 0; i < 10; i++ { 146 go func() { 147 defer wg.Done() 148 hogger(mu1, mu2, start, duration) 149 }() 150 } 151 wg.Wait() 152 } 153 154 func TestDeltaProfile(t *testing.T) { 155 rate := runtime.SetMutexProfileFraction(1) 156 defer func() { 157 runtime.SetMutexProfileFraction(rate) 158 }() 159 160 // mutexHog1 will appear in non-delta mutex profile 161 // if the mutex profile works. 162 mutexHog(20*time.Millisecond, mutexHog1) 163 164 // If mutexHog1 does not appear in the mutex profile, 165 // skip this test. Mutex profile is likely not working, 166 // so is the delta profile. 167 168 p, err := query("/debug/pprof/mutex") 169 if err != nil { 170 t.Skipf("mutex profile is unsupported: %v", err) 171 } 172 173 if !seen(p, "mutexHog1") { 174 t.Skipf("mutex profile is not working: %v", p) 175 } 176 177 // causes mutexHog2 call stacks to appear in the mutex profile. 178 done := make(chan bool) 179 go func() { 180 for { 181 mutexHog(20*time.Millisecond, mutexHog2) 182 select { 183 case <-done: 184 done <- true 185 return 186 default: 187 time.Sleep(10 * time.Millisecond) 188 } 189 } 190 }() 191 defer func() { // cleanup the above goroutine. 192 done <- true 193 <-done // wait for the goroutine to exit. 194 }() 195 196 for _, d := range []int{1, 4, 16, 32} { 197 endpoint := fmt.Sprintf("/debug/pprof/mutex?seconds=%d", d) 198 p, err := query(endpoint) 199 if err != nil { 200 t.Fatalf("failed to query %q: %v", endpoint, err) 201 } 202 if !seen(p, "mutexHog1") && seen(p, "mutexHog2") && p.DurationNanos > 0 { 203 break // pass 204 } 205 if d == 32 { 206 t.Errorf("want mutexHog2 but no mutexHog1 in the profile, and non-zero p.DurationNanos, got %v", p) 207 } 208 } 209 p, err = query("/debug/pprof/mutex") 210 if err != nil { 211 t.Fatalf("failed to query mutex profile: %v", err) 212 } 213 if !seen(p, "mutexHog1") || !seen(p, "mutexHog2") { 214 t.Errorf("want both mutexHog1 and mutexHog2 in the profile, got %v", p) 215 } 216 } 217 218 var srv = httptest.NewServer(nil) 219 220 func query(endpoint string) (*profile.Profile, error) { 221 url := srv.URL + endpoint 222 r, err := http.Get(url) 223 if err != nil { 224 return nil, fmt.Errorf("failed to fetch %q: %v", url, err) 225 } 226 if r.StatusCode != http.StatusOK { 227 return nil, fmt.Errorf("failed to fetch %q: %v", url, r.Status) 228 } 229 230 b, err := io.ReadAll(r.Body) 231 r.Body.Close() 232 if err != nil { 233 return nil, fmt.Errorf("failed to read and parse the result from %q: %v", url, err) 234 } 235 return profile.Parse(bytes.NewBuffer(b)) 236 } 237 238 // seen returns true if the profile includes samples whose stacks include 239 // the specified function name (fname). 240 func seen(p *profile.Profile, fname string) bool { 241 locIDs := map[*profile.Location]bool{} 242 for _, loc := range p.Location { 243 for _, l := range loc.Line { 244 if strings.Contains(l.Function.Name, fname) { 245 locIDs[loc] = true 246 break 247 } 248 } 249 } 250 for _, sample := range p.Sample { 251 for _, loc := range sample.Location { 252 if locIDs[loc] { 253 return true 254 } 255 } 256 } 257 return false 258 }