From 8e58e25e2b191547a8fe97e167951c277438dae2 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 22 Dec 2021 20:45:53 +0800 Subject: [PATCH] change: update logs --- docs/CHANGELOG.md | 2 ++ hrp/cmd/root.go | 2 +- internal/boomer/boomer.go | 20 ++++++++++---------- internal/boomer/output.go | 10 +++++----- internal/boomer/runner.go | 5 +++-- internal/boomer/utils.go | 13 +++++++------ 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 4612721f..506ef68c 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,8 @@ - feat: support `--continue-on-failure` flag to continue running next step when failure occurs, default to failfast - refactor: fork [boomer] as sub module - feat: report GA events with version +- feat: run load test with the given limit and burst as rate limiter +- change: update API models ## v0.2.2 (2021-12-07) diff --git a/hrp/cmd/root.go b/hrp/cmd/root.go index dea583a8..3025d9cd 100644 --- a/hrp/cmd/root.go +++ b/hrp/cmd/root.go @@ -49,7 +49,7 @@ func Execute() { func setLogLevel(level string) { level = strings.ToUpper(level) - log.Info().Msgf("Set log level to %s", level) + log.Info().Str("level", level).Msg("Set log level") switch level { case "DEBUG": zerolog.SetGlobalLevel(zerolog.DebugLevel) diff --git a/internal/boomer/boomer.go b/internal/boomer/boomer.go index 1a9c7e3f..7bc83664 100644 --- a/internal/boomer/boomer.go +++ b/internal/boomer/boomer.go @@ -1,9 +1,10 @@ package boomer import ( - "log" "math" "time" + + "github.com/rs/zerolog/log" ) // A Boomer is used to run tasks. @@ -36,20 +37,19 @@ func (b *Boomer) SetRateLimiter(maxRPS int64, requestIncreaseRate string) { var rateLimiter RateLimiter var err error if requestIncreaseRate != "-1" { - if maxRPS > 0 { - log.Println("The max RPS that boomer may generate is limited to", maxRPS, "with a increase rate", requestIncreaseRate) - rateLimiter, err = NewRampUpRateLimiter(maxRPS, requestIncreaseRate, time.Second) - } else { - log.Println("The max RPS that boomer may generate is limited by a increase rate", requestIncreaseRate) - rateLimiter, err = NewRampUpRateLimiter(math.MaxInt64, requestIncreaseRate, time.Second) + if maxRPS <= 0 { + maxRPS = math.MaxInt64 } + log.Warn().Int64("maxRPS", maxRPS).Str("increaseRate", requestIncreaseRate).Msg("set ramp up rate limiter") + rateLimiter, err = NewRampUpRateLimiter(math.MaxInt64, requestIncreaseRate, time.Second) } else { if maxRPS > 0 { - log.Println("The max RPS that boomer may generate is limited to", maxRPS) + log.Warn().Int64("maxRPS", maxRPS).Msg("set stable rate limiter") rateLimiter = NewStableRateLimiter(maxRPS, time.Second) } } if err != nil { + log.Error().Err(err).Msg("failed to create rate limiter") return } b.rateLimiter = rateLimiter @@ -77,13 +77,13 @@ func (b *Boomer) Run(tasks ...*Task) { if b.cpuProfile != "" { err := startCPUProfile(b.cpuProfile, b.cpuProfileDuration) if err != nil { - log.Printf("Error starting cpu profiling, %v", err) + log.Error().Err(err).Msg("failed to start cpu profiling") } } if b.memoryProfile != "" { err := startMemoryProfile(b.memoryProfile, b.memoryProfileDuration) if err != nil { - log.Printf("Error starting memory profiling, %v", err) + log.Error().Err(err).Msg("failed to start memory profiling") } } diff --git a/internal/boomer/output.go b/internal/boomer/output.go index ae2308fa..80214289 100644 --- a/internal/boomer/output.go +++ b/internal/boomer/output.go @@ -3,7 +3,6 @@ package boomer import ( "encoding/json" "fmt" - "log" "os" "sort" "strconv" @@ -13,6 +12,7 @@ import ( "github.com/olekukonko/tablewriter" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/push" + "github.com/rs/zerolog/log" ) // Output is primarily responsible for printing test results to different destinations @@ -120,7 +120,7 @@ func (o *ConsoleOutput) OnStop() { func (o *ConsoleOutput) OnEvent(data map[string]interface{}) { output, err := convertData(data) if err != nil { - log.Println(fmt.Sprintf("convert data error: %v", err)) + log.Error().Err(err).Msg("failed to convert data") return } @@ -380,7 +380,7 @@ type PrometheusPusherOutput struct { // OnStart will register all prometheus metric collectors func (o *PrometheusPusherOutput) OnStart() { - log.Println("register prometheus metric collectors") + log.Info().Msg("register prometheus metric collectors") registry := prometheus.NewRegistry() registry.MustRegister( // gauge vectors for requests @@ -412,7 +412,7 @@ func (o *PrometheusPusherOutput) OnStop() { func (o *PrometheusPusherOutput) OnEvent(data map[string]interface{}) { output, err := convertData(data) if err != nil { - log.Println(fmt.Sprintf("convert data error: %v", err)) + log.Error().Err(err).Msg("failed to convert data") return } @@ -444,6 +444,6 @@ func (o *PrometheusPusherOutput) OnEvent(data map[string]interface{}) { } if err := o.pusher.Push(); err != nil { - log.Println(fmt.Sprintf("Could not push to Pushgateway: error: %v", err)) + log.Error().Err(err).Msg("push to Pushgateway failed") } } diff --git a/internal/boomer/runner.go b/internal/boomer/runner.go index 61e28ad1..d3f11801 100644 --- a/internal/boomer/runner.go +++ b/internal/boomer/runner.go @@ -2,13 +2,14 @@ package boomer import ( "fmt" - "log" "math/rand" "os" "runtime/debug" "sync" "sync/atomic" "time" + + "github.com/rs/zerolog/log" ) const ( @@ -116,7 +117,7 @@ func (r *runner) outputOnStop() { } func (r *runner) spawnWorkers(spawnCount int, quit chan bool, spawnCompleteFunc func()) { - log.Println("Spawning", spawnCount, "clients immediately") + log.Info().Int("spawnCount", spawnCount).Msg("Spawning clients immediately") for i := 1; i <= spawnCount; i++ { select { diff --git a/internal/boomer/utils.go b/internal/boomer/utils.go index a4ca48c7..7d7bfe6f 100644 --- a/internal/boomer/utils.go +++ b/internal/boomer/utils.go @@ -4,11 +4,12 @@ import ( "crypto/md5" "fmt" "io" - "log" "math" "os" "runtime/pprof" "time" + + "github.com/rs/zerolog/log" ) func round(val float64, roundOn float64, places int) (newVal float64) { @@ -41,14 +42,14 @@ func startMemoryProfile(file string, duration time.Duration) (err error) { return err } - log.Println("Start memory profiling for", duration) + log.Info().Dur("duration", duration).Msg("Start memory profiling") time.AfterFunc(duration, func() { err = pprof.WriteHeapProfile(f) if err != nil { - log.Println(err) + log.Error().Err(err).Msg("failed to write memory profile") } f.Close() - log.Println("Stop memory profiling after", duration) + log.Info().Dur("duration", duration).Msg("Stop memory profiling") }) return nil } @@ -60,7 +61,7 @@ func startCPUProfile(file string, duration time.Duration) (err error) { return err } - log.Println("Start cpu profiling for", duration) + log.Info().Dur("duration", duration).Msg("Start CPU profiling") err = pprof.StartCPUProfile(f) if err != nil { f.Close() @@ -70,7 +71,7 @@ func startCPUProfile(file string, duration time.Duration) (err error) { time.AfterFunc(duration, func() { pprof.StopCPUProfile() f.Close() - log.Println("Stop CPU profiling after", duration) + log.Info().Dur("duration", duration).Msg("Stop CPU profiling") }) return nil }