E57E Add allocation tests for the Record · open-telemetry/opentelemetry-go@fd5fa0b · GitHub
[go: up one dir, main page]

Skip to content

Commit fd5fa0b

Browse files
committed
Add allocation tests for the Record
1 parent 8d63696 commit fd5fa0b

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

log/record_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,63 @@ func TestRecordAttributes(t *testing.T) {
103103
}
104104
})
105105
}
106+
107+
func TestRecordAllocationLimits(t *testing.T) {
108+
const runs = 5
109+
110+
// Assign testing results to external scope so the compiler doesn't
111+
// optimize away the testing statements.
112+
var (
113+
tStamp time.Time
114+
sev log.Severity
115+
text string
116+
body log.Value
117+
n int
118+
attr log.KeyValue
119+
)
120+
121+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
122+
var r log.Record
123+
r.SetTimestamp(y2k)
124+
tStamp = r.Timestamp()
125+
}), "Timestamp")
126+
127+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
128+
var r log.Record
129+
r.SetObservedTimestamp(y2k)
130+
tStamp = r.ObservedTimestamp()
131+
}), "ObservedTimestamp")
132+
133+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
134+
var r log.Record
135+
r.SetSeverity(log.SeverityDebug)
136+
sev = r.Severity()
137+
}), "Severity")
138+
139+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
140+
var r log.Record
141+
r.SetSeverityText("severity text")
142+
text = r.SeverityText()
143+
}), "SeverityText")
144+
145+
bodyVal := log.BoolValue(true)
146+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
147+
var r log.Record
148+
r.SetBody(bodyVal)
149+
body = r.Body()
150+
}), "Body")
151+
152+
attrVal := []log.KeyValue{log.Bool("k", true), log.Int("i", 1)}
153+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
154+
var r log.Record
155+
r.AddAttributes(attrVal...)
156+
n = r.AttributesLen()
157+
r.WalkAttributes(func(kv log.KeyValue) bool {
158+
attr = kv
159+
return true
160+
})
161+
}), "Attributes")
162+
163+
// Convince the linter these values are used.
164+
_, _, _, _, _, _ = tStamp, sev, text, body, n, attr
165+
}

0 commit comments

Comments
 (0)
0