mirror of
https://github.com/golang/go
synced 2024-11-18 16:14:46 -07:00
internal/telemetry: add a benchmark with a null writer for comparison
Make all the benchmarks cleanly pass around the context to remove a non logging difference. Rename the non logging calls benchmark to Baseline Change-Id: I24a33b0a817df403fb43c53b5f097bc1e9418af4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/220520 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
This commit is contained in:
parent
daf22849ce
commit
a0ec867d51
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user