Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061package db
import ( "context" "fmt" "path/filepath" "slices" "testing" "time")
func historyOf(t *testing.T, pipelines int) *DB { t.Helper() database, err := Make(context.Background(), filepath.Join(t.TempDir(), "spindle.db")) if err != nil { t.Fatal(err) } t.Cleanup(func() { database.Close() }) tx, err := database.Begin() if err != nil { t.Fatal(err) } stmt, err := tx.Prepare(`insert into pipelines (pipeline_id, repo_did, commit_sha, kind, payload) values (?, 'did:plc:history', ?, 'push', '{}')`) if err != nil { t.Fatal(err) } for i := range pipelines { if _, err := stmt.Exec(fmt.Sprintf("p%d", i), fmt.Sprintf("%040x", i)); err != nil { t.Fatal(err) } } if err := tx.Commit(); err != nil { t.Fatal(err) } return database}
func medianRareKindQuery(t *testing.T, d *DB) time.Duration { t.Helper() var took []time.Duration for range 9 { start := time.Now() records, _, total, err := d.QueryPipelines(context.Background(), "did:plc:history", nil, "", []string{"manual"}, 30) took = append(took, time.Since(start)) if err != nil || len(records) != 0 || total != 0 { t.Fatalf("query = %d records, total %d, %v, want none", len(records), total, err) } } slices.Sort(took) return took[len(took)/2]}
func TestKindFilterCostDoesNotFollowTheHistory(t *testing.T) { shallow := medianRareKindQuery(t, historyOf(t, 500)) deep := medianRareKindQuery(t, historyOf(t, 100_000)) t.Logf("median rare kind query %v over 500 pipelines, %v over 100000", shallow, deep) if deep > 8*shallow+3*time.Millisecond { t.Fatalf("a kind filter matching nothing takes %v over 100000 pipelines against %v over 500, want it independent of history", deep, shallow) }}