@@ -3,6 +3,7 @@ package perf
33import (
44 "bytes"
55 "context"
6+ "fmt"
67 "io"
78 "log/slog"
89 "net/http"
@@ -247,6 +248,40 @@ func BenchmarkSharedStreamingAuditAndUsageObservers(b *testing.B) {
247248 }
248249}
249250
251+ func TestFormatPerfGuardResult (t * testing.T ) {
252+ result := testing.BenchmarkResult {
253+ N : 1 ,
254+ T : 2 * time .Microsecond ,
255+ MemAllocs : 114 ,
256+ MemBytes : 13654 ,
257+ }
258+
259+ got := formatPerfGuardResult ("gateway_chat_completion_hot_path" , result , 150 , 18 * 1024 )
260+
261+ for _ , want := range []string {
262+ "gateway_chat_completion_hot_path" ,
263+ "ns/op=" ,
264+ "allocs/op=114/150" ,
265+ "bytes/op=13654/18432" ,
266+ } {
267+ if ! strings .Contains (got , want ) {
268+ t .Fatalf ("formatPerfGuardResult() = %q, want substring %q" , got , want )
269+ }
270+ }
271+ }
272+
273+ func formatPerfGuardResult (name string , result testing.BenchmarkResult , maxAllocs , maxBytes int64 ) string {
274+ return fmt .Sprintf (
275+ "%s: ns/op=%d allocs/op=%d/%d bytes/op=%d/%d" ,
276+ name ,
277+ result .NsPerOp (),
278+ result .AllocsPerOp (),
279+ maxAllocs ,
280+ result .AllocedBytesPerOp (),
281+ maxBytes ,
282+ )
283+ }
284+
250285func TestHotPathPerfGuard (t * testing.T ) {
251286 t .Helper ()
252287
@@ -283,6 +318,7 @@ func TestHotPathPerfGuard(t *testing.T) {
283318 tc := tc
284319 t .Run (tc .name , func (t * testing.T ) {
285320 result := testing .Benchmark (tc .bench )
321+ t .Log (formatPerfGuardResult (tc .name , result , tc .maxAllocs , tc .maxBytes ))
286322
287323 if got := result .AllocsPerOp (); got > tc .maxAllocs {
288324 t .Fatalf ("allocs/op = %d, want <= %d" , got , tc .maxAllocs )
0 commit comments