wip: migrate to mono-repo. SPN has already been moved to spn/
This commit is contained in:
41
service/core/base/profiling.go
Normal file
41
service/core/base/profiling.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime/pprof"
|
||||
)
|
||||
|
||||
var cpuProfile string
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&cpuProfile, "cpuprofile", "", "write cpu profile to `file`")
|
||||
}
|
||||
|
||||
func startProfiling() {
|
||||
if cpuProfile != "" {
|
||||
module.StartWorker("cpu profiler", cpuProfiler)
|
||||
}
|
||||
}
|
||||
|
||||
func cpuProfiler(ctx context.Context) error {
|
||||
f, err := os.Create(cpuProfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create CPU profile: %w", err)
|
||||
}
|
||||
if err := pprof.StartCPUProfile(f); err != nil {
|
||||
return fmt.Errorf("could not start CPU profile: %w", err)
|
||||
}
|
||||
|
||||
// wait for shutdown
|
||||
<-ctx.Done()
|
||||
|
||||
pprof.StopCPUProfile()
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to close CPU profile file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user