2021-09-23 09:34:58 +00:00
|
|
|
//go:build debug
|
2018-05-27 22:22:50 +00:00
|
|
|
// +build debug
|
|
|
|
|
2021-05-23 19:42:26 +00:00
|
|
|
package core
|
2017-12-29 04:16:20 +00:00
|
|
|
|
2022-04-17 17:02:25 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2018-06-21 15:32:16 +00:00
|
|
|
|
2022-04-17 17:02:25 +00:00
|
|
|
"github.com/gologme/log"
|
|
|
|
)
|
2019-01-27 13:33:32 +00:00
|
|
|
|
2018-06-21 15:32:16 +00:00
|
|
|
// Start the profiler in debug builds, if the required environment variable is set.
|
|
|
|
func init() {
|
|
|
|
envVarName := "PPROFLISTEN"
|
|
|
|
hostPort := os.Getenv(envVarName)
|
|
|
|
switch {
|
|
|
|
case hostPort == "":
|
2019-07-20 20:56:53 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "DEBUG: %s not set, profiler not started.\n", envVarName)
|
2018-06-21 15:32:16 +00:00
|
|
|
default:
|
2019-07-20 20:56:53 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "DEBUG: Starting pprof on %s\n", hostPort)
|
2018-06-21 15:32:16 +00:00
|
|
|
go func() { fmt.Println(http.ListenAndServe(hostPort, nil)) }()
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 22:22:50 +00:00
|
|
|
|
|
|
|
// Starts the function profiler. This is only supported when built with
|
|
|
|
// '-tags build'.
|
|
|
|
func StartProfiler(log *log.Logger) error {
|
|
|
|
runtime.SetBlockProfileRate(1)
|
|
|
|
go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }()
|
|
|
|
return nil
|
|
|
|
}
|