Skip to content

Instantly share code, notes, and snippets.

@benmoss
Last active June 1, 2022 18:15
Show Gist options
  • Save benmoss/b4bee6eb96fe28ab414cf821e5768edd to your computer and use it in GitHub Desktop.
Save benmoss/b4bee6eb96fe28ab414cf821e5768edd to your computer and use it in GitHub Desktop.
diff --git a/cmd/imgpkg/imgpkg.go b/cmd/imgpkg/imgpkg.go
index 4d1ae1f..9e365f4 100644
--- a/cmd/imgpkg/imgpkg.go
+++ b/cmd/imgpkg/imgpkg.go
@@ -8,6 +8,8 @@ import (
"log"
"math/rand"
"os"
+ "runtime"
+ "runtime/pprof"
"time"
uierrs "github.com/cppforlife/go-cli-ui/errors"
@@ -19,6 +21,26 @@ func main() {
rand.Seed(time.Now().UTC().UnixNano())
log.SetOutput(ioutil.Discard)
+ {
+ f, err := os.Create("cpu.profile")
+ if err != nil {
+ log.Fatal("could not create CPU profile: ", err)
+ }
+ defer f.Close() // error handling omitted for example
+ if err := pprof.StartCPUProfile(f); err != nil {
+ log.Fatal("could not start CPU profile: ", err)
+ }
+ defer pprof.StopCPUProfile()
+ f, err = os.Create("mem.profile")
+ if err != nil {
+ log.Fatal("could not create memory profile: ", err)
+ }
+ defer f.Close() // error handling omitted for example
+ runtime.GC() // get up-to-date statistics
+ if err := pprof.WriteHeapProfile(f); err != nil {
+ log.Fatal("could not write memory profile: ", err)
+ }
+ }
// TODO logs
// TODO log flags used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment