From f6df989e67ebb0b07b99d3f67fc07cccd2acaba6 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 16 Nov 2026 07:13:40 +0000 Subject: [PATCH] coreruleset: time worst-case inspections in processor time Not offered as a PR: processor time alone does not keep the test from failing on a busy host. See the issue for the decision needed. Model: opus-5-5 --- go.mod | 2 +- internal/coreruleset/inspectiontime_test.go | 40 ++++++++++++++++----- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 346ad93..5f24224 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/oschwald/maxminddb-golang/v2 v2.7.0 github.com/prometheus/client_golang v1.24.1 github.com/spf13/viper v1.21.0 + golang.org/x/sys v0.48.0 ) require ( @@ -47,7 +48,6 @@ require ( go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect golang.org/x/net v0.60.0 // indirect golang.org/x/sync v0.23.0 // indirect - golang.org/x/sys v0.48.0 // indirect golang.org/x/text v0.42.0 // indirect google.golang.org/protobuf v1.36.11 // indirect rsc.io/binaryregexp v0.2.0 // indirect diff --git a/internal/coreruleset/inspectiontime_test.go b/internal/coreruleset/inspectiontime_test.go index a6bf51d..5934867 100644 --- a/internal/coreruleset/inspectiontime_test.go +++ b/internal/coreruleset/inspectiontime_test.go @@ -5,12 +5,15 @@ import ( "net/http" "net/http/httptest" "net/netip" + "runtime" "slices" "strconv" "strings" "testing" "time" + "golang.org/x/sys/unix" + "sneak.berlin/go/smallwebwaf/internal/coreruleset" ) @@ -302,8 +305,24 @@ func worstCaseBodies() []visitorRequest { } } -// inspectionTime returns how long crs takes to inspect tc, and fails t unless -// it reads all of tc's body and Coraza parses it. +// threadCPUTime returns the processor time the calling thread has used. +func threadCPUTime(t *testing.T) time.Duration { + t.Helper() + + var now unix.Timespec + + err := unix.ClockGettime(unix.CLOCK_THREAD_CPUTIME_ID, &now) + if err != nil { + t.Fatalf("read the thread's processor time: %v", err) + } + + return time.Duration(now.Nano()) +} + +// inspectionTime returns the processor time crs takes to inspect tc, and +// fails t unless it reads all of tc's body and Coraza parses it. Inspect does +// its work on the calling goroutine, which stays on one thread meanwhile, so +// that thread's processor time is the inspection's. func inspectionTime( t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest, ) time.Duration { @@ -318,11 +337,14 @@ func inspectionTime( req.Header.Set("Content-Type", tc.contentType) } - began := time.Now() + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + began := threadCPUTime(t) result, read, _ := crs.Inspect( req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body), ) - took := time.Since(began) + took := threadCPUTime(t) - began if string(read) != tc.body { t.Errorf("read %d bytes of %d", len(read), len(tc.body)) @@ -336,10 +358,12 @@ func inspectionTime( return took } -// TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime runs one at a time, so -// that the tests running in parallel do not slow it. Other work on the host -// can still slow one try, so a request fails only when three tries in a row -// take too long. +// TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime measures processor +// time, not time on the clock, so that the time other work on the host holds +// the processor does not count. It runs one at a time, so that the tests +// running in parallel do not slow it. Other work can still slow one try a +// little, by sharing the processor's caches, so a request fails only when +// three tries in a row take too long. // //nolint:paralleltest // one at a time, as above func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {