* perf(issueops): index each step of the per-edge cycle and hierarchy walks The single-edge cycle and hierarchy probes (AddDependencyInTx, i.e. bd dep add and apply-batch dep_add items, the domain stack's HasCycle / ValidateBlockingHierarchy, and apply-batch's end-gate hierarchy check) joined the recursive frontier against a UNION of dependencies and wisp_dependencies. Dolt cannot index that derived table, so every recursion step rescanned every matching edge: on a 50k-issue / 100k-edge database one dep add took minutes (an apply-batch of 1 create + 2 dep adds ~10 min). Give each dependency table its own recursive member joined directly on d.issue_id, so each step is an index lookup, and pin the lookup plan with JOIN_ORDER/LOOKUP_JOIN hints: without them the Dolt sql-server planner picks a type-index scan join while table statistics are not yet available (right after a commit), back to minutes. Same reachable set (UNION distinct merges the members per step), same edge types, same error paths, and still one statement per probe. Adds a "depadd" createbatchequiv scenario with a golden digest recorded from the pre-change code: every cycle/hierarchy probe over 17 nodes x 3 table sets (mixed tables, every target column, external and missing ids), then 20 AddDependencyInTx calls covering cycles through both tables, hierarchy conflicts in both directions, self, missing endpoints, type conflict, external and non-scheduling edges. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * perf(issueops): pin lookup joins in the batched blocked-state recompute The batched mark/unmark UPDATEs decide membership through the scoped should-be-blocked union. On the Dolt sql-server its planner intermittently (depending on when the background statistics refresh lands) turned the union's issues legs around: it drove them from an index scan of every open issue through (is_blocked, status) and probed dependencies per row. Over an import's few thousand uncommitted issues that is ~3 s per leg and 6-10 s per mark/unmark statement for each 200-id chunk, where the lookup plan takes ~0.1 s; an import of 5000 issues with edges took ~28 min and came close to the 10 s read timeout. EXPLAIN on the server engine showed the two plans; two of three identical 2000-issue imports hit the slow one (4-5 min vs 14 s). Pin each joined leg of the scoped union to drive from the batch's own dependency rows and look the target/parent up by primary key (JOIN_ORDER/LOOKUP_JOIN hints, ignored by MySQL). The SQL is otherwise unchanged, so results are identical (createbatchequiv golden digests, including is_blocked, pass on both engines) and the statement count is the same. The unscoped union used by full repair and the doctor count walks whole tables and keeps the planner's choice. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * test(storage): pin the hinted graph-walk and blocked-state plans with EXPLAIN The JOIN_ORDER/LOOKUP_JOIN hints on the per-edge reachability walks and the batched blocked-state recompute were only pinned textually: the suite passed with them deleted or naming a wrong alias. TestGraphWalkPlansHonorJoinHints (embedded) records the statements the production entry points send (WouldCreateSchedulingCycleInTx, CheckBlockingHierarchyInTx, RecomputeIsBlockedInTx) through a DBTX wrapper, EXPLAINs exactly those, and asserts each recursive member is a lookup join keyed by the frontier node and each joined union leg a primary-key lookup, never an is_blocked/status scan. Deleting a hint or misspelling its alias fails it. The test found a remaining planner trap in the ancestor walk: with "d.type = 'parent-child'" in the join condition, the embedded planner satisfied the wisp_dependencies member through the type index keyed by the constant, a lookup returning every parent-child row of the table per frontier node. The type filter now sits in each member's projection (a row of another type yields a NULL node, which joins nothing and never equals the goal), so issue_id is the join's only predicate. The depadd golden, recorded from the pre-change code, still matches. Also corrects the hint comments: MySQL honors JOIN_ORDER (the order it forces is the wanted one) and ignores the unknown LOOKUP_JOIN with a warning. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * test(createbatchequiv): check run scenarios against the golden only; probe a stored parent cycle A run scenario (depadd) drives code the create fast paths do not gate, so its fast-vs-per-row comparison compared a body with itself. Apply it once, check it against its golden digest (the pre-change code's answers), and fail if the run recorded nothing. depadd now plants a parent-child 2-cycle straight into dependencies, as legacy or merged data can hold even though every write path refuses it, and probes across it plus three adds touching it, so termination of the walks on stored cycles is covered. Golden re-recorded froma93498c81(pre-change walks); the current code matches it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * perf(issueops): index the waits-for gate's child lookups; waits-for bench fixture The large-graph benchmark gains a formula-shaped fan-out (batchfixtures.WaitsForInserts: one spawner, 1000 parent-child children, 20 waiters over every waits-for gate shape) and two shapes over it: create+waits-for and closing a spawner child (which recomputes every waiter through the waits-for gate). It was not a statistics-dependent plan flip but a consistently bad plan: each "spawner has an open/closed child" EXISTS matched both spawner columns with one OR, which no index serves, so every correlated evaluation scanned the edge table. On 50k issues / 100k edges, closing one child took ~39 s per close on embedded, and on the server create+waits-for hit the 30-minute read timeout. The gate now splits each EXISTS per spawner column (one per dependency table and column, each served by its target-column index) and pins the child lookup by primary key (JOIN_ORDER/LOOKUP_JOIN). EXISTS(a OR b) is EXISTS(a) OR EXISTS(b), so the result is unchanged. The template is shared with the unscoped full repair, where a per-row correlated lookup is the right plan too. Close spawner child per close: emb 39.5 s -> 3.3 s; create+waits-for emb 2.1 s -> 0.46 s, srv >30 min (read timeout) -> 0.29 s; srv close spawner child 3.2 s (not reached before). Adds a "waitsfor" createbatchequiv scenario, golden recorded froma93498c81(pre-change gate): issue and wisp spawners with issue and wisp children, waiters in all five gate shapes, then closes, reopens, a scoped recompute and the full repair, snapshotting every is_blocked. A mutation dropping the wisp-spawner legs fails it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * test(createbatchequiv): compare dependency metadata canonicalized across engines The embedded engine and the Dolt sql-server serialize a JSON metadata value differently (key order, spacing), so the waitsfor golden recorded on embedded failed on the server for its gate metadata alone. Re-serialize each dependency row's metadata through encoding/json before comparing. Rows with '{}' metadata are unchanged, so the other goldens keep their digests; waitsfor is re-recorded froma93498c81and now passes on both engines. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * build(bazel): auto-sync BUILD files Applied from the bazel-sync-patch artifact of https://github.com/gastownhall/beads/actions/runs/37272437653 (`make bazel-sync`: gazelle, tools/bazel/go_srcs.py, bazel mod tidy). Only BUILD.bazel, MODULE.bazel and MODULE.bazel.lock are ever pushed; see scripts/bazel-autofix-push.sh. --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
218 lines
8.6 KiB
Go
218 lines
8.6 KiB
Go
// Package batchbench measures single and batched creates against a database
|
|
// that already holds a large dependency graph (batchfixtures.LargeGraphInserts),
|
|
// the case a fresh-database benchmark cannot show: per-create work that
|
|
// scales with the size of the stored graph rather than with the request.
|
|
// Each backend's test package runs Run against its own engine, opt-in via
|
|
// EnvVar, and the numbers are logged rather than asserted (wall time and
|
|
// allocation are host-sensitive).
|
|
package batchbench
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/storage/batchfixtures"
|
|
"github.com/steveyegge/beads/internal/storage/issueops"
|
|
"github.com/steveyegge/beads/internal/storage/sqlcount"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
publicops "github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
// EnvVar opts in: set it to the number of graph issues to pre-populate
|
|
// (about two edges each), e.g. 50000 for ~100k edges.
|
|
const EnvVar = "BEADS_BENCH_LARGE_GRAPH"
|
|
|
|
// CreatesEnvVar optionally sets how many creates each measured shape
|
|
// performs (default DefaultCreates): the per-edge recursive checks of older
|
|
// code take seconds per create on a large graph.
|
|
const CreatesEnvVar = "BEADS_BENCH_LARGE_GRAPH_CREATES"
|
|
|
|
// DefaultCreates is the number of creates per shape when CreatesEnvVar is
|
|
// unset.
|
|
const DefaultCreates = 20
|
|
|
|
// ApplyCreatesEnvVar optionally overrides the apply-batch shape's create
|
|
// count on its own: its dep_add items take AddDependencyInTx's per-edge
|
|
// recursive checks, which run for minutes per edge on a 100k-edge graph.
|
|
const ApplyCreatesEnvVar = "BEADS_BENCH_LARGE_GRAPH_APPLY_CREATES"
|
|
|
|
// waitsForChildren and waitsForWaiters size the formula-shaped fan-out
|
|
// (batchfixtures.WaitsForInserts) loaded beside the graph: one spawner with
|
|
// that many parent-child children and waiters each holding a waits-for edge
|
|
// on it, the shape whose blocked state the waits-for gate decides.
|
|
const (
|
|
waitsForChildren = 1000
|
|
waitsForWaiters = 20
|
|
)
|
|
|
|
// Issues returns the graph size EnvVar asks for, skipping t when unset.
|
|
func Issues(t *testing.T) int {
|
|
t.Helper()
|
|
n, err := strconv.Atoi(os.Getenv(EnvVar))
|
|
if err != nil || n <= 0 {
|
|
t.Skipf("set %s=<issues> (e.g. 50000) to run the large-graph create benchmark", EnvVar)
|
|
}
|
|
return n
|
|
}
|
|
|
|
// Run populates db (a fresh migrated database) with issues graph issues
|
|
// under prefix, commits them as a Dolt version, then measures three shapes,
|
|
// each performing CreatesEnvVar (default DefaultCreates) creates, logging
|
|
// wall time, statements and Go heap allocation:
|
|
//
|
|
// - create+parent+blocker: ExecuteCreate (the guarded single create every
|
|
// surface's `bd create --parent P --deps B` reaches) under an existing
|
|
// parent with an existing blocker, one transaction per create;
|
|
// - create+2deps: CreateIssuesInTxWithResult with one issue carrying two
|
|
// blocks edges to existing issues (the store's CreateIssue path);
|
|
// - apply-batch: one ApplyBatchInTx of that many creates, each followed by a
|
|
// parent-child and a blocks dep_add to existing issues;
|
|
// - create+waits-for: CreateIssuesInTxWithResult with one issue carrying a
|
|
// waits-for edge on the fan-out's spawner, so its blocked state is decided
|
|
// by the waits-for gate over the spawner's children;
|
|
// - close spawner child: CloseIssueInTx on an open child of the spawner,
|
|
// which recomputes every waiter through the waits-for gate.
|
|
func Run(t *testing.T, db *sql.DB, counts *sqlcount.Counts, prefix string, issues int) {
|
|
t.Helper()
|
|
creates := DefaultCreates
|
|
if n, err := strconv.Atoi(os.Getenv(CreatesEnvVar)); err == nil && n > 0 {
|
|
creates = n
|
|
}
|
|
ctx := context.Background()
|
|
start := time.Now()
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
inserts := batchfixtures.LargeGraphInserts(prefix, issues)
|
|
inserts = append(inserts, batchfixtures.WaitsForInserts(prefix, waitsForChildren, waitsForWaiters)...)
|
|
for _, st := range inserts {
|
|
if _, err := tx.ExecContext(ctx, st.SQL, st.Args...); err != nil {
|
|
_ = tx.Rollback()
|
|
t.Fatalf("populate: %v", err)
|
|
}
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := db.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', 'large graph fixture')"); err != nil {
|
|
t.Fatalf("commit fixture: %v", err)
|
|
}
|
|
var edges int
|
|
if err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM dependencies").Scan(&edges); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("large graph: %d issues, %d edges, populated in %s", issues, edges, time.Since(start).Round(time.Millisecond))
|
|
|
|
pick := func(i, salt int) string { return batchfixtures.LargeGraphID(prefix, (i*7919+salt*104729)%issues) }
|
|
measure := func(name string, creates int, body func()) {
|
|
var before, after runtime.MemStats
|
|
runtime.GC()
|
|
runtime.ReadMemStats(&before)
|
|
counts.Reset()
|
|
start := time.Now()
|
|
body()
|
|
elapsed := time.Since(start)
|
|
runtime.ReadMemStats(&after)
|
|
s := counts.Snapshot()
|
|
t.Logf("BENCH %-22s %d creates: %8s total, %6.1f ms/create, %6d statements, %8.1f MiB Go alloc",
|
|
name, creates, elapsed.Round(time.Millisecond), float64(elapsed.Milliseconds())/float64(creates), s.Total(),
|
|
float64(after.TotalAlloc-before.TotalAlloc)/(1<<20))
|
|
}
|
|
inTx := func(body func(tx *sql.Tx) error) {
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := body(tx); err != nil {
|
|
_ = tx.Rollback()
|
|
t.Fatal(err)
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
measure("create+parent+blocker", creates, func() {
|
|
for i := 0; i < creates; i++ {
|
|
inTx(func(tx *sql.Tx) error {
|
|
_, _, err := issueops.ExecuteCreate(ctx, tx, publicops.CreateRequest{
|
|
Actor: "bench",
|
|
Issue: &types.Issue{Title: fmt.Sprintf("bench child %d", i), Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask},
|
|
ParentID: pick(i, 1),
|
|
Dependencies: []publicops.CreateDependency{{TargetID: pick(i, 2), Type: types.DepBlocks}},
|
|
})
|
|
return err
|
|
})
|
|
}
|
|
})
|
|
measure("create+2deps", creates, func() {
|
|
for i := 0; i < creates; i++ {
|
|
inTx(func(tx *sql.Tx) error {
|
|
id := fmt.Sprintf("%s-b2d%03d", prefix, i)
|
|
_, err := issueops.CreateIssuesInTxWithResult(ctx, tx, []*types.Issue{{
|
|
ID: id, Title: fmt.Sprintf("bench two deps %d", i), Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask,
|
|
Dependencies: []*types.Dependency{
|
|
{IssueID: id, DependsOnID: pick(i, 3), Type: types.DepBlocks},
|
|
{IssueID: id, DependsOnID: pick(i, 4), Type: types.DepBlocks},
|
|
},
|
|
}}, "bench", storage.BatchCreateOptions{})
|
|
return err
|
|
})
|
|
}
|
|
})
|
|
measure("create+waits-for", creates, func() {
|
|
for i := 0; i < creates; i++ {
|
|
inTx(func(tx *sql.Tx) error {
|
|
id := fmt.Sprintf("%s-bwf%03d", prefix, i)
|
|
_, err := issueops.CreateIssuesInTxWithResult(ctx, tx, []*types.Issue{{
|
|
ID: id, Title: fmt.Sprintf("bench waits-for %d", i), Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask,
|
|
Dependencies: []*types.Dependency{{IssueID: id, DependsOnID: batchfixtures.WaitsForSpawnerID(prefix), Type: types.DepWaitsFor}},
|
|
}}, "bench", storage.BatchCreateOptions{})
|
|
return err
|
|
})
|
|
}
|
|
})
|
|
measure("close spawner child", creates, func() {
|
|
for i := 0; i < creates; i++ {
|
|
inTx(func(tx *sql.Tx) error {
|
|
// Children 10k+1.. are open (every tenth is fixture-closed).
|
|
_, err := issueops.CloseIssueInTx(ctx, tx, batchfixtures.WaitsForChildID(prefix, 10*i+1), "bench", "bench", "")
|
|
return err
|
|
})
|
|
}
|
|
})
|
|
applyCreates := creates
|
|
if n, err := strconv.Atoi(os.Getenv(ApplyCreatesEnvVar)); err == nil && n > 0 {
|
|
applyCreates = n
|
|
}
|
|
measure("apply-batch", applyCreates, func() {
|
|
var items []publicops.ApplyItem
|
|
for i := 0; i < applyCreates; i++ {
|
|
key := fmt.Sprintf("k%d", i)
|
|
items = append(items,
|
|
publicops.ApplyItem{Kind: publicops.ItemCreate, Create: &publicops.CreateItem{Key: key, Issue: &types.Issue{
|
|
Title: fmt.Sprintf("bench apply %d", i), Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask}}},
|
|
publicops.ApplyItem{Kind: publicops.ItemDepAdd, DepAdd: &publicops.DepAddItem{
|
|
Source: publicops.Ref{Key: key}, Target: publicops.Ref{ID: pick(i, 5)}, Type: publicops.DepParentChild}},
|
|
publicops.ApplyItem{Kind: publicops.ItemDepAdd, DepAdd: &publicops.DepAddItem{
|
|
Source: publicops.Ref{Key: key}, Target: publicops.Ref{ID: pick(i, 6)}, Type: publicops.DepBlocks}},
|
|
)
|
|
}
|
|
plan, err := storage.PlanApplyBatch(publicops.ApplyBatchRequest{Actor: "bench", Items: items})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
inTx(func(tx *sql.Tx) error {
|
|
_, _, err := issueops.ApplyBatchInTx(ctx, tx, plan)
|
|
return err
|
|
})
|
|
})
|
|
}
|