Skip to content

Commit

Permalink
add -cpuprofile and -memprofile options to profile coroc compiler
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
  • Loading branch information
achille-roussel committed Dec 14, 2023
1 parent b4ab003 commit 7b5c777
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions compiler/cmd/coroc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"runtime/debug"
"runtime/pprof"

"github.com/stealthrocket/coroutine/compiler"
)
Expand All @@ -26,6 +27,8 @@ OPTIONS:
var (
showVersion bool
onlyListFiles bool
cpuProfile string
memProfile string
)

func boolFlag(ptr *bool, short, long string) {
Expand All @@ -45,13 +48,36 @@ func run() error {

boolFlag(&showVersion, "v", "version")
boolFlag(&onlyListFiles, "l", "list")
flag.StringVar(&cpuProfile, "cpuprofile", "", "")
flag.StringVar(&memProfile, "memprofile", "", "")
flag.Parse()

if showVersion {
fmt.Println(version())
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
Expand Down

0 comments on commit 7b5c777

Please sign in to comment.