From 88d879d764f998cdc249cd68f4f1fcc9851acc54 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Tue, 3 Nov 2026 03:06:52 +0100 Subject: [PATCH 1/2] driver/memcached: compare the least of several interleaved rounds in TestOperationLimitDoesNotAllocate1218 The test measured the client with the per-operation limit and the one without it one after the other, one AllocsPerRun of 200 runs each, and failed when the first came out above the second. Under -race sync.Pool drops Puts at random, so a round of either side can end an allocation above its true cost (1 round in 35 on 2 CPUs), and with one round per side the Coverage job saw "4.0 allocs/op with the limit, 3.0 without" (10 failed runs of 1000 under -race before). Both clients are built and warmed first and measured in turns over 7 rounds of 100 runs, and the test compares the least of the rounds of each side. The noise only adds allocations, so the least is the cost; a limit that allocates raises every round of its side. The plain budget (TestAllocBudgets) stays the guard. Refs #1235 --- driver/memcached/op_alloc_1218_test.go | 36 ++++++++++++++++++-------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/driver/memcached/op_alloc_1218_test.go b/driver/memcached/op_alloc_1218_test.go index 089b97c8e..4db4e9717 100644 --- a/driver/memcached/op_alloc_1218_test.go +++ b/driver/memcached/op_alloc_1218_test.go @@ -2,6 +2,7 @@ package memcached import ( "context" + "math" "testing" "time" ) @@ -23,24 +24,37 @@ func TestOperationLimitDoesNotAllocate1218(t *testing.T) { f.store.kv["k"] = memItem{value: []byte("v"), cas: 1} for _, cl := range opClients { t.Run(cl.name, func(t *testing.T) { - allocs := func(timeout time.Duration, ctx context.Context) float64 { + // Both clients are built and warmed first, then measured in turns, so + // whatever the process does meanwhile reaches both alike. + get := func(timeout time.Duration) func() { c := cl.new(t, Config{Addr: f.Addr(), Timeout: timeout}) - if _, err := c.Get(ctx, "k"); err != nil { // warm the pool + bg := context.Background() + if _, err := c.Get(bg, "k"); err != nil { // warm the pool t.Fatal(err) } - return testing.AllocsPerRun(200, func() { - if _, err := c.Get(ctx, "k"); err != nil { + return func() { + if _, err := c.Get(bg, "k"); err != nil { t.Fatal(err) } - }) + } } - bg := context.Background() - on, off := allocs(3*time.Second, bg), allocs(-1, bg) - t.Logf("Get: %.1f allocs/op with the limit, %.1f without", on, off) - if on > off { - t.Errorf("the per-operation limit costs %.1f allocations per command, want 0", on-off) + withLimit, without := get(3*time.Second), get(-1) + // Under -race sync.Pool drops Puts at random, so a round of either side + // can come out an allocation above its true cost (about 1 round in 35 + // under -race on 2 CPUs, either side alike), and with one round per side + // the noise of one was set against the other's. The noise only adds + // allocations: the least of the rounds of a side is its cost. A limit + // that allocates raises every round of its side, so its least too. + const rounds = 7 + onMin, offMin := math.Inf(1), math.Inf(1) + for range rounds { + onMin = min(onMin, testing.AllocsPerRun(100, withLimit)) + offMin = min(offMin, testing.AllocsPerRun(100, without)) + } + t.Logf("Get: %.1f allocs/op with the limit, %.1f without (least of %d rounds)", onMin, offMin, rounds) + if onMin > offMin { + t.Errorf("the per-operation limit costs %.1f allocations per command, want 0", onMin-offMin) } - }) } } From f4fe6b99d08a5e9a2e23dcf02ed11a89fc447541 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Tue, 3 Nov 2026 04:14:53 +0100 Subject: [PATCH 2/2] driver/memcached: say what the least-round comparison sees, and alternate the side measured first The comparison sees a limit that costs a whole allocation or more on every command (AllocsPerRun rounds its average down, so one that allocates on some commands only is invisible, as it was to the single round); the comment says so. The side measured first alternates from round to round. Refs #1235 --- driver/memcached/op_alloc_1218_test.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/driver/memcached/op_alloc_1218_test.go b/driver/memcached/op_alloc_1218_test.go index 4db4e9717..1980a7bab 100644 --- a/driver/memcached/op_alloc_1218_test.go +++ b/driver/memcached/op_alloc_1218_test.go @@ -44,12 +44,24 @@ func TestOperationLimitDoesNotAllocate1218(t *testing.T) { // under -race on 2 CPUs, either side alike), and with one round per side // the noise of one was set against the other's. The noise only adds // allocations: the least of the rounds of a side is its cost. A limit - // that allocates raises every round of its side, so its least too. + // that costs a whole allocation or more on every command raises every + // round of its side, so its least too. AllocsPerRun rounds its average + // down, so a limit that allocates on some of the commands only (under + // 1 per command on average) is not seen here, in plain mode or under + // -race, nor reliably by the single round this replaces (TestAllocBudgets + // is the plain guard). The side measured first alternates, so what a + // round does to the pools (a GC clears them) does not always fall on + // the same side. const rounds = 7 onMin, offMin := math.Inf(1), math.Inf(1) - for range rounds { - onMin = min(onMin, testing.AllocsPerRun(100, withLimit)) - offMin = min(offMin, testing.AllocsPerRun(100, without)) + for r := range rounds { + if r%2 == 0 { + onMin = min(onMin, testing.AllocsPerRun(100, withLimit)) + offMin = min(offMin, testing.AllocsPerRun(100, without)) + } else { + offMin = min(offMin, testing.AllocsPerRun(100, without)) + onMin = min(onMin, testing.AllocsPerRun(100, withLimit)) + } } t.Logf("Get: %.1f allocs/op with the limit, %.1f without (least of %d rounds)", onMin, offMin, rounds) if onMin > offMin {