Skip to content

Commit 7335eba

Browse files
committed
test(bench): AX-11 harnesses for int/encode/format/unicode/result/context/reflect
Wave A of the missing-bench sweep — seven primitive files covered. All are stdlib passthroughs; the harnesses lock the contract floor so a Core reroute can't slow them silently. * int.go — Atoi/Itoa/FormatInt/FormatUint/ParseInt * encode.go — Hex + Base64 (std + URL) encode/decode at 16B + 1KB * format.go — Sprint/Sprintf/Sprintln/Print/Errorf (with %w wrap) * unicode.go — IsLetter/IsDigit/IsSpace/IsUpper/IsLower + ToUpper/ToLower (ASCII + non-ASCII paths) * result.go — Ok/Fail/ResultOf/Error/Code/Or/Must/Cast/MustCast/Try * context.go — Background/TODO/WithCancel/WithTimeout/WithDeadline/ WithValue + Value lookup * reflect.go — TypeOf/ValueOf/Kind/DeepEqual/Zero across int/struct/ slice/map fixtures Floor numbers on Apple M3 Ultra captured in commit message via 'go test -bench=. -benchmem'.
1 parent 2547f66 commit 7335eba

7 files changed

Lines changed: 763 additions & 0 deletions

context_bench_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// SPDX-License-Identifier: EUPL-1.2
2+
3+
// Benchmarks for the context primitives in context.go.
4+
// Per AX-11 — Background / TODO / WithCancel / WithTimeout / WithValue
5+
// are called by every Action handler and Service lifecycle entry point.
6+
// Bare passthroughs to context but the bench harness gates the contract.
7+
//
8+
// Run: go test -bench='BenchmarkCtx' -benchmem -run='^$' .
9+
10+
package core_test
11+
12+
import (
13+
. "dappco.re/go"
14+
)
15+
16+
// Sinks defeat compiler DCE.
17+
var (
18+
ctxSinkContext Context
19+
ctxSinkCancel CancelFunc
20+
ctxSinkAny any
21+
)
22+
23+
type ctxKey struct{}
24+
25+
// --- Roots ---
26+
27+
func BenchmarkCtx_Background(b *B) {
28+
b.ReportAllocs()
29+
for i := 0; i < b.N; i++ {
30+
ctxSinkContext = Background()
31+
}
32+
}
33+
34+
func BenchmarkCtx_TODO(b *B) {
35+
b.ReportAllocs()
36+
for i := 0; i < b.N; i++ {
37+
ctxSinkContext = TODO()
38+
}
39+
}
40+
41+
// --- Cancellable variants ---
42+
//
43+
// WithCancel + WithTimeout + WithDeadline allocate a child ctx node
44+
// each call. The cancel func must be invoked to release the goroutine
45+
// the runtime starts behind it — otherwise the bench leaks one per iter.
46+
47+
func BenchmarkCtx_WithCancel(b *B) {
48+
parent := Background()
49+
b.ReportAllocs()
50+
for i := 0; i < b.N; i++ {
51+
ctxSinkContext, ctxSinkCancel = WithCancel(parent)
52+
ctxSinkCancel()
53+
}
54+
}
55+
56+
func BenchmarkCtx_WithTimeout(b *B) {
57+
parent := Background()
58+
b.ReportAllocs()
59+
for i := 0; i < b.N; i++ {
60+
ctxSinkContext, ctxSinkCancel = WithTimeout(parent, 1*Second)
61+
ctxSinkCancel()
62+
}
63+
}
64+
65+
func BenchmarkCtx_WithDeadline(b *B) {
66+
parent := Background()
67+
deadline := Now().Add(1 * Second)
68+
b.ReportAllocs()
69+
for i := 0; i < b.N; i++ {
70+
ctxSinkContext, ctxSinkCancel = WithDeadline(parent, deadline)
71+
ctxSinkCancel()
72+
}
73+
}
74+
75+
// --- Values ---
76+
77+
func BenchmarkCtx_WithValue(b *B) {
78+
parent := Background()
79+
k := ctxKey{}
80+
b.ReportAllocs()
81+
for i := 0; i < b.N; i++ {
82+
ctxSinkContext = WithValue(parent, k, "req-12345")
83+
}
84+
}
85+
86+
func BenchmarkCtx_Value_Lookup(b *B) {
87+
k := ctxKey{}
88+
ctx := WithValue(Background(), k, "req-12345")
89+
b.ReportAllocs()
90+
for i := 0; i < b.N; i++ {
91+
ctxSinkAny = ctx.Value(k)
92+
}
93+
}

