From 7b5c777075f4d17d440e9cea219281b1675d8193 Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Wed, 13 Dec 2023 22:02:50 -0800 Subject: [PATCH 1/2] add -cpuprofile and -memprofile options to profile coroc compiler Signed-off-by: Achille Roussel --- compiler/cmd/coroc/main.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/compiler/cmd/coroc/main.go b/compiler/cmd/coroc/main.go index bb7064b..06929f2 100644 --- a/compiler/cmd/coroc/main.go +++ b/compiler/cmd/coroc/main.go @@ -7,6 +7,7 @@ import ( "log" "os" "runtime/debug" + "runtime/pprof" "github.com/stealthrocket/coroutine/compiler" ) @@ -26,6 +27,8 @@ OPTIONS: var ( showVersion bool onlyListFiles bool + cpuProfile string + memProfile string ) func boolFlag(ptr *bool, short, long string) { @@ -45,6 +48,8 @@ func run() error { boolFlag(&showVersion, "v", "version") boolFlag(&onlyListFiles, "l", "list") + flag.StringVar(&cpuProfile, "cpuprofile", "", "") + flag.StringVar(&memProfile, "memprofile", "", "") flag.Parse() if showVersion { @@ -52,6 +57,27 @@ func run() error { return nil } + if memProfile != "" { + f, err := os.Create(memProfile) + if err != nil { + return err + } + defer f.Close() + defer pprof.WriteHeapProfile(f) + } + + if cpuProfile != "" { + f, err := os.Create(cpuProfile) + if err != nil { + return err + } + defer f.Close() + if err := pprof.StartCPUProfile(f); err != nil { + return err + } + defer pprof.StopCPUProfile() + } + path := flag.Arg(0) if path == "" { // If the compiler was invoked via go generate, the GOFILE From f300cce415af82fe407ca2ba0febad365393df58 Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Wed, 13 Dec 2023 22:05:42 -0800 Subject: [PATCH 2/2] add help message Signed-off-by: Achille Roussel --- compiler/cmd/coroc/main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/cmd/coroc/main.go b/compiler/cmd/coroc/main.go index 06929f2..e45c029 100644 --- a/compiler/cmd/coroc/main.go +++ b/compiler/cmd/coroc/main.go @@ -22,6 +22,10 @@ OPTIONS: -h, --help Show this help information -l, --list List all files that would be compiled -v, --version Show the compiler version + +ADVANCED OPTIONS: + -cpuprofile Write CPU profile to file + -memprofile Write memory profile to file ` var (