Source file src/net/http/pprof/testdata/delta_mutex.go

     1  // Copyright 2023 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  // This binary collects a 1s delta mutex profile and dumps it to os.Stdout.
     6  //
     7  // This is in a subprocess because we want the base mutex profile to be empty
     8  // (as a regression test for https://go.dev/issue/64566) and the only way to
     9  // force reset the profile is to create a new subprocess.
    10  //
    11  // This manually collects the HTTP response and dumps to stdout in order to
    12  // avoid any flakiness around port selection for a real HTTP server.
    13  package main
    14  
    15  import (
    16  	"bytes"
    17  	"fmt"
    18  	"log"
    19  	"net/http"
    20  	"net/http/pprof"
    21  	"net/http/httptest"
    22  	"runtime"
    23  )
    24  
    25  func main() {
    26  	// Disable the mutex profiler. This is the default, but that default is
    27  	// load-bearing for this test, which needs the base profile to be empty.
    28  	runtime.SetMutexProfileFraction(0)
    29  
    30  	h := pprof.Handler("mutex")
    31  
    32  	req := httptest.NewRequest("GET", "/debug/pprof/mutex?seconds=1", nil)
    33  	rec := httptest.NewRecorder()
    34  	rec.Body = new(bytes.Buffer)
    35  
    36  	h.ServeHTTP(rec, req)
    37  	resp := rec.Result()
    38  	if resp.StatusCode != http.StatusOK {
    39  		log.Fatalf("Request failed: %s\n%s", resp.Status, rec.Body)
    40  	}
    41  
    42  	fmt.Print(rec.Body)
    43  }
    44  

View as plain text