math-optim/profile.go
leo adfba66f38
All checks were successful
continuous-integration/drone/push Build is passing
mv most of profiling logic to profile.go
2023-02-24 16:00:00 +01:00

47 lines
897 B
Go

// Copyright 2023 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"os"
"runtime/pprof"
)
// profileCPU checks whether CPU profiling was requested and if so, enables it.
// NOTE: deferred pprof.StopCPUProfile() needs to be called at the call site of
// this function, otherwise the profiling would end when this function goes out
// of scope.
func profileCPU(prof *string) error {
if *prof != "" {
f, err := os.Create(*prof)
if err != nil {
return err
}
err = pprof.StartCPUProfile(f)
if err != nil {
return err
}
}
return nil
}
// profileMem checks if memory profiling was requested and if so, enables it.
func profileMem(prof *string) error {
if *prof != "" {
f, err := os.Create(*prof)
if err != nil {
return err
}
err = pprof.WriteHeapProfile(f)
if err != nil {
return err
}
}
return nil
}