encode_bench_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// SPDX-License-Identifier: EUPL-1.2
2+
3+
// Benchmarks for the encoding primitives in encode.go.
4+
// Per AX-11 — hex + base64 are on every fingerprint emit, every API
5+
// auth-header build, every model-hash compare, every keys.Pack write.
6+
// The wrappers are encoding/{hex,base64} passthroughs; the bench
7+
// harness gates the contract so a Core reroute can't slow them silently.
8+
//
9+
// Run: go test -bench='BenchmarkEncode' -benchmem -run='^$' .
10+
11+
package core_test
12+
13+
import (
14+
. "dappco.re/go"
15+
)
16+
17+
// Sinks defeat compiler DCE.
18+
var (
19+
encodeSinkString string
20+
encodeSinkResult Result
21+
)
22+
23+
// --- Fixtures ---
24+
//
25+
// 16-byte payload mirrors SHA-256 truncated or a small auth token.
26+
// 1KB payload covers larger blobs (model header chunk, jwt body).
27+
28+
var (
29+
encodeBytes16 = []byte("aaaaaaaaaaaaaaaa")
30+
encodeBytes1K = func() []byte {
31+
out := make([]byte, 1024)
32+
for i := range out {
33+
out[i] = byte('a' + i%26)
34+
}
35+
return out
36+
}()
37+
)
38+
39+
// --- Hex ---
40+
41+
func BenchmarkEncode_HexEncode_16B(b *B) {
42+
b.ReportAllocs()
43+
for i := 0; i < b.N; i++ {
44+
encodeSinkString = HexEncode(encodeBytes16)
45+
}
46+
}
47+
48+
func BenchmarkEncode_HexEncode_1KB(b *B) {
49+
b.ReportAllocs()
50+
for i := 0; i < b.N; i++ {
51+
encodeSinkString = HexEncode(encodeBytes1K)
52+
}
53+
}
54+
55+
func BenchmarkEncode_HexDecode_16B(b *B) {
56+
encoded := HexEncode(encodeBytes16)
57+
b.ReportAllocs()
58+
for i := 0; i < b.N; i++ {
59+
encodeSinkResult = HexDecode(encoded)
60+
}
61+
}
62+
63+
func BenchmarkEncode_HexDecode_1KB(b *B) {
64+
encoded := HexEncode(encodeBytes1K)
65+
b.ReportAllocs()
66+
for i := 0; i < b.N; i++ {
67+
encodeSinkResult = HexDecode(encoded)
68+
}
69+
}
70+
71+
// --- Base64 std ---
72+
73+
func BenchmarkEncode_Base64Encode_16B(b *B) {
74+
b.ReportAllocs()
75+
for i := 0; i < b.N; i++ {
76+
encodeSinkString = Base64Encode(encodeBytes16)
77+
}
78+
}
79+
80+
func BenchmarkEncode_Base64Encode_1KB(b *B) {
81+
b.ReportAllocs()
82+
for i := 0; i < b.N; i++ {
83+
encodeSinkString = Base64Encode(encodeBytes1K)
84+
}
85+
}
86+
87+
func BenchmarkEncode_Base64Decode_16B(b *B) {
88+
encoded := Base64Encode(encodeBytes16)
89+
b.ReportAllocs()
90+
for i := 0; i < b.N; i++ {
91+
encodeSinkResult = Base64Decode(encoded)
92+
}
93+
}
94+
95+
func BenchmarkEncode_Base64Decode_1KB(b *B) {
96+
encoded := Base64Encode(encodeBytes1K)
97+
b.ReportAllocs()
98+
for i := 0; i < b.N; i++ {
99+
encodeSinkResult = Base64Decode(encoded)
100+
}
101+
}
102+
103+
// --- Base64 url ---
104+
105+
func BenchmarkEncode_Base64URLEncode_16B(b *B) {
106+
b.ReportAllocs()
107+
for i := 0; i < b.N; i++ {
108+
encodeSinkString = Base64URLEncode(encodeBytes16)
109+
}
110+
}
111+
112+
func BenchmarkEncode_Base64URLDecode_16B(b *B) {
113+
encoded := Base64URLEncode(encodeBytes16)
114+
b.ReportAllocs()
115+
for i := 0; i < b.N; i++ {
116+
encodeSinkResult = Base64URLDecode(encoded)
117+
}
118+
}

