test: add perf reference benchmarks (#4032)
* test: add perf reference benchmarks * test: tighten perf reference benchmarks * Fix perf benchmark review findings (bd-fsgqsj)
This commit is contained in:
parent
8f5ec4611f
commit
8517ba1afa
6 changed files with 2625 additions and 4 deletions
|
|
@ -41,6 +41,8 @@ Tests on graphs with different topologies (linear chains, trees, dense graphs):
|
|||
### Search Operations
|
||||
- **BenchmarkSearchIssues_Large_NoFilter** - Search all open issues (10K dataset)
|
||||
- **BenchmarkSearchIssues_Large_ComplexFilter** - Search with priority/status filters (10K dataset)
|
||||
- **BenchmarkPerfSearchTypedLabelFilter_5K** - Label/type search over a 5K issue/label catalog
|
||||
- **BenchmarkPerfResolvePartialIDInvalidInput_5K** - Invalid partial-ID rejection without a broad fallback scan
|
||||
|
||||
### CRUD Operations
|
||||
- **BenchmarkCreateIssue_Large** - Create new issue in 10K database
|
||||
|
|
@ -51,6 +53,54 @@ Tests on graphs with different topologies (linear chains, trees, dense graphs):
|
|||
- **BenchmarkLargeDescription** - Handling 100KB+ issue descriptions (NEW)
|
||||
- **BenchmarkSyncMerge** - Simulate sync cycle with create/update operations (NEW)
|
||||
|
||||
### Recent Perf Regression References
|
||||
|
||||
These benchmarks cover the May 2026 Dolt hot-path changes so future perf PRs can run before/after checks against the same fixture shapes:
|
||||
|
||||
| PR / change | Benchmark |
|
||||
|-------------|-----------|
|
||||
| #3966 `perf(deps): narrow recursive cycle checks` | `BenchmarkPerfAddDependencyCycleCheck_DiamondDAG` |
|
||||
| #3967 `perf(search): tighten label and partial-id queries` | `BenchmarkPerfSearchTypedLabelFilter_5K`, `BenchmarkPerfResolvePartialIDInvalidInput_5K` |
|
||||
| #3968 `perf(ready): page blocked checks for limited ready work` | `BenchmarkPerfReadyWorkLimited_LargeBlockedGraph` |
|
||||
| #4001 `perf(ready): narrow deferred-parent child filtering` | `BenchmarkPerfReadyWorkDeferredParentExclusion_5K` |
|
||||
| #4002 `perf(ready): restrict blocked dependency scans to active IDs` | `BenchmarkPerfBlockedIssues_ClosedDependencySkew` |
|
||||
| #4003 `perf(get): query primary issues before wisp fallback` | `BenchmarkPerfGetIssuePrimaryFirst_PermanentWithWisps` |
|
||||
| #4004 `perf(deps): scan one cycle table for same-storage edges` | No standalone executable perf diff in the landed squash; covered by the cycle-check benchmark above |
|
||||
|
||||
Measured with `-benchtime=1x -benchmem -count=1` on the same host, copying this benchmark file onto each before/after ref:
|
||||
|
||||
| PR / path | Benchmark | Before | After | Time gain | Alloc gain |
|
||||
|-----------|-----------|--------|-------|-----------|------------|
|
||||
| #3967 label/type search | `BenchmarkPerfSearchTypedLabelFilter_5K` | 134.8 ms | 51.8 ms | 61.6% | -0.1% |
|
||||
| #3967 invalid partial-ID fallback | `BenchmarkPerfResolvePartialIDInvalidInput_5K` | 124.3 ms | 22.5 ms | 81.9% | 43.6% |
|
||||
| #3966 dependency cycle check | `BenchmarkPerfAddDependencyCycleCheck_DiamondDAG` | 80.0 ms | 25.8 ms | 67.7% | 1.4% |
|
||||
| #3968 limited ready work | `BenchmarkPerfReadyWorkLimited_LargeBlockedGraph` | 1677.4 ms | 341.7 ms | 79.6% | 85.4% |
|
||||
| #4001 deferred parent exclusion | `BenchmarkPerfReadyWorkDeferredParentExclusion_5K` | 3257.3 ms | 130.8 ms | 96.0% | 83.1% |
|
||||
| #4002 active blocked-dep scan | `BenchmarkPerfBlockedIssues_ClosedDependencySkew` | 44.3 ms | 36.2 ms | 18.1% | 96.0% |
|
||||
| #4003 primary issue lookup | `BenchmarkPerfGetIssuePrimaryFirst_PermanentWithWisps` | 9.0 ms | 6.4 ms | 28.7% | 10.7% |
|
||||
|
||||
Run the recent perf reference set with:
|
||||
|
||||
```bash
|
||||
go test -run=^$ -bench='BenchmarkPerf(SearchTypedLabelFilter|ResolvePartialIDInvalidInput|AddDependencyCycleCheck|ReadyWorkLimited|BlockedIssues|ReadyWorkDeferredParentExclusion|GetIssuePrimaryFirst)' -benchtime=1x -benchmem ./internal/storage/dolt
|
||||
```
|
||||
|
||||
For production-shaped CLI timeout and index experiments, use:
|
||||
|
||||
```bash
|
||||
go run ./scripts/repro-dolt-prod-timeouts --bd ./bd --scenario all
|
||||
go run ./scripts/bench-ready-indexes --dsn 'root@tcp(127.0.0.1:33307)/mc?timeout=30s&readTimeout=30s&writeTimeout=30s'
|
||||
```
|
||||
|
||||
When `repro-dolt-prod-timeouts` targets an existing workspace with
|
||||
`--workspace`, fixture seeding defaults to `--seed-mode=none`; pass
|
||||
`--seed-mode=full` or `--seed-mode=dep-only` only when intentionally writing
|
||||
and committing synthetic fixture rows into that workspace.
|
||||
|
||||
`bench-ready-indexes` drops its candidate indexes again before exit by default;
|
||||
pass `--keep-indexes` only when intentionally leaving the final index set
|
||||
installed.
|
||||
|
||||
## Performance Targets
|
||||
|
||||
### Typical Results (M2 Pro)
|
||||
|
|
|
|||
|
|
@ -11,14 +11,23 @@ package dolt
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
"github.com/steveyegge/beads/internal/utils"
|
||||
)
|
||||
|
||||
func benchDatabaseName() string {
|
||||
return fmt.Sprintf("beads_test_bench_%d", time.Now().UnixNano())
|
||||
}
|
||||
|
||||
// setupBenchStore creates a store for benchmarks
|
||||
func setupBenchStore(b *testing.B) (*DoltStore, func()) {
|
||||
b.Helper()
|
||||
|
|
@ -42,7 +51,7 @@ func setupBenchStore(b *testing.B) (*DoltStore, func()) {
|
|||
Path: tmpDir,
|
||||
CommitterName: "bench",
|
||||
CommitterEmail: "bench@example.com",
|
||||
Database: "benchdb",
|
||||
Database: benchDatabaseName(),
|
||||
CreateIfMissing: true,
|
||||
}
|
||||
|
||||
|
|
@ -94,7 +103,7 @@ func BenchmarkBootstrapEmbedded(b *testing.B) {
|
|||
Path: tmpDir,
|
||||
CommitterName: "bench",
|
||||
CommitterEmail: "bench@example.com",
|
||||
Database: "benchdb",
|
||||
Database: benchDatabaseName(),
|
||||
CreateIfMissing: true,
|
||||
}
|
||||
|
||||
|
|
@ -136,13 +145,14 @@ func BenchmarkColdStart(b *testing.B) {
|
|||
|
||||
// Get the path for reopening
|
||||
tmpDir := store.dbPath
|
||||
dbName := store.database
|
||||
store.Close()
|
||||
|
||||
cfg := &Config{
|
||||
Path: tmpDir,
|
||||
CommitterName: "bench",
|
||||
CommitterEmail: "bench@example.com",
|
||||
Database: "benchdb",
|
||||
Database: dbName,
|
||||
CreateIfMissing: true,
|
||||
}
|
||||
|
||||
|
|
@ -220,13 +230,14 @@ func BenchmarkCLIWorkflow(b *testing.B) {
|
|||
}
|
||||
|
||||
tmpDir := store.dbPath
|
||||
dbName := store.database
|
||||
store.Close()
|
||||
|
||||
cfg := &Config{
|
||||
Path: tmpDir,
|
||||
CommitterName: "bench",
|
||||
CommitterEmail: "bench@example.com",
|
||||
Database: "benchdb",
|
||||
Database: dbName,
|
||||
CreateIfMissing: true,
|
||||
}
|
||||
|
||||
|
|
@ -909,6 +920,534 @@ func BenchmarkGetReadyWork(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Recent production hot-path regression benchmarks (May 2026 perf stack)
|
||||
// =============================================================================
|
||||
|
||||
func createBenchIssueBatch(b *testing.B, store *DoltStore, issues []*types.Issue) {
|
||||
b.Helper()
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
err := store.withRetryTx(ctx, func(tx *sql.Tx) error {
|
||||
issuesByTable := map[string][]*types.Issue{
|
||||
"issues": nil,
|
||||
"wisps": nil,
|
||||
}
|
||||
for _, issue := range issues {
|
||||
if issue.Status == "" {
|
||||
issue.Status = types.StatusOpen
|
||||
}
|
||||
if issue.Priority == 0 {
|
||||
issue.Priority = 2
|
||||
}
|
||||
if issue.IssueType == "" {
|
||||
issue.IssueType = types.TypeTask
|
||||
}
|
||||
if issue.CreatedAt.IsZero() {
|
||||
issue.CreatedAt = now
|
||||
}
|
||||
if issue.UpdatedAt.IsZero() {
|
||||
issue.UpdatedAt = issue.CreatedAt
|
||||
}
|
||||
issue.ContentHash = issue.ComputeContentHash()
|
||||
|
||||
issueTable, _ := issueops.TableRouting(issue)
|
||||
issuesByTable[issueTable] = append(issuesByTable[issueTable], issue)
|
||||
}
|
||||
for table, tableIssues := range issuesByTable {
|
||||
if err := insertBenchIssues(ctx, tx, table, tableIssues); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := insertBenchLabels(ctx, tx, table, tableIssues); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := insertBenchDependencies(ctx, tx, "dependencies", issuesByTable["issues"], now); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := insertBenchDependencies(ctx, tx, "wisp_dependencies", issuesByTable["wisps"], now); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
b.Fatalf("failed to create %d benchmark issues: %v", len(issues), err)
|
||||
}
|
||||
}
|
||||
|
||||
func insertBenchIssues(ctx context.Context, tx *sql.Tx, table string, issues []*types.Issue) error {
|
||||
const batchSize = 500
|
||||
for start := 0; start < len(issues); start += batchSize {
|
||||
end := start + batchSize
|
||||
if end > len(issues) {
|
||||
end = len(issues)
|
||||
}
|
||||
var placeholders []string
|
||||
var args []interface{}
|
||||
for _, issue := range issues[start:end] {
|
||||
placeholders = append(placeholders, "(?, ?, ?, '', '', '', '', ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
||||
args = append(args,
|
||||
issue.ID,
|
||||
issue.ContentHash,
|
||||
issue.Title,
|
||||
issue.Status,
|
||||
issue.Priority,
|
||||
issue.IssueType,
|
||||
issue.CreatedAt,
|
||||
issue.UpdatedAt,
|
||||
issue.DeferUntil,
|
||||
issue.Ephemeral,
|
||||
issue.NoHistory,
|
||||
issue.Pinned,
|
||||
)
|
||||
}
|
||||
//nolint:gosec // G201: table is selected from fixed issue table names.
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO %s (
|
||||
id, content_hash, title, description, design, acceptance_criteria, notes,
|
||||
status, priority, issue_type, created_at, updated_at, defer_until,
|
||||
ephemeral, no_history, pinned
|
||||
) VALUES %s
|
||||
ON DUPLICATE KEY UPDATE title = VALUES(title)
|
||||
`, table, strings.Join(placeholders, ","))
|
||||
if _, err := tx.ExecContext(ctx, query, args...); err != nil {
|
||||
return fmt.Errorf("insert benchmark issues into %s: %w", table, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertBenchLabels(ctx context.Context, tx *sql.Tx, issueTable string, issues []*types.Issue) error {
|
||||
const batchSize = 500
|
||||
labelTable := "labels"
|
||||
if issueTable == "wisps" {
|
||||
labelTable = "wisp_labels"
|
||||
}
|
||||
var issueIDs []string
|
||||
var labels []string
|
||||
for _, issue := range issues {
|
||||
for _, label := range issue.Labels {
|
||||
issueIDs = append(issueIDs, issue.ID)
|
||||
labels = append(labels, label)
|
||||
}
|
||||
}
|
||||
for start := 0; start < len(labels); start += batchSize {
|
||||
end := start + batchSize
|
||||
if end > len(labels) {
|
||||
end = len(labels)
|
||||
}
|
||||
var placeholders []string
|
||||
var args []interface{}
|
||||
for i := start; i < end; i++ {
|
||||
placeholders = append(placeholders, "(?, ?)")
|
||||
args = append(args, issueIDs[i], labels[i])
|
||||
}
|
||||
//nolint:gosec // G201: labelTable is selected from fixed label table names.
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO %s (issue_id, label)
|
||||
VALUES %s
|
||||
ON DUPLICATE KEY UPDATE label = label
|
||||
`, labelTable, strings.Join(placeholders, ","))
|
||||
if _, err := tx.ExecContext(ctx, query, args...); err != nil {
|
||||
return fmt.Errorf("insert benchmark labels into %s: %w", labelTable, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertBenchDependencies(ctx context.Context, tx *sql.Tx, depTable string, issues []*types.Issue, now time.Time) error {
|
||||
type benchDependencyRow struct {
|
||||
issueID string
|
||||
dependsOnID string
|
||||
depType types.DependencyType
|
||||
createdAt time.Time
|
||||
}
|
||||
|
||||
const batchSize = 500
|
||||
rowsByColumn := map[string][]benchDependencyRow{}
|
||||
for _, issue := range issues {
|
||||
for _, dep := range issue.Dependencies {
|
||||
if dep.IssueID == "" {
|
||||
dep.IssueID = issue.ID
|
||||
}
|
||||
createdAt := dep.CreatedAt
|
||||
if createdAt.IsZero() {
|
||||
createdAt = now
|
||||
}
|
||||
column := issueops.ClassifyDepTarget(ctx, tx, dep, false).Column()
|
||||
rowsByColumn[column] = append(rowsByColumn[column], benchDependencyRow{
|
||||
issueID: dep.IssueID,
|
||||
dependsOnID: dep.DependsOnID,
|
||||
depType: dep.Type,
|
||||
createdAt: createdAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
for column, rows := range rowsByColumn {
|
||||
for start := 0; start < len(rows); start += batchSize {
|
||||
end := start + batchSize
|
||||
if end > len(rows) {
|
||||
end = len(rows)
|
||||
}
|
||||
var placeholders []string
|
||||
var args []interface{}
|
||||
for _, row := range rows[start:end] {
|
||||
placeholders = append(placeholders, "(?, ?, ?, 'bench', ?, '{}')")
|
||||
args = append(args, row.issueID, row.dependsOnID, row.depType, row.createdAt)
|
||||
}
|
||||
//nolint:gosec // G201: depTable is fixed and column comes from issueops.DepTargetKind.Column.
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO %s (issue_id, %s, type, created_by, created_at, metadata)
|
||||
VALUES %s
|
||||
ON DUPLICATE KEY UPDATE type = type
|
||||
`, depTable, column, strings.Join(placeholders, ","))
|
||||
if _, err := tx.ExecContext(ctx, query, args...); err != nil {
|
||||
return fmt.Errorf("insert benchmark dependencies into %s: %w", depTable, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func BenchmarkPerfSearchTypedLabelFilter_5K(b *testing.B) {
|
||||
store, cleanup := setupBenchStore(b)
|
||||
defer cleanup()
|
||||
|
||||
const total = 5000
|
||||
issues := make([]*types.Issue, 0, total)
|
||||
for i := 0; i < total; i++ {
|
||||
labels := []string{fmt.Sprintf("bucket-%03d", i%500)}
|
||||
issueType := types.TypeBug
|
||||
if i%3 == 0 {
|
||||
issueType = types.TypeFeature
|
||||
}
|
||||
if i%40 == 0 {
|
||||
issueType = types.TypeTask
|
||||
labels = append(labels, "perf-hot")
|
||||
}
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-search-%05d", i),
|
||||
Title: fmt.Sprintf("Search label benchmark issue %05d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: (i % 4) + 1,
|
||||
IssueType: issueType,
|
||||
Labels: labels,
|
||||
})
|
||||
}
|
||||
createBenchIssueBatch(b, store, issues)
|
||||
|
||||
ctx := context.Background()
|
||||
taskType := types.TypeTask
|
||||
filter := types.IssueFilter{IssueType: &taskType, Labels: []string{"perf-hot"}, Limit: 200}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
results, err := store.SearchIssues(ctx, "", filter)
|
||||
if err != nil {
|
||||
b.Fatalf("SearchIssues label filter: %v", err)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
b.Fatal("SearchIssues label filter returned no issues")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPerfResolvePartialIDInvalidInput_5K(b *testing.B) {
|
||||
store, cleanup := setupBenchStore(b)
|
||||
defer cleanup()
|
||||
|
||||
const total = 5000
|
||||
issues := make([]*types.Issue, 0, total)
|
||||
for i := 0; i < total; i++ {
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-partial-%05d", i),
|
||||
Title: fmt.Sprintf("Partial ID benchmark issue %05d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: (i % 4) + 1,
|
||||
IssueType: types.TypeTask,
|
||||
})
|
||||
}
|
||||
createBenchIssueBatch(b, store, issues)
|
||||
|
||||
ctx := context.Background()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if _, err := utils.ResolvePartialID(ctx, store, "not/a/valid/id"); err == nil {
|
||||
b.Fatal("ResolvePartialID invalid input unexpectedly succeeded")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPerfAddDependencyCycleCheck_DiamondDAG(b *testing.B) {
|
||||
store, cleanup := setupBenchStore(b)
|
||||
defer cleanup()
|
||||
|
||||
const layers = 10
|
||||
issues := make([]*types.Issue, 0, 2*layers+1)
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: "bench-perf-cycle-source",
|
||||
Title: "Cycle source",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
})
|
||||
for layer := 0; layer < layers; layer++ {
|
||||
for _, suffix := range []string{"a", "b"} {
|
||||
issue := &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-cycle-l%02d-%s", layer, suffix),
|
||||
Title: fmt.Sprintf("Cycle diamond layer %02d %s", layer, suffix),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
}
|
||||
if layer < layers-1 {
|
||||
issue.Dependencies = []*types.Dependency{
|
||||
{DependsOnID: fmt.Sprintf("bench-perf-cycle-l%02d-a", layer+1), Type: types.DepBlocks},
|
||||
{DependsOnID: fmt.Sprintf("bench-perf-cycle-l%02d-b", layer+1), Type: types.DepBlocks},
|
||||
}
|
||||
}
|
||||
issues = append(issues, issue)
|
||||
}
|
||||
}
|
||||
createBenchIssueBatch(b, store, issues)
|
||||
|
||||
ctx := context.Background()
|
||||
dep := &types.Dependency{
|
||||
IssueID: "bench-perf-cycle-source",
|
||||
DependsOnID: "bench-perf-cycle-l00-a",
|
||||
Type: types.DepBlocks,
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := store.AddDependency(ctx, dep, "bench"); err != nil {
|
||||
b.Fatalf("AddDependency cycle check: %v", err)
|
||||
}
|
||||
b.StopTimer()
|
||||
if err := store.RemoveDependency(ctx, dep.IssueID, dep.DependsOnID, "bench"); err != nil {
|
||||
b.Fatalf("RemoveDependency cycle check fixture reset: %v", err)
|
||||
}
|
||||
if i < b.N-1 {
|
||||
b.StartTimer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPerfReadyWorkLimited_LargeBlockedGraph(b *testing.B) {
|
||||
store, cleanup := setupBenchStore(b)
|
||||
defer cleanup()
|
||||
|
||||
const (
|
||||
readyCount = 100
|
||||
blockedPairCount = 2500
|
||||
estimatedRowCount = readyCount + 2*blockedPairCount
|
||||
)
|
||||
issues := make([]*types.Issue, 0, estimatedRowCount)
|
||||
for i := 0; i < readyCount; i++ {
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-ready-clear-%04d", i),
|
||||
Title: fmt.Sprintf("Ready issue %04d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
})
|
||||
}
|
||||
for i := 0; i < blockedPairCount; i++ {
|
||||
blockerID := fmt.Sprintf("bench-perf-ready-blocker-%05d", i)
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: blockerID,
|
||||
Title: fmt.Sprintf("Ready blocker %05d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
})
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-ready-blocked-%05d", i),
|
||||
Title: fmt.Sprintf("Blocked ready issue %05d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Dependencies: []*types.Dependency{
|
||||
{DependsOnID: blockerID, Type: types.DepBlocks},
|
||||
},
|
||||
})
|
||||
}
|
||||
createBenchIssueBatch(b, store, issues)
|
||||
|
||||
ctx := context.Background()
|
||||
filter := types.WorkFilter{Limit: 50, SortPolicy: types.SortPolicyPriority}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
results, err := store.GetReadyWork(ctx, filter)
|
||||
if err != nil {
|
||||
b.Fatalf("GetReadyWork limited blocked graph: %v", err)
|
||||
}
|
||||
if len(results) != filter.Limit {
|
||||
b.Fatalf("GetReadyWork limited blocked graph returned %d issues, want %d", len(results), filter.Limit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPerfBlockedIssues_ClosedDependencySkew(b *testing.B) {
|
||||
store, cleanup := setupBenchStore(b)
|
||||
defer cleanup()
|
||||
|
||||
const (
|
||||
closedPairCount = 2500
|
||||
activePairCount = 10
|
||||
)
|
||||
issues := make([]*types.Issue, 0, 2*(closedPairCount+activePairCount))
|
||||
for i := 0; i < closedPairCount; i++ {
|
||||
blockerID := fmt.Sprintf("bench-perf-closed-blocker-%05d", i)
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: blockerID,
|
||||
Title: fmt.Sprintf("Closed blocker %05d", i),
|
||||
Status: types.StatusClosed,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
})
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-closed-blocked-%05d", i),
|
||||
Title: fmt.Sprintf("Closed blocked issue %05d", i),
|
||||
Status: types.StatusClosed,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Dependencies: []*types.Dependency{
|
||||
{DependsOnID: blockerID, Type: types.DepBlocks},
|
||||
},
|
||||
})
|
||||
}
|
||||
for i := 0; i < activePairCount; i++ {
|
||||
blockerID := fmt.Sprintf("bench-perf-active-blocker-%02d", i)
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: blockerID,
|
||||
Title: fmt.Sprintf("Active blocker %02d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
})
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-active-blocked-%02d", i),
|
||||
Title: fmt.Sprintf("Active blocked issue %02d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
Dependencies: []*types.Dependency{
|
||||
{DependsOnID: blockerID, Type: types.DepBlocks},
|
||||
},
|
||||
})
|
||||
}
|
||||
createBenchIssueBatch(b, store, issues)
|
||||
|
||||
ctx := context.Background()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
results, err := store.GetBlockedIssues(ctx, types.WorkFilter{})
|
||||
if err != nil {
|
||||
b.Fatalf("GetBlockedIssues closed skew: %v", err)
|
||||
}
|
||||
if len(results) != activePairCount {
|
||||
b.Fatalf("GetBlockedIssues closed skew returned %d issues, want %d", len(results), activePairCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPerfReadyWorkDeferredParentExclusion_5K(b *testing.B) {
|
||||
store, cleanup := setupBenchStore(b)
|
||||
defer cleanup()
|
||||
|
||||
future := time.Now().UTC().Add(24 * time.Hour)
|
||||
const (
|
||||
readyCount = 100
|
||||
deferredParentCount = 5000
|
||||
deferredChildCount = 50
|
||||
)
|
||||
issues := make([]*types.Issue, 0, readyCount+deferredParentCount+deferredChildCount)
|
||||
for i := 0; i < readyCount; i++ {
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-deferred-ready-%03d", i),
|
||||
Title: fmt.Sprintf("Ready non-deferred issue %03d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
})
|
||||
}
|
||||
for i := 0; i < deferredParentCount; i++ {
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-deferred-parent-%05d", i),
|
||||
Title: fmt.Sprintf("Future deferred parent %05d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
DeferUntil: &future,
|
||||
})
|
||||
}
|
||||
for i := 0; i < deferredChildCount; i++ {
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-deferred-child-%03d", i),
|
||||
Title: fmt.Sprintf("Child of deferred parent %03d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
Dependencies: []*types.Dependency{
|
||||
{DependsOnID: fmt.Sprintf("bench-perf-deferred-parent-%05d", i), Type: types.DepParentChild},
|
||||
},
|
||||
})
|
||||
}
|
||||
createBenchIssueBatch(b, store, issues)
|
||||
|
||||
ctx := context.Background()
|
||||
filter := types.WorkFilter{Limit: 50, SortPolicy: types.SortPolicyPriority}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
results, err := store.GetReadyWork(ctx, filter)
|
||||
if err != nil {
|
||||
b.Fatalf("GetReadyWork deferred parent exclusion: %v", err)
|
||||
}
|
||||
if len(results) != filter.Limit {
|
||||
b.Fatalf("GetReadyWork deferred parent exclusion returned %d issues, want %d", len(results), filter.Limit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPerfGetIssuePrimaryFirst_PermanentWithWisps(b *testing.B) {
|
||||
store, cleanup := setupBenchStore(b)
|
||||
defer cleanup()
|
||||
|
||||
const wispCount = 5000
|
||||
issues := make([]*types.Issue, 0, wispCount+1)
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: "bench-perf-get-primary",
|
||||
Title: "Permanent issue fetched from primary table",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
})
|
||||
for i := 0; i < wispCount; i++ {
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: fmt.Sprintf("bench-perf-get-wisp-%04d", i),
|
||||
Title: fmt.Sprintf("Wisp noise issue %04d", i),
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Ephemeral: true,
|
||||
})
|
||||
}
|
||||
createBenchIssueBatch(b, store, issues)
|
||||
|
||||
ctx := context.Background()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
issue, err := store.GetIssue(ctx, "bench-perf-get-primary")
|
||||
if err != nil {
|
||||
b.Fatalf("GetIssue permanent with wisps: %v", err)
|
||||
}
|
||||
if issue == nil || issue.ID != "bench-perf-get-primary" {
|
||||
b.Fatalf("GetIssue permanent with wisps returned %v", issue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Label Benchmarks
|
||||
// =============================================================================
|
||||
|
|
|
|||
303
scripts/bench-ready-indexes/main.go
Normal file
303
scripts/bench-ready-indexes/main.go
Normal file
|
|
@ -0,0 +1,303 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
type candidateIndex struct {
|
||||
Name string
|
||||
Table string
|
||||
SQL string
|
||||
Drop string
|
||||
}
|
||||
|
||||
type queryCase struct {
|
||||
Name string
|
||||
Query string
|
||||
Args []any
|
||||
}
|
||||
|
||||
type result struct {
|
||||
Name string
|
||||
Count int
|
||||
Errors int
|
||||
Min time.Duration
|
||||
P50 time.Duration
|
||||
P95 time.Duration
|
||||
Max time.Duration
|
||||
Elapsed time.Duration
|
||||
}
|
||||
|
||||
var indexes = []candidateIndex{
|
||||
{
|
||||
Name: "idx_dependencies_type_issue_target",
|
||||
Table: "dependencies",
|
||||
SQL: "CREATE INDEX idx_dependencies_type_issue_target ON dependencies (type, issue_id, depends_on_issue_id)",
|
||||
Drop: "DROP INDEX idx_dependencies_type_issue_target ON dependencies",
|
||||
},
|
||||
{
|
||||
Name: "idx_issues_ready_assignee",
|
||||
Table: "issues",
|
||||
SQL: "CREATE INDEX idx_issues_ready_assignee ON issues (assignee, status, priority, created_at, id)",
|
||||
Drop: "DROP INDEX idx_issues_ready_assignee ON issues",
|
||||
},
|
||||
{
|
||||
Name: "idx_issues_ready_status",
|
||||
Table: "issues",
|
||||
SQL: "CREATE INDEX idx_issues_ready_status ON issues (status, priority, created_at, id)",
|
||||
Drop: "DROP INDEX idx_issues_ready_status ON issues",
|
||||
},
|
||||
{
|
||||
Name: "idx_issues_defer_until",
|
||||
Table: "issues",
|
||||
SQL: "CREATE INDEX idx_issues_defer_until ON issues (defer_until)",
|
||||
Drop: "DROP INDEX idx_issues_defer_until ON issues",
|
||||
},
|
||||
}
|
||||
|
||||
var sqlOpen = sql.Open
|
||||
|
||||
const dependencyTargetExpr = "COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external)"
|
||||
|
||||
func main() {
|
||||
if err := run(context.Background()); err != nil {
|
||||
log.Print(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context) error {
|
||||
return runWithArgs(ctx, os.Args[1:])
|
||||
}
|
||||
|
||||
func runWithArgs(ctx context.Context, args []string) error {
|
||||
var dsn string
|
||||
var concurrency int
|
||||
var iterations int
|
||||
var keepIndexes bool
|
||||
fs := flag.NewFlagSet("bench-ready-indexes", flag.ContinueOnError)
|
||||
fs.StringVar(&dsn, "dsn", "root@tcp(127.0.0.1:33307)/mc?timeout=30s&readTimeout=30s&writeTimeout=30s", "MySQL DSN")
|
||||
fs.IntVar(&concurrency, "concurrency", 64, "concurrent query workers")
|
||||
fs.IntVar(&iterations, "iterations", 2, "query executions per worker per case")
|
||||
fs.BoolVar(&keepIndexes, "keep-indexes", false, "leave candidate indexes installed after the final benchmark state")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
db, err := sqlOpen("mysql", dsn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetMaxOpenConns(concurrency + 8)
|
||||
db.SetMaxIdleConns(concurrency + 8)
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := cleanupCandidateIndexes(context.Background(), db, keepIndexes); err != nil {
|
||||
log.Printf("cleanup candidate indexes: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
queries, err := buildQueries(ctx, db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
states := [][]candidateIndex{nil}
|
||||
for _, idx := range indexes {
|
||||
states = append(states, []candidateIndex{idx})
|
||||
}
|
||||
states = append(states, indexes)
|
||||
|
||||
for _, state := range states {
|
||||
if err := dropAll(ctx, db); err != nil {
|
||||
return err
|
||||
}
|
||||
stateName := "baseline"
|
||||
if len(state) > 0 {
|
||||
names := make([]string, 0, len(state))
|
||||
for _, idx := range state {
|
||||
start := time.Now()
|
||||
if _, err := db.ExecContext(ctx, idx.SQL); err != nil {
|
||||
return fmt.Errorf("create %s: %w", idx.Name, err)
|
||||
}
|
||||
fmt.Printf("created %s in %s\n", idx.Name, time.Since(start).Round(time.Millisecond))
|
||||
names = append(names, idx.Name)
|
||||
}
|
||||
stateName = strings.Join(names, "+")
|
||||
}
|
||||
|
||||
fmt.Printf("\n## %s\n", stateName)
|
||||
for _, qc := range queries {
|
||||
r := benchQuery(ctx, db, qc, concurrency, iterations)
|
||||
fmt.Printf("%-32s count=%4d errors=%2d min=%7s p50=%7s p95=%7s max=%7s wall=%7s\n",
|
||||
r.Name, r.Count, r.Errors, r.Min.Round(time.Millisecond), r.P50.Round(time.Millisecond),
|
||||
r.P95.Round(time.Millisecond), r.Max.Round(time.Millisecond), r.Elapsed.Round(time.Millisecond))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanupCandidateIndexes(ctx context.Context, db *sql.DB, keepIndexes bool) error {
|
||||
if keepIndexes {
|
||||
return nil
|
||||
}
|
||||
return dropAll(ctx, db)
|
||||
}
|
||||
|
||||
func dropAll(ctx context.Context, db *sql.DB) error {
|
||||
for _, idx := range indexes {
|
||||
if _, err := db.ExecContext(ctx, idx.Drop); err != nil && !strings.Contains(err.Error(), "not found") && !strings.Contains(err.Error(), "does not exist") && !strings.Contains(err.Error(), "can't drop") {
|
||||
return fmt.Errorf("drop %s: %w", idx.Name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildQueries(ctx context.Context, db *sql.DB) ([]queryCase, error) {
|
||||
ids, err := candidateIDs(ctx, db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
placeholders := strings.TrimRight(strings.Repeat("?,", len(ids)), ",")
|
||||
args := make([]any, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
args = append(args, id)
|
||||
}
|
||||
return []queryCase{
|
||||
{
|
||||
Name: "ready_assignee_page",
|
||||
Query: `SELECT id FROM issues
|
||||
WHERE status IN ('open','in_progress')
|
||||
AND (pinned = 0 OR pinned IS NULL)
|
||||
AND (ephemeral = 0 OR ephemeral IS NULL)
|
||||
AND id IN (SELECT id FROM issues WHERE issue_type NOT IN ('merge-request','gate','molecule','message','agent','role','rig'))
|
||||
AND assignee = 'gascity--control-dispatcher'
|
||||
AND (defer_until IS NULL OR defer_until <= UTC_TIMESTAMP())
|
||||
ORDER BY priority ASC, created_at DESC, id ASC
|
||||
LIMIT 100`,
|
||||
},
|
||||
{
|
||||
Name: "ready_routed_page",
|
||||
Query: `SELECT id FROM issues
|
||||
WHERE status IN ('open','in_progress')
|
||||
AND (pinned = 0 OR pinned IS NULL)
|
||||
AND (ephemeral = 0 OR ephemeral IS NULL)
|
||||
AND id IN (SELECT id FROM issues WHERE issue_type NOT IN ('merge-request','gate','molecule','message','agent','role','rig'))
|
||||
AND (assignee IS NULL OR assignee = '')
|
||||
AND (defer_until IS NULL OR defer_until <= UTC_TIMESTAMP())
|
||||
AND JSON_UNQUOTE(JSON_EXTRACT(metadata, '$."gc.routed_to"')) = 'gascity/control-dispatcher'
|
||||
ORDER BY priority ASC, created_at DESC, id ASC
|
||||
LIMIT 100`,
|
||||
},
|
||||
{
|
||||
Name: "deferred_parents",
|
||||
Query: `SELECT id FROM issues
|
||||
WHERE defer_until IS NOT NULL AND defer_until > UTC_TIMESTAMP()`,
|
||||
},
|
||||
{
|
||||
Name: "candidate_blocking_deps",
|
||||
Query: fmt.Sprintf(`SELECT issue_id, %s AS target_id, type, metadata FROM dependencies WHERE issue_id IN (%s) AND type IN ('blocks','waits-for','conditional-blocks')`, dependencyTargetExpr, placeholders),
|
||||
Args: args,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func candidateIDs(ctx context.Context, db *sql.DB) ([]string, error) {
|
||||
rows, err := db.QueryContext(ctx, `SELECT id FROM issues WHERE status IN ('open','in_progress') ORDER BY priority ASC, created_at DESC, id ASC LIMIT 100`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var ids []string
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return nil, fmt.Errorf("no candidate IDs")
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func benchQuery(ctx context.Context, db *sql.DB, qc queryCase, concurrency, iterations int) result {
|
||||
start := time.Now()
|
||||
var mu sync.Mutex
|
||||
var latencies []time.Duration
|
||||
errors := 0
|
||||
var wg sync.WaitGroup
|
||||
for worker := 0; worker < concurrency; worker++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < iterations; i++ {
|
||||
qStart := time.Now()
|
||||
rows, err := db.QueryContext(ctx, qc.Query, qc.Args...)
|
||||
if err == nil {
|
||||
for rows.Next() {
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
err = rows.Err()
|
||||
}
|
||||
_ = rows.Close()
|
||||
}
|
||||
latency := time.Since(qStart)
|
||||
mu.Lock()
|
||||
if err != nil {
|
||||
errors++
|
||||
} else {
|
||||
latencies = append(latencies, latency)
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
sort.Slice(latencies, func(i, j int) bool { return latencies[i] < latencies[j] })
|
||||
return result{
|
||||
Name: qc.Name,
|
||||
Count: len(latencies),
|
||||
Errors: errors,
|
||||
Min: percentile(latencies, 0),
|
||||
P50: percentile(latencies, 50),
|
||||
P95: percentile(latencies, 95),
|
||||
Max: percentile(latencies, 100),
|
||||
Elapsed: time.Since(start),
|
||||
}
|
||||
}
|
||||
|
||||
func percentile(values []time.Duration, pct int) time.Duration {
|
||||
if len(values) == 0 {
|
||||
return 0
|
||||
}
|
||||
if pct <= 0 {
|
||||
return values[0]
|
||||
}
|
||||
if pct >= 100 {
|
||||
return values[len(values)-1]
|
||||
}
|
||||
idx := (len(values)*pct + 99) / 100
|
||||
if idx < 1 {
|
||||
idx = 1
|
||||
}
|
||||
return values[idx-1]
|
||||
}
|
||||
143
scripts/bench-ready-indexes/main_test.go
Normal file
143
scripts/bench-ready-indexes/main_test.go
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
)
|
||||
|
||||
func TestCleanupCandidateIndexesDropsByDefault(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
for _, idx := range indexes {
|
||||
mock.ExpectExec(regexp.QuoteMeta(idx.Drop)).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
}
|
||||
|
||||
if err := cleanupCandidateIndexes(context.Background(), db, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupCandidateIndexesHonorsKeepIndexes(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if err := cleanupCandidateIndexes(context.Background(), db, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCleansUpCandidateIndexesAfterCreateFailure(t *testing.T) {
|
||||
db, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
oldSQLOpen := sqlOpen
|
||||
sqlOpen = func(driverName, dataSourceName string) (*sql.DB, error) {
|
||||
if driverName != "mysql" {
|
||||
t.Fatalf("driverName = %q, want mysql", driverName)
|
||||
}
|
||||
if dataSourceName != "sqlmock-dsn" {
|
||||
t.Fatalf("dataSourceName = %q, want sqlmock-dsn", dataSourceName)
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
sqlOpen = oldSQLOpen
|
||||
})
|
||||
|
||||
mock.ExpectPing()
|
||||
mock.ExpectQuery(regexp.QuoteMeta(`SELECT id FROM issues WHERE status IN ('open','in_progress') ORDER BY priority ASC, created_at DESC, id ASC LIMIT 100`)).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("bd-a"))
|
||||
|
||||
expectDropAll(mock) // baseline
|
||||
for _, idx := range indexes {
|
||||
expectDropAll(mock)
|
||||
mock.ExpectExec(regexp.QuoteMeta(idx.SQL)).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
}
|
||||
expectDropAll(mock) // all-indexes state starts from a clean slate
|
||||
mock.ExpectExec(regexp.QuoteMeta(indexes[0].SQL)).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec(regexp.QuoteMeta(indexes[1].SQL)).
|
||||
WillReturnError(errors.New("create failed"))
|
||||
expectDropAll(mock) // deferred cleanup still runs after the create error
|
||||
mock.ExpectClose()
|
||||
|
||||
err = runWithArgs(context.Background(), []string{
|
||||
"--dsn", "sqlmock-dsn",
|
||||
"--concurrency", "0",
|
||||
"--iterations", "1",
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected create-index error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "create "+indexes[1].Name) {
|
||||
t.Fatalf("error = %v, want create failure for %s", err, indexes[1].Name)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildQueriesUsesTypedDependencyTargetProjection(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
mock.ExpectQuery(regexp.QuoteMeta(`SELECT id FROM issues WHERE status IN ('open','in_progress') ORDER BY priority ASC, created_at DESC, id ASC LIMIT 100`)).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("bd-a"))
|
||||
|
||||
queries, err := buildQueries(context.Background(), db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var found bool
|
||||
for _, qc := range queries {
|
||||
if qc.Name != "candidate_blocking_deps" {
|
||||
continue
|
||||
}
|
||||
found = true
|
||||
if !strings.Contains(qc.Query, dependencyTargetExpr) {
|
||||
t.Fatalf("candidate query = %q, want typed target projection", qc.Query)
|
||||
}
|
||||
if strings.Contains(qc.Query, "depends_on"+"_id") {
|
||||
t.Fatalf("candidate query still references generated column: %q", qc.Query)
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("candidate_blocking_deps query not found")
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func expectDropAll(mock sqlmock.Sqlmock) {
|
||||
for _, idx := range indexes {
|
||||
mock.ExpectExec(regexp.QuoteMeta(idx.Drop)).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
}
|
||||
}
|
||||
1149
scripts/repro-dolt-prod-timeouts/main.go
Normal file
1149
scripts/repro-dolt-prod-timeouts/main.go
Normal file
File diff suppressed because it is too large
Load diff
437
scripts/repro-dolt-prod-timeouts/main_test.go
Normal file
437
scripts/repro-dolt-prod-timeouts/main_test.go
Normal file
|
|
@ -0,0 +1,437 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/steveyegge/beads/internal/storage/doltutil"
|
||||
"github.com/steveyegge/beads/internal/storage/schema"
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
)
|
||||
|
||||
func TestIsDriverReadTimeout(t *testing.T) {
|
||||
result := opResult{
|
||||
StderrTail: "[mysql] 2026/05/13 18:13:48 packets.go:58 read tcp 127.0.0.1:39308->127.0.0.1:21791: i/o timeout",
|
||||
}
|
||||
if !isDriverReadTimeout(result) {
|
||||
t.Fatal("expected MySQL driver read timeout to be classified")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDriverReadTimeoutIgnoresHarnessTimeout(t *testing.T) {
|
||||
result := opResult{
|
||||
TimedOut: true,
|
||||
StderrTail: "signal: killed",
|
||||
}
|
||||
if isDriverReadTimeout(result) {
|
||||
t.Fatal("harness timeout should not be classified as driver read timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMixedBackgroundJobsIncludesSessionLoadShapes(t *testing.T) {
|
||||
jobs := mixedBackgroundJobs(12)
|
||||
seen := map[string]bool{}
|
||||
for _, job := range jobs {
|
||||
seen[job.Kind] = true
|
||||
}
|
||||
for _, want := range []string{"session-ready", "control-ready", "route-ready", "show", "list", "claim"} {
|
||||
if !seen[want] {
|
||||
t.Fatalf("mixed background jobs missing %q; seen=%v", want, seen)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDepFixtureIssueCountIncludesChainTargets(t *testing.T) {
|
||||
if got := depFixtureIssueCount(10, 0); got != 20 {
|
||||
t.Fatalf("without chains got %d, want 20", got)
|
||||
}
|
||||
if got := depFixtureIssueCount(10, 100); got != 1132 {
|
||||
t.Fatalf("with chains got %d, want 1132", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveExecutablePathReturnsAbsolutePath(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
bd := filepath.Join(tmp, "bd")
|
||||
if err := os.WriteFile(bd, []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Chdir(tmp)
|
||||
|
||||
got, err := resolveExecutablePath("./bd")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != bd {
|
||||
t.Fatalf("got %q, want %q", got, bd)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsDefaultsSyntheticWorkspaceToFullSeed(t *testing.T) {
|
||||
cfg, err := parseFlags(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.SeedMode != "full" {
|
||||
t.Fatalf("SeedMode = %q, want full", cfg.SeedMode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsDefaultsExistingWorkspaceToNoSeed(t *testing.T) {
|
||||
cfg, err := parseFlags([]string{"--workspace", "/tmp/existing-beads-workspace"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.SeedMode != "none" {
|
||||
t.Fatalf("SeedMode = %q, want none", cfg.SeedMode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsPreservesExplicitExistingWorkspaceSeedMode(t *testing.T) {
|
||||
cfg, err := parseFlags([]string{"--workspace", "/tmp/existing-beads-workspace", "--seed-mode", "full"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.SeedMode != "full" {
|
||||
t.Fatalf("SeedMode = %q, want full", cfg.SeedMode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkSubprocessesStripDoltEnvOverrides(t *testing.T) {
|
||||
for _, key := range subprocessEnvDenylist {
|
||||
t.Setenv(key, "caller-"+strings.ToLower(key))
|
||||
}
|
||||
|
||||
tmp := t.TempDir()
|
||||
bd := filepath.Join(tmp, "bd")
|
||||
if err := os.WriteFile(bd, []byte("#!/bin/sh\n"+noDoltOverrideEnvScript()), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := config{BDPath: bd, Timeout: time.Second}
|
||||
ws := &workspace{Dir: tmp}
|
||||
if got := runBD(context.Background(), cfg, ws, job{Kind: "env", Argv: []string{"status"}}); got.Err != "" {
|
||||
t.Fatalf("runBD inherited denied env: err=%q stderr=%q", got.Err, got.StderrTail)
|
||||
}
|
||||
if got := runShell(context.Background(), cfg, ws, job{Kind: "env", Sh: noDoltOverrideEnvScript()}); got.Err != "" {
|
||||
t.Fatalf("runShell inherited denied env: err=%q stderr=%q", got.Err, got.StderrTail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestControlQueryScriptPreservesReadyProbeFailure(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
bd := filepath.Join(tmp, "bd")
|
||||
if err := os.WriteFile(bd, []byte("#!/bin/sh\necho ready failed >&2\nexit 17\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := config{BDPath: bd, Timeout: time.Second}
|
||||
ws := &workspace{Dir: tmp}
|
||||
result := runShell(context.Background(), cfg, ws, controlQueryJobs(1)[0])
|
||||
if result.Err == "" {
|
||||
t.Fatalf("expected control query shell to fail when bd ready fails; stderr=%q", result.StderrTail)
|
||||
}
|
||||
if !strings.Contains(result.StderrTail, "ready failed") {
|
||||
t.Fatalf("stderr tail = %q, want ready probe stderr", result.StderrTail)
|
||||
}
|
||||
}
|
||||
|
||||
func noDoltOverrideEnvScript() string {
|
||||
var b strings.Builder
|
||||
b.WriteString("for key in")
|
||||
for _, key := range subprocessEnvDenylist {
|
||||
b.WriteByte(' ')
|
||||
b.WriteString(key)
|
||||
}
|
||||
b.WriteString(`; do
|
||||
eval "value=\${$key:-}"
|
||||
if [ -n "$value" ]; then
|
||||
echo "$key inherited: $value" >&2
|
||||
exit 42
|
||||
fi
|
||||
done
|
||||
exit 0
|
||||
`)
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func TestScenarioNamesAllIncludesEveryAdvertisedScenario(t *testing.T) {
|
||||
got, err := scenarioNames("all")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{
|
||||
"ready",
|
||||
"dep",
|
||||
"control",
|
||||
"mixed",
|
||||
"outage",
|
||||
"cycle-current",
|
||||
"cycle-deps-only",
|
||||
"cycle-wisps-only",
|
||||
"cycle-bfs",
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("scenarioNames(all) got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertDependenciesWritesTypedIssueTargetColumn(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
mock.ExpectExec(`INSERT INTO dependencies\s+\(issue_id, depends_on_issue_id, type, created_by, metadata\)\s+VALUES`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
if err := insertDependencies(context.Background(), db, 1, 100000); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertDependenciesUsesConfiguredIssueRange(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
mock.ExpectExec(`INSERT INTO dependencies\s+\(issue_id, depends_on_issue_id, type, created_by, metadata\)\s+VALUES`).
|
||||
WithArgs(
|
||||
"perf-000000", "perf-000004", "blocks", "bench", "{}",
|
||||
"perf-000001", "perf-000000", "blocks", "bench", "{}",
|
||||
"perf-000002", "perf-000001", "blocks", "bench", "{}",
|
||||
"perf-000003", "perf-000002", "blocks", "bench", "{}",
|
||||
"perf-000004", "perf-000003", "blocks", "bench", "{}",
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 5))
|
||||
|
||||
if err := insertDependencies(context.Background(), db, 5, 5); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertDependenciesRejectsImpossibleUniquePairCount(t *testing.T) {
|
||||
db, _, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
err = insertDependencies(context.Background(), db, 3, 2)
|
||||
if err == nil {
|
||||
t.Fatal("expected impossible pair count error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "exceeds unique dependency pairs") {
|
||||
t.Fatalf("error = %v, want unique dependency pair failure", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertDepAddChainsWritesTypedIssueTargetColumn(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
mock.ExpectExec(`INSERT INTO dependencies\s+\(issue_id, depends_on_issue_id, type, created_by, metadata\)\s+VALUES`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
if err := insertDepAddChains(context.Background(), db, 1, 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCycleCheckCurrentSQLUsesTypedDependencyTargetProjection(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
mock.ExpectQuery(regexp.QuoteMeta(dependencyTargetExpr)).
|
||||
WithArgs("perf-000002", "perf-000001").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow(0))
|
||||
|
||||
if err := cycleCheckCurrentSQL(context.Background(), db, "perf-000001", "perf-000002", 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchBlockingTargetsUsesTypedDependencyTargetProjection(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
mock.ExpectQuery(regexp.QuoteMeta(dependencyTargetExpr)).
|
||||
WithArgs("perf-000001", "perf-000001").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"target_id"}).AddRow("perf-000002"))
|
||||
|
||||
got, err := fetchBlockingTargets(context.Background(), db, []string{"perf-000001"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, []string{"perf-000002"}) {
|
||||
t.Fatalf("targets = %v, want [perf-000002]", got)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedProductionShapeFullCoversTypedDependencyInserts(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
mock.ExpectExec(`INSERT INTO issues\s+\(id, title, description, design, acceptance_criteria, notes,\s+status, priority, issue_type, assignee, metadata\)\s+VALUES`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 8))
|
||||
mock.ExpectExec(`INSERT INTO dependencies\s+\(issue_id, depends_on_issue_id, type, created_by, metadata\)\s+VALUES`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectExec(`INSERT INTO dependencies\s+\(issue_id, depends_on_issue_id, type, created_by, metadata\)\s+VALUES`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_ADD('-A')")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', 'seed production timeout fixture')")).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
|
||||
cfg := config{SeedMode: "full", IssueCount: 1, DepCount: 1, Ops: 1, ChainDepth: 1}
|
||||
if err := seedProductionShape(context.Background(), db, cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedProductionShapeFullSmallIssueCountRealSchema(t *testing.T) {
|
||||
if _, err := exec.LookPath("dolt"); err != nil {
|
||||
t.Skip("dolt not installed, skipping real-schema smoke")
|
||||
}
|
||||
skipOldDoltForCurrentSchema(t)
|
||||
|
||||
ctx := context.Background()
|
||||
baseDir := t.TempDir()
|
||||
dbName := "testdb"
|
||||
dbDir := filepath.Join(baseDir, dbName)
|
||||
if err := os.MkdirAll(dbDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runTestCmd(t, dbDir, "dolt", "init", "--name", "test", "--email", "test@example.com")
|
||||
|
||||
port, err := testutil.FindFreePort()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
serverCmd := exec.Command("dolt", "sql-server",
|
||||
"-H", "127.0.0.1",
|
||||
"-P", fmt.Sprintf("%d", port),
|
||||
)
|
||||
serverCmd.Dir = baseDir
|
||||
if err := serverCmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = serverCmd.Process.Kill()
|
||||
_ = serverCmd.Wait()
|
||||
})
|
||||
if !testutil.WaitForServer(port, 15*time.Second) {
|
||||
t.Fatal("dolt sql-server did not become ready")
|
||||
}
|
||||
|
||||
dsn := doltutil.ServerDSN{Host: "127.0.0.1", Port: port, User: "root", Database: dbName, Timeout: 10 * time.Second}.String()
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
defer db.Close()
|
||||
|
||||
if _, err := schema.MigrateUp(ctx, db); err != nil {
|
||||
t.Fatalf("migrate schema: %v", err)
|
||||
}
|
||||
|
||||
cfg := config{SeedMode: "full", IssueCount: 50, DepCount: 50, Ops: 5, ChainDepth: 2}
|
||||
if err := seedProductionShape(ctx, db, cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var depRows int
|
||||
if err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM dependencies").Scan(&depRows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantDeps := cfg.DepCount + cfg.Ops*cfg.ChainDepth
|
||||
if depRows != wantDeps {
|
||||
t.Fatalf("dependency rows = %d, want %d", depRows, wantDeps)
|
||||
}
|
||||
|
||||
sourceID, targetID := dependencyEndpoints(0, cfg.IssueCount, 0, 200)
|
||||
if err := cycleCheckCurrentSQL(ctx, db, perfIssueID(1), sourceID, 0); err != nil {
|
||||
t.Fatalf("cycleCheckCurrentSQL: %v", err)
|
||||
}
|
||||
targets, err := fetchBlockingTargets(ctx, db, []string{sourceID})
|
||||
if err != nil {
|
||||
t.Fatalf("fetchBlockingTargets: %v", err)
|
||||
}
|
||||
if !slices.Contains(targets, targetID) {
|
||||
t.Fatalf("fetchBlockingTargets(%q) = %v, want %q", sourceID, targets, targetID)
|
||||
}
|
||||
}
|
||||
|
||||
func skipOldDoltForCurrentSchema(t *testing.T) {
|
||||
t.Helper()
|
||||
output, err := exec.Command("dolt", "version").CombinedOutput()
|
||||
if err != nil {
|
||||
t.Skipf("dolt version unavailable, skipping real-schema smoke: %v", err)
|
||||
}
|
||||
if regexp.MustCompile(`\bdolt version 1\.`).Match(output) {
|
||||
t.Skipf("dolt 1.x cannot initialize the current migration set: %s", strings.TrimSpace(string(output)))
|
||||
}
|
||||
}
|
||||
|
||||
func runTestCmd(t *testing.T, dir string, name string, args ...string) {
|
||||
t.Helper()
|
||||
cmd := exec.Command(name, args...)
|
||||
cmd.Dir = dir
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("%s %v failed in %s: %v\nOutput: %s", name, args, dir, err, output)
|
||||
}
|
||||
}
|
||||
|
||||
func runTestDoltSQL(t *testing.T, dir, query string) {
|
||||
t.Helper()
|
||||
cmd := exec.Command("dolt", "sql", "-q", query)
|
||||
cmd.Dir = dir
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("dolt sql failed in %s: %v\nQuery: %.200s...\nOutput: %s", dir, err, query, output)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue