Skip to content

Commit 8459346

Browse files
committed
test(bench): white-box benches for unexported helpers + Println/HTTPListenAndServe
safeName (Action/Task), Fs.path, assertFail/assertMsg (via a capturing TB stub); Println redirected to /dev/null; HTTPListenAndServe on its error path. Co-Authored-By: Virgil <virgil@lethean.io>
1 parent 6551be4 commit 8459346

3 files changed

Lines changed: 90 additions & 3 deletions

File tree

api_bench_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ func BenchmarkCore_RemoteAction(b *B) {
308308
}
309309
}
310310

311-
// HTTPListenAndServe + RemoteAction are not benched — they bind a real
312-
// socket / make a real outbound HTTP call. A unit-bench cannot do that
313-
// deterministically. They are exercised end-to-end in api_test.go.
311+
// HTTPListenAndServe is benched on its error path: an invalid address
312+
// fails the bind immediately and returns the error Result, with no real
313+
// socket. The success path blocks serving, so it can't be unit-benched.
314+
func BenchmarkHTTPListenAndServe(b *B) {
315+
b.ReportAllocs()
316+
for i := 0; i < b.N; i++ {
317+
apiSinkResult = HTTPListenAndServe("127.0.0.1:-1", nil)
318+
}
319+
}

format_bench_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package core_test
1111

1212
import (
1313
"bytes"
14+
"os"
1415

1516
. "dappco.re/go"
1617
)
@@ -89,3 +90,22 @@ func BenchmarkFormat_Errorf_Wrap(b *B) {
8990
formatSinkErr = Errorf("connect %s: %w", "homelab", cause)
9091
}
9192
}
93+
94+
// Println writes to os.Stdout; redirect to /dev/null so the bench measures
95+
// the formatting + write cost, not terminal scroll.
96+
func BenchmarkPrintln(b *B) {
97+
devnull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
98+
if err != nil {
99+
b.Skip("no /dev/null")
100+
}
101+
old := os.Stdout
102+
os.Stdout = devnull
103+
defer func() {
104+
os.Stdout = old
105+
devnull.Close()
106+
}()
107+
b.ReportAllocs()
108+
for i := 0; i < b.N; i++ {
109+
Println("agent ready", 42)
110+
}
111+
}

whitebox_bench_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: EUPL-1.2
2+
3+
// White-box benchmarks for unexported helpers a black-box (core_test)
4+
// bench can't reach: action/task name sanitisation, sandbox path
5+
// resolution, and the assertion failure/message formatters (driven
6+
// through a capturing TB stub so the bench exercises the real formatting
7+
// path without actually failing).
8+
//
9+
// Run: go test -bench='Benchmark(Action_safeName|Task_safeName|Fs_path|AssertFail|AssertMsg)' -benchmem -run='^$' .
10+
11+
package core
12+
13+
// assertBenchStub embeds *B — inheriting the unexported method that makes
14+
// it a testing.TB — and swallows Error/Fatal so assertFail runs its real
15+
// formatting path without failing the benchmark.
16+
type assertBenchStub struct{ *B }
17+
18+
func (assertBenchStub) Error(args ...any) {}
19+
func (assertBenchStub) Fatal(args ...any) {}
20+
21+
var whiteboxSink string
22+
23+
func BenchmarkAction_safeName(b *B) {
24+
a := &Action{Name: "agentic.dispatch"}
25+
b.ReportAllocs()
26+
for i := 0; i < b.N; i++ {
27+
whiteboxSink = a.safeName()
28+
}
29+
}
30+
31+
func BenchmarkTask_safeName(b *B) {
32+
t := &Task{Name: "deploy.homelab"}
33+
b.ReportAllocs()
34+
for i := 0; i < b.N; i++ {
35+
whiteboxSink = t.safeName()
36+
}
37+
}
38+
39+
func BenchmarkFs_path(b *B) {
40+
m := (&Fs{}).New("/tmp")
41+
b.ReportAllocs()
42+
for i := 0; i < b.N; i++ {
43+
whiteboxSink = m.path("workspace/agent/readme.md")
44+
}
45+
}
46+
47+
func BenchmarkAssertFail(b *B) {
48+
stub := assertBenchStub{b}
49+
b.ReportAllocs()
50+
for i := 0; i < b.N; i++ {
51+
assertFail(stub, false, "Bench", nil, "want", 1, "got", 2)
52+
}
53+
}
54+
55+
func BenchmarkAssertMsg(b *B) {
56+
msg := []string{"context line", "detail line"}
57+
b.ReportAllocs()
58+
for i := 0; i < b.N; i++ {
59+
whiteboxSink = assertMsg(msg)
60+
}
61+
}

0 commit comments

Comments
 (0)