Skip to content

Commit

Permalink
add -cpuprofile and -memprofile options to profile coroc compiler (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
achille-roussel authored Dec 14, 2023
2 parents b4ab003 + f300cce commit 8fd653a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 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 @@ -21,11 +22,17 @@ 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 (
showVersion bool
onlyListFiles bool
cpuProfile string
memProfile string
)

func boolFlag(ptr *bool, short, long string) {
Expand All @@ -45,13 +52,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 8fd653a

Please sign in to comment.