run.go: add a way to profile program's mem usage
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
to prevent defer-after-exit, rework the way run() is done: * make run return an error * return nil if all went well * return err if something went south special-case ErrNoAlgoSelected in main(). adjust run_test.go to these new realities, also.
This commit is contained in:
parent
a74ea8c8e4
commit
cf61dd4795
13
main.go
13
main.go
@ -3,6 +3,17 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
run()
|
err := run()
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() == "ErrNoAlgoSelected" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
28
run.go
28
run.go
@ -4,6 +4,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -17,7 +18,9 @@ import (
|
|||||||
var version = "development"
|
var version = "development"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
// ref: https://go.dev/blog/pprof.
|
||||||
|
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
|
||||||
|
memprofile = flag.String("memprofile", "", "write memory profile to this file")
|
||||||
|
|
||||||
doPrint = flag.Bool("printreport", true, "print report.tex to console")
|
doPrint = flag.Bool("printreport", true, "print report.tex to console")
|
||||||
generate = flag.Bool("generate", true, "run algos and generate plot pics/statistical tables (anew)")
|
generate = flag.Bool("generate", true, "run algos and generate plot pics/statistical tables (anew)")
|
||||||
@ -33,7 +36,8 @@ var (
|
|||||||
c2SOMAT3A = flag.Bool("c2somat3a", false, "run CEC2020 version of the SOMA Team-to-Team Adaptive (T3A)")
|
c2SOMAT3A = flag.Bool("c2somat3a", false, "run CEC2020 version of the SOMA Team-to-Team Adaptive (T3A)")
|
||||||
)
|
)
|
||||||
|
|
||||||
func run() {
|
// nolint: gocognit
|
||||||
|
func run() error {
|
||||||
log.Println("starting math-optim version", "'"+version+"'")
|
log.Println("starting math-optim version", "'"+version+"'")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@ -41,12 +45,12 @@ func run() {
|
|||||||
if *cpuprofile != "" {
|
if *cpuprofile != "" {
|
||||||
f, err := os.Create(*cpuprofile)
|
f, err := os.Create(*cpuprofile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pprof.StartCPUProfile(f)
|
err = pprof.StartCPUProfile(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer pprof.StopCPUProfile()
|
defer pprof.StopCPUProfile()
|
||||||
@ -56,7 +60,7 @@ func run() {
|
|||||||
if !*jDE && !*c2jDE && !*c2SOMAT3A && !*sHC && !*rS {
|
if !*jDE && !*c2jDE && !*c2SOMAT3A && !*sHC && !*rS {
|
||||||
log.Println("at least one algo needs to be specified, exiting...")
|
log.Println("at least one algo needs to be specified, exiting...")
|
||||||
|
|
||||||
return
|
return errors.New("ErrNoAlgoSelected")
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
@ -116,6 +120,20 @@ func run() {
|
|||||||
|
|
||||||
report.SaveAndPrint(*doPrint)
|
report.SaveAndPrint(*doPrint)
|
||||||
|
|
||||||
|
if *memprofile != "" {
|
||||||
|
f, err := os.Create(*memprofile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = pprof.WriteHeapProfile(f)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Println("looks like we're done")
|
log.Println("looks like we're done")
|
||||||
log.Println("run an equivalent of `pdflatex -clean -shell-escape -interaction=nonstopmode ./report.tex` to get a pdf")
|
log.Println("run an equivalent of `pdflatex -clean -shell-escape -interaction=nonstopmode ./report.tex` to get a pdf")
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -23,5 +23,12 @@ func TestRun(t *testing.T) {
|
|||||||
t.Errorf("failed to not run jDE: %q", err)
|
t.Errorf("failed to not run jDE: %q", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
run()
|
err := run()
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() == "ErrNoAlgoSelected" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user