github.com/status-im/status-go@v1.1.0/profiling/profiler.go (about)

     1  package profiling
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	hpprof "net/http/pprof"
     7  	"time"
     8  
     9  	"github.com/ethereum/go-ethereum/log"
    10  )
    11  
    12  // Profiler runs and controls a HTTP pprof interface.
    13  type Profiler struct {
    14  	server *http.Server
    15  }
    16  
    17  // NewProfiler creates an instance of the profiler with
    18  // the given port.
    19  func NewProfiler(port int) *Profiler {
    20  	mux := http.NewServeMux()
    21  	mux.HandleFunc("/debug/pprof/", hpprof.Index)
    22  	mux.HandleFunc("/debug/pprof/cmdline", hpprof.Cmdline)
    23  	mux.HandleFunc("/debug/pprof/profile", hpprof.Profile)
    24  	mux.HandleFunc("/debug/pprof/symbol", hpprof.Symbol)
    25  	mux.HandleFunc("/debug/pprof/trace", hpprof.Trace)
    26  	p := Profiler{
    27  		server: &http.Server{
    28  			Addr:              fmt.Sprintf(":%d", port),
    29  			ReadHeaderTimeout: 5 * time.Second,
    30  			Handler:           mux,
    31  		},
    32  	}
    33  	return &p
    34  }
    35  
    36  // Go starts the HTTP pprof in the background.
    37  func (p *Profiler) Go() {
    38  	go func() {
    39  		log.Info("debug server stopped", "err", p.server.ListenAndServe())
    40  	}()
    41  	log.Info("debug server started")
    42  }