forked from mtusi2026/mtusi2026lab2
ЛР 2: завершить трек 2 на оценку 5
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)
|
||||
|
||||
Reference in New Issue
Block a user