Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert lib/benchmark to use slog #50301

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/bench/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func main() {
}

// Run Linear generator
ctx := context.Background()
results, err := benchmark.Run(
context.TODO(),
ctx,
linear,
"host",
"username",
Expand All @@ -59,7 +60,7 @@ func main() {

// Export latency profile
responseHistogram := results[0].Histogram
_, err = benchmark.ExportLatencyProfile("profiles/", responseHistogram, 1, 1.0)
_, err = benchmark.ExportLatencyProfile(ctx, "profiles/", responseHistogram, 1, 1.0)
if err != nil {
fmt.Println(err)
}
Expand Down
10 changes: 5 additions & 5 deletions lib/benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"path/filepath"
Expand All @@ -33,7 +34,6 @@ import (

"github.com/HdrHistogram/hdrhistogram-go"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"

"github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/observability/tracing"
Expand Down Expand Up @@ -102,7 +102,7 @@ func Run(ctx context.Context, lg *Linear, host, login, proxy string, suite Suite
signal.Notify(exitSignals, syscall.SIGTERM, syscall.SIGINT)
defer signal.Stop(exitSignals)
sig := <-exitSignals
logrus.Debugf("signal: %v", sig)
slog.DebugContext(ctx, "terminating benchmark due to signal", "signal", sig)
cancel()
}()
var results []Result
Expand Down Expand Up @@ -131,7 +131,7 @@ func Run(ctx context.Context, lg *Linear, host, login, proxy string, suite Suite
}

// ExportLatencyProfile exports the latency profile and returns the path as a string if no errors
func ExportLatencyProfile(path string, h *hdrhistogram.Histogram, ticks int32, valueScale float64) (string, error) {
func ExportLatencyProfile(ctx context.Context, path string, h *hdrhistogram.Histogram, ticks int32, valueScale float64) (string, error) {
timeStamp := time.Now().Format("2006-01-02_15:04:05")
suffix := fmt.Sprintf("latency_profile_%s.txt", timeStamp)
if path != "." {
Expand All @@ -147,7 +147,7 @@ func ExportLatencyProfile(path string, h *hdrhistogram.Histogram, ticks int32, v

if _, err := h.PercentilesPrint(fo, ticks, valueScale); err != nil {
if err := fo.Close(); err != nil {
logrus.WithError(err).Warningf("failed to close file")
slog.WarnContext(ctx, "failed to close latency profile file", "error", err)
}
return "", trace.Wrap(err)
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func (c *Config) Benchmark(ctx context.Context, tc *client.TeleportClient, suite
result.Duration = time.Since(start)
return result, nil
case <-statusTicker.C:
logrus.Infof("working... current observation count: %d", result.RequestsOriginated)
slog.InfoContext(ctx, "working...", "current_observation_count", result.RequestsOriginated)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tool/tsh/common/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3998,7 +3998,7 @@ func onBenchmark(cf *CLIConf, suite benchmark.Suite) error {
}
fmt.Fprintf(cf.Stdout(), "\n")
if cf.BenchExport {
path, err := benchmark.ExportLatencyProfile(cf.BenchExportPath, result.Histogram, cf.BenchTicks, cf.BenchValueScale)
path, err := benchmark.ExportLatencyProfile(cf.Context, cf.BenchExportPath, result.Histogram, cf.BenchTicks, cf.BenchValueScale)
if err != nil {
fmt.Fprintf(cf.Stderr(), "failed exporting latency profile: %s\n", utils.UserMessageFromError(err))
} else {
Expand Down
Loading