diff --git a/cmd/preflight/cmd/check_container.go b/cmd/preflight/cmd/check_container.go index e30c0e4c..01e3d588 100644 --- a/cmd/preflight/cmd/check_container.go +++ b/cmd/preflight/cmd/check_container.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" rt "runtime" + "runtime/pprof" "slices" "strings" @@ -89,6 +90,9 @@ func checkContainerCmd(runpreflight runPreflight) *cobra.Command { flags.String("platform", rt.GOARCH, "Architecture of image to pull. Defaults to runtime platform.") _ = viper.BindPFlag("platform", flags.Lookup("platform")) + _ = viper.BindEnv("cpuprofile") + _ = viper.BindEnv("memprofile") + return checkContainerCmd } @@ -101,6 +105,20 @@ func checkContainerRunE(cmd *cobra.Command, args []string, runpreflight runPrefl } logger.Info("certification library version", "version", version.Version.String()) + if viper.Instance().IsSet("cpuprofile") { + f, err := os.Create(viper.Instance().GetString("cpuprofile")) + if err != nil { + logger.Error(err, "could not create CPU profile") + return err + } + defer f.Close() + + if err := pprof.StartCPUProfile(f); err != nil { + logger.Error(err, "could not start CPU profile") + } + defer pprof.StopCPUProfile() + } + containerImage := args[0] // Render the Viper configuration as a runtime.Config @@ -194,6 +212,18 @@ func checkContainerRunE(cmd *cobra.Command, args []string, runpreflight runPrefl } } + if viper.Instance().IsSet("memprofile") { + f, err := os.Create(viper.Instance().GetString("memprofile")) + if err != nil { + logger.Error(err, "could not create memory profile") + } + defer f.Close() + + rt.GC() // get up-to-date statistics + if err := pprof.Lookup("allocs").WriteTo(f, 0); err != nil { + logger.Error(err, "could not start memory profile") + } + } return nil }