This commit is contained in:
Jonas Böwer 2025-09-24 08:54:07 +02:00 committed by GitHub
commit b1bb5b596d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"os/exec"
"runtime"
"runtime/pprof"
"runtime/trace"
"strings"
"syscall"
@ -40,6 +41,8 @@ type globalFlags struct {
CPUProfile string
cpuProfileFile *os.File
MemoryProfile string
TraceProfile string
traceProfileFile *os.File
UserShortNameAliasConfPath string
CgroupManager string
}
@ -109,6 +112,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&globalFlagResults.LogLevel, logLevel, "warn", `the log level to be used, one of "trace", "debug", "info", "warn", "error", "fatal", or "panic"`)
rootCmd.PersistentFlags().StringVar(&globalFlagResults.CPUProfile, "cpu-profile", "", "`file` to write CPU profile")
rootCmd.PersistentFlags().StringVar(&globalFlagResults.MemoryProfile, "memory-profile", "", "`file` to write memory profile")
rootCmd.PersistentFlags().StringVar(&globalFlagResults.TraceProfile, "trace-profile", "", "`file` to write trace profile")
if err := rootCmd.PersistentFlags().MarkHidden("cpu-profile"); err != nil {
logrus.Fatalf("unable to mark cpu-profile flag as hidden: %v", err)
@ -122,6 +126,9 @@ func init() {
if err := rootCmd.PersistentFlags().MarkHidden("memory-profile"); err != nil {
logrus.Fatalf("unable to mark memory-profile flag as hidden: %v", err)
}
if err := rootCmd.PersistentFlags().MarkHidden("trace-profile"); err != nil {
logrus.Fatalf("unable to mark trace-profile flag as hidden: %v", err)
}
}
func initConfig() {
@ -161,6 +168,16 @@ func before(cmd *cobra.Command) error {
}
}
if globalFlagResults.TraceProfile != "" {
globalFlagResults.traceProfileFile, err = os.Create(globalFlagResults.TraceProfile)
if err != nil {
logrus.Fatalf("could not create trace output file %s: %v", globalFlagResults.TraceProfile, err)
}
if err := trace.Start(globalFlagResults.traceProfileFile); err != nil {
logrus.Fatalf("could not start trace: %v", err)
}
}
defaultContainerConfig, err := config.Default()
if err != nil {
return err
@ -223,6 +240,10 @@ func after(cmd *cobra.Command) error {
logrus.Fatalf("could not write memory profile %s: %v", globalFlagResults.MemoryProfile, err)
}
}
if globalFlagResults.TraceProfile != "" {
trace.Stop()
globalFlagResults.traceProfileFile.Close()
}
return nil
}

33
tests/profiling.bats Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env bats
load helpers
@test "trace-profile flag creates a valid Go trace" {
tmpfile="$TEST_SCRATCH_DIR/buildah-trace.$RANDOM.$RANDOM"
run_buildah info --trace-profile="$tmpfile"
assert "$status" -eq 0 "buildah info with --trace-profile should succeed"
[ -s "$tmpfile" ] || die "trace profile should not be empty"
run go tool trace -pprof=net "$tmpfile"
assert "$status" -eq 0 "go tool trace should succeed"
rm -f "$tmpfile"
}
@test "cpu-profile flag creates a valid CPU profile" {
tmpfile="$TEST_SCRATCH_DIR/buildah-cpu.$RANDOM.$RANDOM"
run_buildah info --cpu-profile="$tmpfile"
assert "$status" -eq 0 "buildah info with --cpu-profile should succeed"
[ -s "$tmpfile" ] || die "CPU profile should not be empty"
run go tool pprof -top "$tmpfile"
assert "$status" -eq 0 "go tool pprof should succeed on CPU profile"
rm -f "$tmpfile"
}
@test "memory-profile flag creates a valid memory profile" {
tmpfile="$TEST_SCRATCH_DIR/buildah-mem.$RANDOM.$RANDOM"
run_buildah info --memory-profile="$tmpfile"
assert "$status" -eq 0 "buildah info with --memory-profile should succeed"
[ -s "$tmpfile" ] || die "memory profile should not be empty"
run go tool pprof -top "$tmpfile"
assert "$status" -eq 0 "go tool pprof should succeed on memory profile"
rm -f "$tmpfile"
}