-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_bench_test.go
More file actions
182 lines (163 loc) · 4.91 KB
/
Copy pathstack_bench_test.go
File metadata and controls
182 lines (163 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package stack_test
import (
"context"
"errors"
"io"
"testing"
"time"
"github.com/themakers/stack"
"github.com/themakers/stack/stack_backend"
"github.com/themakers/stack/stack_backend/stack_backend_text"
)
// benchCtx builds a context with a text backend writing to io.Discard, so
// the benchmarks measure the library's work rather than writing to stdout.
func benchCtx() context.Context {
return stack.With().
Backend(stack_backend_text.NewWithWriter(io.Discard)).
ServiceName("bench").
Environment("test").
Instance("bench-0").
Apply(context.Background())
}
// BenchmarkSpanOpenClose — opening and closing a span (the most frequent path).
func BenchmarkSpanOpenClose(b *testing.B) {
ctx := benchCtx()
b.ReportAllocs()
for b.Loop() {
_, done := stack.Span(ctx)
done()
}
}
// BenchmarkInfo_3StringAttrs — a typical log with three string attributes.
func BenchmarkInfo_3StringAttrs(b *testing.B) {
ctx, done := stack.Span(benchCtx())
defer done()
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
stack.Info(ctx, "request handled",
stack.F("request_url_path", "/api/v1/order"),
stack.F("method", "POST"),
stack.F("session", "abc123"),
)
}
}
// BenchmarkInfo_DynamicAttrs — attrs with dynamic (non-constant) values: this
// is where union Value shows up — constant strings box for free via static
// read-only eface data, dynamic values used to allocate per attribute.
func BenchmarkInfo_DynamicAttrs(b *testing.B) {
ctx, done := stack.Span(benchCtx())
defer done()
path := time.Now().Format("/2006/01/02/order")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
stack.Info(ctx, "request handled",
stack.F("request_url_path", path),
stack.F("attempt", i),
stack.F("ok", i%2 == 0),
stack.F("elapsed", time.Duration(i)*time.Microsecond),
)
}
}
// BenchmarkError — the path that records an error and collects a stack trace.
func BenchmarkError(b *testing.B) {
ctx, done := stack.Span(benchCtx())
defer done()
err := errors.New("boom")
b.ReportAllocs()
for b.Loop() {
_ = stack.Error(ctx, "operation failed", err,
stack.F("code", 500),
)
}
}
// BenchmarkTextHandle_SpanEnd — rendering a span-end event in the text backend.
func BenchmarkTextHandle_SpanEnd(b *testing.B) {
backend := stack_backend_text.NewWithWriter(io.Discard)
e := makeSpanEndEvent()
b.ReportAllocs()
for b.Loop() {
backend.Handle(e)
}
}
// BenchmarkTextHandle_Log — rendering a log event with string attributes.
func BenchmarkTextHandle_Log(b *testing.B) {
backend := stack_backend_text.NewWithWriter(io.Discard)
e := makeLogEvent(false)
b.ReportAllocs()
for b.Loop() {
backend.Handle(e)
}
}
// BenchmarkTextHandle_LogMapAttr — rendering a log event with a map attribute
// (the cold json.Marshal path).
func BenchmarkTextHandle_LogMapAttr(b *testing.B) {
backend := stack_backend_text.NewWithWriter(io.Discard)
e := makeLogEvent(true)
b.ReportAllocs()
for b.Loop() {
backend.Handle(e)
}
}
// BenchmarkFullPath — the end-to-end path: span+ / info / span-.
func BenchmarkFullPath(b *testing.B) {
ctx := benchCtx()
b.ReportAllocs()
for b.Loop() {
c, done := stack.Span(ctx)
stack.Info(c, "processing",
stack.F("request_url_path", "/api/v1/order"),
stack.F("user", "u-42"),
stack.F("session", "abc123"),
)
done()
}
}
func makeSpanEndEvent() stack_backend.Event {
s := stack_backend.Get(context.Background())
s.Span.TraceID = stack_backend.NewTraceID()
s.Span.ID = stack_backend.NewID()
s.Span.Name = "handler()"
s.Span.File = "/src/app/handler.go"
s.Span.Line = 42
s.Span.Attrs = []stack_backend.Attr{
{Name: "request_url_path", Value: stack_backend.StringValue("/api/v1/order")},
{Name: "method", Value: stack_backend.StringValue("POST")},
{Name: "session", Value: stack_backend.StringValue("abc123")},
{Name: "user", Value: stack_backend.StringValue("u-42")},
}
return stack_backend.Event{Kind: stack_backend.KindSpanEnd, State: s}
}
func makeLogEvent(withMap bool) stack_backend.Event {
s := stack_backend.Get(context.Background())
s.Span.TraceID = stack_backend.NewTraceID()
s.Span.ID = stack_backend.NewID()
s.Span.Attrs = []stack_backend.Attr{
{Name: "request_url_path", Value: stack_backend.StringValue("/api/v1/order")},
{Name: "method", Value: stack_backend.StringValue("POST")},
}
own := []stack_backend.Attr{
{Name: "session", Value: stack_backend.StringValue("abc123")},
{Name: "user", Value: stack_backend.StringValue("u-42")},
{Name: "level", Value: stack_backend.StringValue("info")},
}
if withMap {
own = append(own, stack_backend.Attr{
Name: "payload",
Value: stack_backend.AnyValue(map[string]any{"a": 1, "b": "x", "c": true}),
})
}
return stack_backend.Event{
Kind: stack_backend.KindLog,
State: s,
LogEvent: stack_backend.LogEvent{
ID: stack_backend.NewID(),
Name: "request handled",
Level: stack_backend.LevelInfo,
OwnAttrs: own,
File: "/src/app/handler.go",
Line: 42,
},
}
}