run.go: add a way to profile program's mem usage
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:
leo 2023-02-24 15:37:59 +01:00
parent a74ea8c8e4
commit cf61dd4795
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
3 changed files with 43 additions and 7 deletions

13
main.go
View File

@ -3,6 +3,17 @@
package main
import (
"log"
)
func main() {
run()
err := run()
if err != nil {
if err.Error() == "ErrNoAlgoSelected" {
return
}
log.Fatal(err)
}
}

28
run.go
View File

@ -4,6 +4,7 @@
package main
import (
"errors"
"flag"
"log"
"os"
@ -17,7 +18,9 @@
var version = "development"
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")
generate = flag.Bool("generate", true, "run algos and generate plot pics/statistical tables (anew)")
@ -33,7 +36,8 @@
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+"'")
flag.Parse()
@ -41,12 +45,12 @@ func run() {
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
return err
}
err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatal(err)
return err
}
defer pprof.StopCPUProfile()
@ -56,7 +60,7 @@ func run() {
if !*jDE && !*c2jDE && !*c2SOMAT3A && !*sHC && !*rS {
log.Println("at least one algo needs to be specified, exiting...")
return
return errors.New("ErrNoAlgoSelected")
}
var wg sync.WaitGroup
@ -116,6 +120,20 @@ func run() {
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("run an equivalent of `pdflatex -clean -shell-escape -interaction=nonstopmode ./report.tex` to get a pdf")
return nil
}

View File

@ -23,5 +23,12 @@ func TestRun(t *testing.T) {
t.Errorf("failed to not run jDE: %q", err)
}
run()
err := run()
if err != nil {
if err.Error() == "ErrNoAlgoSelected" {
return
}
t.Error(err)
}
}