From 31f36fe86fd1afe267ece75325a4c53c1d6d4496 Mon Sep 17 00:00:00 2001 From: Hari Date: Thu, 28 Sep 2023 16:26:50 +0530 Subject: [PATCH] test: add a flag to generate the CPU profile for the transform command (#1093) Signed-off-by: Harikrishnan Balagopal --- cmd/flags.go | 2 ++ cmd/transform.go | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/cmd/flags.go b/cmd/flags.go index 5dfcf79f4..d25e5a0b4 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -25,6 +25,8 @@ const ( nameFlag = "name" // planFlag is the name of the flag that contains the path to the plan file planFlag = "plan" + // profileFlag is the name of the flag that contains the path where the CPU profile file should be generated + profileFlag = "profile" // ignoreEnvFlag is the name of the flag that tells us whether to use data collected from the local machine ignoreEnvFlag = "ignore-env" // qaSkipFlag is the name of the flag that lets you skip all the question answers diff --git a/cmd/transform.go b/cmd/transform.go index 2e8148458..d5a71bc7c 100644 --- a/cmd/transform.go +++ b/cmd/transform.go @@ -21,6 +21,7 @@ import ( "os" "os/signal" "path/filepath" + "runtime/pprof" "github.com/konveyor/move2kube/common" "github.com/konveyor/move2kube/common/download" @@ -40,6 +41,8 @@ type transformFlags struct { disableLocalExecution bool // planfile is contains the path to the plan file planfile string + // profilepath contains the path to the CPU profile file + profilepath string // outpath contains the path to the output folder outpath string // SourceFlag contains path to the source folder @@ -56,6 +59,15 @@ type transformFlags struct { } func transformHandler(cmd *cobra.Command, flags transformFlags) { + if flags.profilepath != "" { + if f, err := os.Create(flags.profilepath); err != nil { + panic(err) + } else if err := pprof.StartCPUProfile(f); err != nil { + panic(err) + } + defer pprof.StopCPUProfile() + } + ctx, cancel := context.WithCancel(cmd.Context()) logrus.AddHook(common.NewCleanupHook(cancel)) logrus.AddHook(common.NewCleanupHook(lib.Destroy)) @@ -223,6 +235,7 @@ func GetTransformCommand() *cobra.Command { } // Basic options + transformCmd.Flags().StringVar(&flags.profilepath, profileFlag, "", "Path where the CPU profile file should be generated. By default we don't profile.") transformCmd.Flags().StringVarP(&flags.planfile, planFlag, "p", common.DefaultPlanFile, "Specify a plan file to execute.") transformCmd.Flags().BoolVar(&flags.overwrite, overwriteFlag, false, "Overwrite the output directory if it exists. By default we don't overwrite.") transformCmd.Flags().StringVarP(&flags.srcpath, sourceFlag, "s", "", "Specify source directory or a git url (see https://move2kube.konveyor.io/concepts/git-support) to transform. If you already have a m2k.plan then this will override the sourceDir value specified in that plan.")