-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathassert_bench_test.go
More file actions
72 lines (61 loc) · 1.86 KB
/
Copy pathassert_bench_test.go
File metadata and controls
72 lines (61 loc) · 1.86 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
package assert
import "testing"
// BenchmarkAssert_Success benchmarks the successful assertion path.
// This is the hot path - assertions that pass should be extremely cheap.
func BenchmarkAssert_Success(b *testing.B) {
for i := 0; i < b.N; i++ {
Assert(true, "never fails")
}
}
// BenchmarkAssert_SuccessWithValues benchmarks successful assertions with contextual values.
// Even though values are provided, they should not be evaluated when the assertion passes.
func BenchmarkAssert_SuccessWithValues(b *testing.B) {
for i := 0; i < b.N; i++ {
Assert(true, "never fails",
"key1", "value1",
"key2", 42,
"key3", struct{ Field string }{Field: "test"},
)
}
}
// BenchmarkAssert_WithSourceContext benchmarks assertions with source context enabled.
func BenchmarkAssert_WithSourceContext(b *testing.B) {
// Save and restore original config
originalConfig := activeConfig
defer func() {
SetConfig(originalConfig)
}()
SetConfig(Config{IncludeSource: true, ContextLines: 5})
b.ResetTimer()
for i := 0; i < b.N; i++ {
Assert(true, "with context")
}
}
// BenchmarkAssert_WithoutSourceContext benchmarks assertions with source context disabled.
func BenchmarkAssert_WithoutSourceContext(b *testing.B) {
// Save and restore original config
originalConfig := activeConfig
defer func() {
SetConfig(originalConfig)
}()
SetConfig(Config{IncludeSource: false})
b.ResetTimer()
for i := 0; i < b.N; i++ {
Assert(true, "no context")
}
}
// BenchmarkDebug_Success benchmarks the Debug assertion (disabled by default).
func BenchmarkDebug_Success(b *testing.B) {
for i := 0; i < b.N; i++ {
Debug(true, "debug assertion")
}
}
// BenchmarkDebug_SuccessWithValues benchmarks Debug assertions with values.
func BenchmarkDebug_SuccessWithValues(b *testing.B) {
for i := 0; i < b.N; i++ {
Debug(true, "debug assertion",
"key1", "value1",
"key2", 42,
)
}
}