github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/main_profile.go (about)

     1  //+build profile
     2  
     3  package main
     4  
     5  import (
     6  	"flag"
     7  	"log"
     8  	"os"
     9  	"runtime"
    10  	"runtime/pprof"
    11  )
    12  
    13  func main() {
    14  	cmd := new(luaCmd)
    15  	cmd.setFlags()
    16  	cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
    17  	memprofile := flag.String("memprofile", "", "write memory profile to `file`")
    18  	flag.Parse()
    19  
    20  	if *cpuprofile != "" {
    21  		f, err := os.Create(*cpuprofile)
    22  		if err != nil {
    23  			log.Fatal("could not create CPU profile: ", err)
    24  		}
    25  		defer f.Close() // error handling omitted for example
    26  		if err := pprof.StartCPUProfile(f); err != nil {
    27  			log.Fatal("could not start CPU profile: ", err)
    28  		}
    29  		defer pprof.StopCPUProfile()
    30  	}
    31  
    32  	cmd.run()
    33  
    34  	if *memprofile != "" {
    35  		f, err := os.Create(*memprofile)
    36  		if err != nil {
    37  			log.Fatal("could not create memory profile: ", err)
    38  		}
    39  		defer f.Close() // error handling omitted for example
    40  		runtime.GC()    // get up-to-date statistics
    41  		if err := pprof.WriteHeapProfile(f); err != nil {
    42  			log.Fatal("could not write memory profile: ", err)
    43  		}
    44  	}
    45  }