diff --git a/internal/telemetry/export/export.go b/internal/telemetry/export/export.go index a48c6f1f09e..6ef33020980 100644 --- a/internal/telemetry/export/export.go +++ b/internal/telemetry/export/export.go @@ -35,6 +35,12 @@ var ( exporter = LogWriter(os.Stderr, true) ) +func SetExporter(e Exporter) { + exporterMu.Lock() + defer exporterMu.Unlock() + exporter = e +} + func AddExporters(e ...Exporter) { exporterMu.Lock() defer exporterMu.Unlock() diff --git a/internal/telemetry/log/bench_test.go b/internal/telemetry/log/bench_test.go index dc77bd6a1e6..5eb0b33bca0 100644 --- a/internal/telemetry/log/bench_test.go +++ b/internal/telemetry/log/bench_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "golang.org/x/tools/internal/telemetry/export" tellog "golang.org/x/tools/internal/telemetry/log" "golang.org/x/tools/internal/telemetry/tag" ) @@ -20,14 +21,14 @@ func (nw *noopWriter) Write(b []byte) (int, error) { return len(b), nil } -func A(a int) int { +func A(ctx context.Context, a int) int { if a > 0 { _ = 10 * 12 } - return B("Called from A") + return B(ctx, "Called from A") } -func B(b string) int { +func B(ctx context.Context, b string) int { b = strings.ToUpper(b) if len(b) > 1024 { b = strings.ToLower(b) @@ -54,16 +55,16 @@ func B_log(ctx context.Context, b string) int { return len(b) } -func A_log_stdlib(a int) int { +func A_log_stdlib(ctx context.Context, a int) int { if a > 0 { stdlog.Printf("a > 0 where a=%d", a) _ = 10 * 12 } stdlog.Print("calling b") - return B_log_stdlib("Called from A") + return B_log_stdlib(ctx, "Called from A") } -func B_log_stdlib(b string) int { +func B_log_stdlib(ctx context.Context, b string) int { b = strings.ToUpper(b) stdlog.Printf("b uppercased, so lowercased where len_b=%d", len(b)) if len(b) > 1024 { @@ -73,12 +74,15 @@ func B_log_stdlib(b string) int { return len(b) } -func BenchmarkNoTracingNoMetricsNoLogging(b *testing.B) { +var values = []int{0, 10, 20, 100, 1000} + +func BenchmarkBaseline(b *testing.B) { + ctx := context.Background() b.ReportAllocs() - values := []int{0, 10, 20, 100, 1000} + b.ResetTimer() for i := 0; i < b.N; i++ { for _, value := range values { - if g := A(value); g <= 0 { + if g := A(ctx, value); g <= 0 { b.Fatalf("Unexpected got g(%d) <= 0", g) } } @@ -86,11 +90,27 @@ func BenchmarkNoTracingNoMetricsNoLogging(b *testing.B) { } func BenchmarkLoggingNoExporter(b *testing.B) { + ctx := context.Background() + export.SetExporter(export.Null()) b.ReportAllocs() - values := []int{0, 10, 20, 100, 1000} + b.ResetTimer() for i := 0; i < b.N; i++ { for _, value := range values { - if g := A_log(context.TODO(), value); g <= 0 { + if g := A_log(ctx, value); g <= 0 { + b.Fatalf("Unexpected got g(%d) <= 0", g) + } + } + } +} + +func BenchmarkLogging(b *testing.B) { + ctx := context.Background() + export.SetExporter(export.LogWriter(new(noopWriter), false)) + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + for _, value := range values { + if g := A_log(ctx, value); g <= 0 { b.Fatalf("Unexpected got g(%d) <= 0", g) } } @@ -98,11 +118,12 @@ func BenchmarkLoggingNoExporter(b *testing.B) { } func BenchmarkLoggingStdlib(b *testing.B) { + ctx := context.Background() b.ReportAllocs() - values := []int{0, 10, 20, 100, 1000} + b.ResetTimer() for i := 0; i < b.N; i++ { for _, value := range values { - if g := A_log_stdlib(value); g <= 0 { + if g := A_log_stdlib(ctx, value); g <= 0 { b.Fatalf("Unexpected got g(%d) <= 0", g) } }