2018-03-23 13:58:40 +00:00
|
|
|
package profiling
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
hpprof "net/http/pprof"
|
2023-01-13 17:12:46 +00:00
|
|
|
"time"
|
2018-03-23 13:58:40 +00:00
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
"github.com/status-im/status-go/common"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2018-03-23 13:58:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Profiler runs and controls a HTTP pprof interface.
|
|
|
|
type Profiler struct {
|
|
|
|
server *http.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProfiler creates an instance of the profiler with
|
|
|
|
// the given port.
|
|
|
|
func NewProfiler(port int) *Profiler {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/debug/pprof/", hpprof.Index)
|
|
|
|
mux.HandleFunc("/debug/pprof/cmdline", hpprof.Cmdline)
|
|
|
|
mux.HandleFunc("/debug/pprof/profile", hpprof.Profile)
|
|
|
|
mux.HandleFunc("/debug/pprof/symbol", hpprof.Symbol)
|
|
|
|
mux.HandleFunc("/debug/pprof/trace", hpprof.Trace)
|
|
|
|
p := Profiler{
|
|
|
|
server: &http.Server{
|
2023-01-13 17:12:46 +00:00
|
|
|
Addr: fmt.Sprintf(":%d", port),
|
|
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
|
|
Handler: mux,
|
2018-03-23 13:58:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go starts the HTTP pprof in the background.
|
|
|
|
func (p *Profiler) Go() {
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("debug server stopped", zap.Error(p.server.ListenAndServe()))
|
2018-03-23 13:58:40 +00:00
|
|
|
}()
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("debug server started")
|
2018-03-23 13:58:40 +00:00
|
|
|
}
|