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