@@ 3,11 3,32 @@ package bearer
import (
"context"
"errors"
+ "io"
+ "log"
"net/http"
"net/http/httptest"
"testing"
)
+// discardStdLog silences the standard logger for one benchmark. sr-ht-core's
+// auth.DecodeBearerToken calls log.Printf on every token it refuses, so the
+// invalid case below writes one line per iteration — an unfiltered run of this
+// package produced 9.5 million of them and a 901 MB file. Two things follow.
+// The number stops being a measurement of this package: log.Printf takes a
+// mutex, so what was timed was the logger. And the file is worse than useless
+// rather than merely large, because benchfmt skips lines it cannot parse, so
+// bench.sr.ht would accept that upload and report success over it.
+//
+// It is the std log package here rather than slog — that is why this is not
+// the discardLog the middleware and chimw benchmarks share.
+func discardStdLog(b *testing.B) {
+ b.Helper()
+
+ previous := log.Writer()
+ log.SetOutput(io.Discard)
+ b.Cleanup(func() { log.SetOutput(previous) })
+}
+
// The grant strings these benchmarks present. The stateless one is what a short
// token carries; the registered one adds the id: member that turns step 4 from
// a no-op into a cache lookup.
@@ 74,6 95,8 @@ func benchValidator(b *testing.B) *Validator {
// the path an instance under a flood of forged credentials runs; it must
// stay the cheapest thing here.
func BenchmarkValidate(b *testing.B) {
+ discardStdLog(b)
+
ctx := context.Background()
v := benchValidator(b)
@@ 136,6 159,8 @@ func BenchmarkValidate(b *testing.B) {
// beside BenchmarkValidate/stateless — the gap is the grant check that Inspect
// leaves to the handler.
func BenchmarkInspect(b *testing.B) {
+ discardStdLog(b)
+
ctx := context.Background()
v := benchValidator(b)
presented := ourToken(benchGrants)
@@ 158,6 183,8 @@ func BenchmarkInspect(b *testing.B) {
// is not, and the point of measuring it is to know which of the two dominates
// before somebody proposes sharding the cache.
func BenchmarkValidateParallel(b *testing.B) {
+ discardStdLog(b)
+
ctx := context.Background()
v := benchValidator(b)
registered := ourToken(benchGrantsRegistered)