From 6f1e38f2ceb0166e50ba9c20489e82b58c2bb8f6 Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Tue, 12 Sep 2023 14:43:52 +0200 Subject: [PATCH] add flag to mox to store execution trace, similar to cpu/memory profiling useful for performance testing --- main.go | 6 +++++- profile.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 10ec02c..3eea02f 100644 --- a/main.go +++ b/main.go @@ -407,9 +407,10 @@ func main() { flag.BoolVar(&pedantic, "pedantic", false, "protocol violations result in errors instead of accepting/working around them") flag.BoolVar(&store.CheckConsistencyOnClose, "checkconsistency", false, "dangerous option for testing only, enables data checks that abort/panic when inconsistencies are found") - var cpuprofile, memprofile string + var cpuprofile, memprofile, tracefile string flag.StringVar(&cpuprofile, "cpuprof", "", "store cpu profile to file") flag.StringVar(&memprofile, "memprof", "", "store mem profile to file") + flag.StringVar(&tracefile, "trace", "", "store execution trace to file") flag.Usage = func() { usage(cmds, false) } flag.Parse() @@ -418,6 +419,9 @@ func main() { usage(cmds, false) } + if tracefile != "" { + defer traceExecution(tracefile)() + } defer profile(cpuprofile, memprofile)() if pedantic { diff --git a/profile.go b/profile.go index 4441c5a..251e37d 100644 --- a/profile.go +++ b/profile.go @@ -5,6 +5,7 @@ import ( "os" "runtime" "runtime/pprof" + "runtime/trace" ) func memprofile(mempath string) { @@ -43,3 +44,14 @@ func profile(cpupath, mempath string) func() { memprofile(mempath) } } + +func traceExecution(path string) func() { + f, err := os.Create(path) + xcheckf(err, "create trace file") + trace.Start(f) + return func() { + trace.Stop() + err := f.Close() + xcheckf(err, "close trace file") + } +}