Skip to content

Commit 0a213c2

Browse files
authored
test: start the engine's reference processes in parallel, trim the floor walk (#897)
TestConcurrentEqualsSerialized started 40 reference processes one after another; under -race each takes ~1s just to start, so the test spent 42s waiting. They share nothing, so they now run GOMAXPROCS at a time (5.5s). applyStabilityFloor walks the whole tree on every invocation. It resolved the command's level once per flag instead of once per command, and copied every command's flags into a map to check shadowing. Both are now done without the repeat work: the floor costs ~0.48ms per run instead of ~0.70ms.
1 parent 0ad61f6 commit 0a213c2

2 files changed

Lines changed: 49 additions & 14 deletions

File tree

‎cmd/stability.go‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,11 @@ func blockBelowFloorFlags(cmd, root *cobra.Command, path string, p stability.Pol
583583
return // flags are the command's own business; nothing to check
584584
}
585585
blocked := make(map[string]stability.Level)
586+
// The command's level is the same for every flag, and Effective walks the
587+
// ancestors: resolve it once, not once per flag.
588+
cmdLevel := stability.Effective(cmd)
586589
consider := func(f *pflag.Flag, own stability.Level) {
587-
lvl := session.Weakest(stability.Effective(cmd), own)
590+
lvl := session.Weakest(cmdLevel, own)
588591
if p.AllowsFlag(path, f.Name, lvl, own) {
589592
return
590593
}
@@ -638,14 +641,18 @@ func blockBelowFloorFlags(cmd, root *cobra.Command, path string, p stability.Pol
638641
// permanently into this command's own flag set. These stages run on every
639642
// invocation, so that merge would be a global side effect of rendering help.
640643
func visitInheritedFlags(cmd *cobra.Command, fn func(*pflag.Flag)) {
641-
seen := map[string]bool{}
642-
cmd.Flags().VisitAll(func(f *pflag.Flag) { seen[f.Name] = true })
643-
644+
// Names a nearer ancestor already declared persistently. The command's own
645+
// flags are checked with Lookup rather than copied in here: this runs for
646+
// every command on every invocation, and the copy was most of its cost.
647+
var seen map[string]bool
644648
for parent := cmd.Parent(); parent != nil; parent = parent.Parent() {
645649
parent.PersistentFlags().VisitAll(func(f *pflag.Flag) {
646-
if seen[f.Name] {
650+
if seen[f.Name] || cmd.Flags().Lookup(f.Name) != nil {
647651
return // shadowed by this command, or by a nearer ancestor
648652
}
653+
if seen == nil {
654+
seen = map[string]bool{}
655+
}
649656
seen[f.Name] = true
650657
fn(f)
651658
})

‎pkg/engine/concurrent_isolation_test.go‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io"
910
"net/http"
1011
"net/http/httptest"
1112
"os"
1213
"os/exec"
14+
"runtime"
1315
"strings"
1416
"sync"
1517
"testing"
@@ -173,11 +175,34 @@ func TestConcurrentEqualsSerialized(t *testing.T) {
173175

174176
type key struct{ command, floor string }
175177
want := make(map[key]*engine.Result, len(isolationCorpus)*len(floors))
178+
// The reference processes share nothing, so they start side by side:
179+
// one after another, their startup alone took most of a minute under -race.
180+
var (
181+
mu sync.Mutex
182+
wg sync.WaitGroup
183+
errs []error
184+
sem = make(chan struct{}, runtime.GOMAXPROCS(0))
185+
)
176186
for _, f := range floors {
177187
for _, c := range isolationCorpus {
178-
want[key{c, f}] = pristineReference(t, env.URL, token, c, f)
188+
wg.Add(1)
189+
go func() {
190+
defer wg.Done()
191+
sem <- struct{}{}
192+
defer func() { <-sem }()
193+
res, err := pristineReference(env.URL, token, c, f)
194+
mu.Lock()
195+
defer mu.Unlock()
196+
if err != nil {
197+
errs = append(errs, err)
198+
return
199+
}
200+
want[key{c, f}] = res
201+
}()
179202
}
180203
}
204+
wg.Wait()
205+
require.NoError(t, errors.Join(errs...))
181206

182207
const rounds = 4
183208
total := rounds * len(want)
@@ -221,24 +246,27 @@ const (
221246

222247
// pristineReference runs one command through the serialized engine in a fresh
223248
// copy of this test binary, so that no earlier request has touched the
224-
// singleton tree it runs on.
225-
func pristineReference(t *testing.T, url, token, command, floor string) *engine.Result {
226-
t.Helper()
249+
// singleton tree it runs on. It returns an error rather than failing the test,
250+
// because it runs on a goroutine of its own.
251+
func pristineReference(url, token, command, floor string) (*engine.Result, error) {
227252
cmd := exec.Command(os.Args[0], "-test.run=^TestHelperSerializedReference$", "-test.count=1")
228253
cmd.Env = append(os.Environ(),
229254
referenceCommandEnv+"="+command, referenceURLEnv+"="+url,
230255
referenceTokenEnv+"="+token, referenceFloorEnv+"="+floor)
231256
out, err := cmd.Output()
232-
require.NoError(t, err, "reference process for %q", command)
257+
if err != nil {
258+
return nil, fmt.Errorf("reference process for %q [floor %q]: %w", command, floor, err)
259+
}
233260
for _, line := range strings.Split(string(out), "\n") {
234261
if payload, ok := strings.CutPrefix(line, referenceMarker); ok {
235262
var res engine.Result
236-
require.NoError(t, json.Unmarshal([]byte(payload), &res))
237-
return &res
263+
if err := json.Unmarshal([]byte(payload), &res); err != nil {
264+
return nil, fmt.Errorf("reference process for %q [floor %q]: %w", command, floor, err)
265+
}
266+
return &res, nil
238267
}
239268
}
240-
t.Fatalf("reference process for %q printed no result:\n%s", command, out)
241-
return nil
269+
return nil, fmt.Errorf("reference process for %q [floor %q] printed no result:\n%s", command, floor, out)
242270
}
243271

244272
// TestHelperSerializedReference is not a test of its own: pristineReference

0 commit comments

Comments
 (0)