This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
|
||||
"highload/db/migrations"
|
||||
"highload/internal/database/sqlc"
|
||||
"highload/internal/model"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -50,13 +51,14 @@ func (r *recorder) TraceQueryStart(ctx context.Context, _ *pgx.Conn, data pgx.Tr
|
||||
func (*recorder) TraceQueryEnd(context.Context, *pgx.Conn, pgx.TraceQueryEndData) {}
|
||||
|
||||
type fixture struct {
|
||||
admin *pgx.Conn
|
||||
pool *pgxpool.Pool
|
||||
schema string
|
||||
owns bool
|
||||
user string
|
||||
chats []string
|
||||
record recorder
|
||||
admin *pgx.Conn
|
||||
pool *pgxpool.Pool
|
||||
schema string
|
||||
owns bool
|
||||
user string
|
||||
chats []string
|
||||
record recorder
|
||||
researchBudget time.Duration
|
||||
}
|
||||
|
||||
func (f *fixture) cleanup() error {
|
||||
@@ -160,18 +162,28 @@ ORDER BY created_at,id OFFSET (SELECT count(*)*8/10 FROM messages WHERE chat_id=
|
||||
}
|
||||
|
||||
func (f *fixture) read(ctx context.Context, after *repository2.MessagePosition) (time.Duration, error) {
|
||||
return f.readChat(ctx, f.chats[0], after)
|
||||
}
|
||||
|
||||
func (f *fixture) readChat(ctx context.Context, chatID string, after *repository2.MessagePosition) (time.Duration, error) {
|
||||
repo := postgres.NewMessageRepository(sqlc.New(f.pool))
|
||||
f.record.mu.Lock()
|
||||
f.record.active = true
|
||||
f.record.mu.Unlock()
|
||||
started := time.Now()
|
||||
items, err := repo.GetMessagesByChatID(ctx, f.chats[0], 51, after)
|
||||
var items []*model.Message
|
||||
var err error
|
||||
if f.researchBudget > 0 {
|
||||
items, err = f.researchRead(ctx, chatID, after)
|
||||
} else {
|
||||
items, err = repo.GetMessagesByChatID(ctx, chatID, 51, after)
|
||||
}
|
||||
elapsed := time.Since(started)
|
||||
f.record.mu.Lock()
|
||||
f.record.active = false
|
||||
f.record.mu.Unlock()
|
||||
if err == nil && len(items) != 51 {
|
||||
err = errors.New("fixture history returned an unexpected page length")
|
||||
err = fmt.Errorf("fixture history returned %d rows instead of 51", len(items))
|
||||
}
|
||||
for i := 1; err == nil && i < len(items); i++ {
|
||||
if items[i].CreatedAt.Before(items[i-1].CreatedAt) {
|
||||
@@ -194,7 +206,11 @@ func maxVisited(node map[string]any) float64 {
|
||||
}
|
||||
|
||||
func (f *fixture) plan(ctx context.Context, after *repository2.MessagePosition) (map[string]any, error) {
|
||||
if _, err := f.read(ctx, after); err != nil {
|
||||
return f.planChat(ctx, f.chats[0], after)
|
||||
}
|
||||
|
||||
func (f *fixture) planChat(ctx context.Context, chatID string, after *repository2.MessagePosition) (map[string]any, error) {
|
||||
if _, err := f.readChat(ctx, chatID, after); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.record.mu.Lock()
|
||||
@@ -450,20 +466,29 @@ func main() {
|
||||
rows := flag.Int("rows", 10000, "fixture message count")
|
||||
budget := flag.Int("scan-budget", 1000, "engineering threshold, not a teacher-mandated constant")
|
||||
repetitions := flag.Int("repetitions", 5, "measurement samples per variant")
|
||||
series := flag.Int("series", 4, "independent AB/BA research rounds (even, >= 4)")
|
||||
writeBatches := flag.Int("write-batches", 20, "committed batches of 200 rows per research trial")
|
||||
timeout := flag.Duration("timeout", 3*time.Minute, "total SQL probe execution budget")
|
||||
flag.Parse()
|
||||
url := os.Getenv("LAB2_SQL_DATABASE_URL")
|
||||
if url == "" {
|
||||
fmt.Fprintln(os.Stderr, "set LAB2_SQL_DATABASE_URL for a dedicated test database")
|
||||
os.Exit(2)
|
||||
}
|
||||
if *mode != "snapshot" && *mode != "plan" && *mode != "compare" && *mode != "seed" {
|
||||
if *mode != "snapshot" && *mode != "plan" && *mode != "compare" && *mode != "seed" && *mode != "research" {
|
||||
os.Exit(2)
|
||||
}
|
||||
signals, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
ctx, cancel := context.WithTimeout(signals, 3*time.Minute)
|
||||
ctx, cancel := context.WithTimeout(signals, *timeout)
|
||||
defer cancel()
|
||||
result, err := execute(ctx, *mode, url, *schema, *rows, *budget, *repetitions)
|
||||
var result map[string]any
|
||||
var err error
|
||||
if *mode == "research" {
|
||||
result, err = research(ctx, url, *rows, *repetitions, *series, *writeBatches)
|
||||
} else {
|
||||
result, err = execute(ctx, *mode, url, *schema, *rows, *budget, *repetitions)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "SQL probe failed; private error details suppressed")
|
||||
os.Exit(1)
|
||||
|
||||
@@ -0,0 +1,255 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"highload/internal/database/sqlc"
|
||||
"highload/internal/model"
|
||||
repository2 "highload/internal/repository"
|
||||
)
|
||||
|
||||
// Research reuses the actual repository SQL. No application index is changed:
|
||||
// the fixture owns a fresh schema and removes it when the experiment finishes.
|
||||
func research(ctx context.Context, url string, rows, repetitions, series, batches int) (result map[string]any, err error) {
|
||||
stage := "validate"
|
||||
defer func() {
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "research: failed at "+stage)
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
fmt.Fprintln(os.Stderr, "sqlstate: "+pgErr.Code)
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
fmt.Fprintln(os.Stderr, "research: deadline exceeded")
|
||||
}
|
||||
if errors.Is(err, repository2.ErrServiceUnavailable) {
|
||||
fmt.Fprintln(os.Stderr, "research: repository unavailable")
|
||||
}
|
||||
for _, text := range []string{"timeout", "context deadline exceeded", "invalid input", "unexpected page", "history out of order"} {
|
||||
if strings.Contains(err.Error(), text) {
|
||||
fmt.Fprintln(os.Stderr, "research: "+text)
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(err.Error(), "fixture history returned ") {
|
||||
fmt.Fprintln(os.Stderr, "research: "+err.Error())
|
||||
}
|
||||
}
|
||||
}()
|
||||
if rows < 100000 || rows%100 != 0 || repetitions < 30 || series < 4 || series%2 != 0 || batches < 10 {
|
||||
return nil, errors.New("research needs >=100k rows, >=30 reads, even >=4 series, >=10 write batches")
|
||||
}
|
||||
schema := "lab2_test_" + uuid.NewString()[:8] + "_research"
|
||||
stage = "create-fixture"
|
||||
f, err := createFixture(ctx, url, schema, rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Preserve the API's one-second policy. Only the offline SQL experiment uses
|
||||
// a larger, identical budget for both indexes, so slow baselines are measurable.
|
||||
f.researchBudget = 10 * time.Second
|
||||
defer func() {
|
||||
if cleanupErr := f.cleanup(); err == nil {
|
||||
err = cleanupErr
|
||||
}
|
||||
}()
|
||||
stage = "drop-original-index"
|
||||
if _, err = f.pool.Exec(ctx, "DROP INDEX idx_messages_chat_created_id"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Disable autovacuum only for the disposable table to control trial cleanup.
|
||||
if _, err = f.pool.Exec(ctx, "ALTER TABLE messages SET (autovacuum_enabled=false)"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Fixed message body size; index keys and logical rows remain identical in A/B.
|
||||
stage = "payload"
|
||||
if _, err = f.pool.Exec(ctx, "UPDATE messages SET text=rpad(text,256,'x')"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var version string
|
||||
if err = f.pool.QueryRow(ctx, "SHOW server_version").Scan(&version); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
type readCase struct {
|
||||
name, chat string
|
||||
after *repository2.MessagePosition
|
||||
}
|
||||
var cases []readCase
|
||||
for _, item := range []struct {
|
||||
name string
|
||||
index int
|
||||
}{{"large", 0}, {"medium", 2}, {"small", 4}} {
|
||||
stage = "cursor-" + item.name
|
||||
chatID := f.chats[item.index]
|
||||
position := &repository2.MessagePosition{}
|
||||
if err = f.pool.QueryRow(ctx, `SELECT id::text,created_at FROM messages WHERE chat_id=$1
|
||||
ORDER BY created_at,id OFFSET (SELECT count(*)*8/10 FROM messages WHERE chat_id=$1) LIMIT 1`, chatID).Scan(&position.ID, &position.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cases = append(cases, readCase{item.name + "_first", chatID, nil}, readCase{item.name + "_deep", chatID, position})
|
||||
}
|
||||
stage = "initial-digest"
|
||||
before, err := f.messageDigest(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var trials []map[string]any
|
||||
for round := 0; round < series; round++ {
|
||||
order := []string{"A", "B"}
|
||||
if round%2 == 1 {
|
||||
order = []string{"B", "A"}
|
||||
}
|
||||
for ordinal, variant := range order {
|
||||
stage = "vacuum-" + variant
|
||||
// Remove dead tuples and reset physical bloat between trials, outside timing.
|
||||
if _, err = f.pool.Exec(ctx, "VACUUM (FULL, ANALYZE) messages"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
columns := "chat_id"
|
||||
if variant == "B" {
|
||||
columns = "chat_id,created_at,id"
|
||||
}
|
||||
buildStart := time.Now()
|
||||
stage = "build-index-" + variant
|
||||
if _, err = f.pool.Exec(ctx, "CREATE INDEX lab2_history_variant ON messages("+columns+")"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buildMS := float64(time.Since(buildStart).Microseconds()) / 1000
|
||||
if _, err = f.pool.Exec(ctx, "ANALYZE messages"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var indexBytes, tableBytes int64
|
||||
if err = f.pool.QueryRow(ctx, "SELECT pg_relation_size('lab2_history_variant'),pg_relation_size('messages')").Scan(&indexBytes, &tableBytes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
readResults := map[string]any{}
|
||||
for _, c := range cases {
|
||||
stage = "warmup-" + variant + "-" + c.name
|
||||
for warmup := 0; warmup < 5; warmup++ {
|
||||
if _, err = f.readChat(ctx, c.chat, c.after); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
var timings []time.Duration
|
||||
var raw []float64
|
||||
started := time.Now()
|
||||
stage = "read-" + variant + "-" + c.name
|
||||
for n := 0; n < repetitions; n++ {
|
||||
d, readErr := f.readChat(ctx, c.chat, c.after)
|
||||
if readErr != nil {
|
||||
return nil, readErr
|
||||
}
|
||||
timings = append(timings, d)
|
||||
raw = append(raw, float64(d.Microseconds())/1000)
|
||||
}
|
||||
elapsed := time.Since(started).Seconds()
|
||||
stage = "plan-" + variant + "-" + c.name
|
||||
plan, planErr := f.planChat(ctx, c.chat, c.after)
|
||||
if planErr != nil {
|
||||
return nil, planErr
|
||||
}
|
||||
readResults[c.name] = map[string]any{"samples_ms": raw, "latency_ms": latency(timings), "successful_rps": float64(repetitions) / elapsed, "elapsed_seconds": elapsed, "plan": plan}
|
||||
}
|
||||
var writes []time.Duration
|
||||
var writeMS []float64
|
||||
var walBytes []int64
|
||||
for n := 0; n < batches; n++ {
|
||||
stage = "write-" + variant
|
||||
var lsn string
|
||||
if err = f.pool.QueryRow(ctx, "SELECT pg_current_wal_insert_lsn()::text").Scan(&lsn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
started := time.Now()
|
||||
tx, txErr := f.pool.Begin(ctx)
|
||||
if txErr != nil {
|
||||
return nil, txErr
|
||||
}
|
||||
_, txErr = tx.Exec(ctx, `INSERT INTO messages(chat_id,sender_id,text)
|
||||
SELECT $1::uuid,$2::uuid,rpad('research-write-'||n,256,'x') FROM generate_series(1,200) n`, f.chats[0], f.user)
|
||||
if txErr == nil {
|
||||
txErr = tx.Commit(ctx)
|
||||
} else {
|
||||
_ = tx.Rollback(context.Background())
|
||||
}
|
||||
if txErr != nil {
|
||||
return nil, txErr
|
||||
}
|
||||
d := time.Since(started)
|
||||
writes = append(writes, d)
|
||||
writeMS = append(writeMS, float64(d.Microseconds())/1000)
|
||||
var delta int64
|
||||
if err = f.pool.QueryRow(ctx, "SELECT pg_wal_lsn_diff(pg_current_wal_insert_lsn(),$1::pg_lsn)::bigint", lsn).Scan(&delta); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
walBytes = append(walBytes, delta)
|
||||
}
|
||||
stage = "delete-fixture-writes-" + variant
|
||||
if _, err = f.pool.Exec(ctx, "DELETE FROM messages WHERE text LIKE 'research-write-%'"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stage = "verify-dataset-" + variant
|
||||
after, digestErr := f.messageDigest(ctx)
|
||||
if digestErr != nil || after != before {
|
||||
return nil, errors.New("logical dataset changed between trials")
|
||||
}
|
||||
trials = append(trials, map[string]any{"series": round + 1, "order": ordinal + 1, "variant": variant, "columns": columns, "read_cases": readResults, "index_bytes": indexBytes, "table_bytes": tableBytes, "build_ms": buildMS, "insert_200_with_commit_ms": latency(writes), "write_samples_ms": writeMS, "write_wal_bytes": walBytes, "dataset_sha256": before})
|
||||
fmt.Fprintf(os.Stderr, "series %d variant %s complete\n", round+1, variant)
|
||||
stage = "drop-index-" + variant
|
||||
if _, err = f.pool.Exec(ctx, "DROP INDEX lab2_history_variant"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return map[string]any{"mode": "research", "rows": rows, "chat_counts": []int{rows * 70 / 100, rows * 20 / 100, rows * 6 / 100, rows * 3 / 100, rows / 100}, "payload_bytes": 256, "postgres_version": version, "series": series, "reads_per_case": repetitions, "write_batches_per_trial": batches, "rows_per_write_batch": 200, "warmup_per_case": 5, "concurrent_clients": 1, "sql_experiment_timeout_seconds": 10, "application_repository_timeout_seconds": 1, "dataset_sha256": before, "trials": trials, "limitations": "Warm sequential closed-loop SQL through the same generated sqlc query; no HTTP/KDF. Offline query budget 10s, application repository unchanged at 1s. Same logical rows, VACUUM FULL and rebuild before trials. pgx prepared-plan behavior is retained. WAL is cluster-wide on a dedicated quiet instance; sampled CPU includes administration. No cold-cache or sustained-capacity claim."}, nil
|
||||
}
|
||||
|
||||
func (f *fixture) researchRead(ctx context.Context, chatID string, after *repository2.MessagePosition) ([]*model.Message, error) {
|
||||
queryCtx, cancel := context.WithTimeout(ctx, f.researchBudget)
|
||||
defer cancel()
|
||||
var id pgtype.UUID
|
||||
if err := id.Scan(chatID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
params := sqlc.GetMessagesByChatIDParams{ChatID: id, LimitCount: 51}
|
||||
if after != nil {
|
||||
params.AfterCreatedAt = pgtype.Timestamptz{Time: after.CreatedAt, Valid: true}
|
||||
if err := params.AfterID.Scan(after.ID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rows, err := sqlc.New(f.pool).GetMessagesByChatID(queryCtx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]*model.Message, len(rows))
|
||||
for i, m := range rows {
|
||||
items[i] = &model.Message{ID: m.ID.String(), ChatID: m.ChatID.String(), SenderID: m.SenderID.String(), Text: m.Text, CreatedAt: m.CreatedAt.Time}
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (f *fixture) messageDigest(ctx context.Context) (string, error) {
|
||||
rows, err := f.pool.Query(ctx, "SELECT row_to_json(m)::text FROM messages m ORDER BY id")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer rows.Close()
|
||||
h := sha256.New()
|
||||
for rows.Next() {
|
||||
var line string
|
||||
if err = rows.Scan(&line); err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, _ = fmt.Fprintln(h, line)
|
||||
}
|
||||
return hex.EncodeToString(h.Sum(nil)), rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user