format_bench_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// SPDX-License-Identifier: EUPL-1.2
2+
3+
// Benchmarks for the formatting primitives in format.go.
4+
// Per AX-11 — Sprintf is on every error message, every log key=val
5+
// pair, every CLI output line. Bare passthroughs to fmt but the bench
6+
// harness gates the contract so a Core reroute can't slow them.
7+
//
8+
// Run: go test -bench='BenchmarkFormat' -benchmem -run='^$' .
9+
10+
package core_test
11+
12+
import (
13+
"bytes"
14+
15+
. "dappco.re/go"
16+
)
17+
18+
// Sinks defeat compiler DCE.
19+
var (
20+
formatSinkString string
21+
formatSinkErr error
22+
)
23+
24+
// --- Sprint ---
25+
26+
func BenchmarkFormat_Sprint_OneArg(b *B) {
27+
b.ReportAllocs()
28+
for i := 0; i < b.N; i++ {
29+
formatSinkString = Sprint(42)
30+
}
31+
}
32+
33+
func BenchmarkFormat_Sprint_ThreeArgs(b *B) {
34+
b.ReportAllocs()
35+
for i := 0; i < b.N; i++ {
36+
formatSinkString = Sprint("agent", "ready", 9000)
37+
}
38+
}
39+
40+
// --- Sprintf ---
41+
42+
func BenchmarkFormat_Sprintf_Verbs(b *B) {
43+
b.ReportAllocs()
44+
for i := 0; i < b.N; i++ {
45+
formatSinkString = Sprintf("%s connected on %d", "homelab", 9000)
46+
}
47+
}
48+
49+
func BenchmarkFormat_Sprintf_KeyVal(b *B) {
50+
b.ReportAllocs()
51+
for i := 0; i < b.N; i++ {
52+
formatSinkString = Sprintf("%v=%q", "key", "value")
53+
}
54+
}
55+
56+
// --- Sprintln ---
57+
58+
func BenchmarkFormat_Sprintln(b *B) {
59+
b.ReportAllocs()
60+
for i := 0; i < b.N; i++ {
61+
formatSinkString = Sprintln("agent", "ready")
62+
}
63+
}
64+
65+
// --- Print to a buffer (writer-mediated, avoids stdout in bench) ---
66+
67+
func BenchmarkFormat_Print_Buffer(b *B) {
68+
var buf bytes.Buffer
69+
b.ReportAllocs()
70+
for i := 0; i < b.N; i++ {
71+
buf.Reset()
72+
Print(&buf, "port: %d", 9000)
73+
}
74+
}
75+
76+
// --- Errorf ---
77+
78+
func BenchmarkFormat_Errorf_NoWrap(b *B) {
79+
b.ReportAllocs()
80+
for i := 0; i < b.N; i++ {
81+
formatSinkErr = Errorf("connect %s failed", "homelab")
82+
}
83+
}
84+
85+
func BenchmarkFormat_Errorf_Wrap(b *B) {
86+
cause := NewError("connection refused")
87+
b.ReportAllocs()
88+
for i := 0; i < b.N; i++ {
89+
formatSinkErr = Errorf("connect %s: %w", "homelab", cause)
90+
}
91+
}

int_bench_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// SPDX-License-Identifier: EUPL-1.2
2+
3+
// Benchmarks for the integer conversion primitives in int.go.
4+
// Per AX-11 — Atoi / Itoa / ParseInt / FormatInt land on every config
5+
// parse, every HTTP query-string read, every protocol-port handler.
6+
// These are strconv passthroughs but the bench harness gates the
7+
// contract: if a Core reroute slows them down, the regression surfaces
8+
// against a published floor.
9+
//
10+
// Run: go test -bench='BenchmarkInt' -benchmem -run='^$' .
11+
12+
package core_test
13+
14+
import (
15+
. "dappco.re/go"
16+
)
17+
18+
// Sinks defeat compiler DCE.
19+
var (
20+
intSinkResult Result
21+
intSinkString string
22+
)
23+
24+
// --- Atoi / Itoa ---
25+
26+
func BenchmarkInt_Atoi(b *B) {
27+
b.ReportAllocs()
28+
for i := 0; i < b.N; i++ {
29+
intSinkResult = Atoi("42")
30+
}
31+
}
32+
33+
func BenchmarkInt_Atoi_Bad(b *B) {
34+
b.ReportAllocs()
35+
for i := 0; i < b.N; i++ {
36+
intSinkResult = Atoi("not-a-number")
37+
}
38+
}
39+
40+
func BenchmarkInt_Itoa(b *B) {
41+
b.ReportAllocs()
42+
for i := 0; i < b.N; i++ {
43+
intSinkString = Itoa(123456)
44+
}
45+
}
46+
47+
// --- FormatInt / FormatUint ---
48+
49+
func BenchmarkInt_FormatInt_Decimal(b *B) {
50+
b.ReportAllocs()
51+
for i := 0; i < b.N; i++ {
52+
intSinkString = FormatInt(1234567890, 10)
53+
}
54+
}
55+
56+
func BenchmarkInt_FormatInt_Hex(b *B) {
57+
b.ReportAllocs()
58+
for i := 0; i < b.N; i++ {
59+
intSinkString = FormatInt(255, 16)
60+
}
61+
}
62+
63+
func BenchmarkInt_FormatUint_Hex(b *B) {
64+
b.ReportAllocs()
65+
for i := 0; i < b.N; i++ {
66+
intSinkString = FormatUint(0xdeadbeef, 16)
67+
}
68+
}
69+
70+
// --- ParseInt ---
71+
72+
func BenchmarkInt_ParseInt_Hex(b *B) {
73+
b.ReportAllocs()
74+
for i := 0; i < b.N; i++ {
75+
intSinkResult = ParseInt("deadbeef", 16, 64)
76+
}
77+
}
78+
79+
func BenchmarkInt_ParseInt_Decimal(b *B) {
80+
b.ReportAllocs()
81+
for i := 0; i < b.N; i++ {
82+
intSinkResult = ParseInt("1234567890", 10, 64)
83+
}
84+
}

0 commit comments

Comments
 (0)