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