forked from mtusi2026/mtusi2026lab2
lab2
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Seed >=100k messages with skewed chat sizes (track 2)."""
|
||||
import os
|
||||
import sys
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import psycopg
|
||||
|
||||
TARGET = int(os.environ.get('TRACK2_MESSAGE_COUNT', '100000'))
|
||||
DATABASE_URL = os.environ.get('DATABASE_URL', '')
|
||||
|
||||
|
||||
def main():
|
||||
if not DATABASE_URL:
|
||||
print('DATABASE_URL required', file=sys.stderr)
|
||||
raise SystemExit(2)
|
||||
base = datetime.now(timezone.utc)
|
||||
with psycopg.connect(DATABASE_URL) as conn:
|
||||
hot_exists = conn.execute(
|
||||
"SELECT 1 FROM chats WHERE title = 'track2_hot'"
|
||||
).fetchone()
|
||||
if hot_exists:
|
||||
total = conn.execute('SELECT count(*) FROM messages').fetchone()[0]
|
||||
print(f'skip_seed existing track2 dataset messages_total={total}')
|
||||
return
|
||||
row = conn.execute(
|
||||
"SELECT id FROM users WHERE username = 'track2_bench'"
|
||||
).fetchone()
|
||||
if row:
|
||||
user_id = str(row[0])
|
||||
else:
|
||||
user_id = str(uuid.uuid4())
|
||||
conn.execute(
|
||||
'INSERT INTO users (id, username, display_name, password_hash, created_at) '
|
||||
'VALUES (%s, %s, %s, %s, %s)',
|
||||
(user_id, 'track2_bench', 'Track2', 'x', base),
|
||||
)
|
||||
plans = [
|
||||
('hot', int(TARGET * 0.70)),
|
||||
('medium_a', int(TARGET * 0.05)),
|
||||
('medium_b', int(TARGET * 0.05)),
|
||||
('medium_c', int(TARGET * 0.05)),
|
||||
('medium_d', int(TARGET * 0.05)),
|
||||
('medium_e', int(TARGET * 0.05)),
|
||||
]
|
||||
tail = TARGET - sum(count for _, count in plans)
|
||||
small_chats = 50
|
||||
per_small = max(1, tail // small_chats)
|
||||
chat_ids = []
|
||||
for name, count in plans:
|
||||
chat_id = str(uuid.uuid4())
|
||||
chat_ids.append((chat_id, count, name))
|
||||
conn.execute(
|
||||
'INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)',
|
||||
(chat_id, 'track2_' + name, base),
|
||||
)
|
||||
conn.execute(
|
||||
'INSERT INTO chat_members (chat_id, user_id, position) VALUES (%s, %s, 0)',
|
||||
(chat_id, user_id),
|
||||
)
|
||||
for index in range(small_chats):
|
||||
chat_id = str(uuid.uuid4())
|
||||
chat_ids.append((chat_id, per_small, f'small_{index}'))
|
||||
conn.execute(
|
||||
'INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)',
|
||||
(chat_id, 'track2_small_' + str(index), base),
|
||||
)
|
||||
conn.execute(
|
||||
'INSERT INTO chat_members (chat_id, user_id, position) VALUES (%s, %s, 0)',
|
||||
(chat_id, user_id),
|
||||
)
|
||||
total = 0
|
||||
with conn.cursor() as cur:
|
||||
for chat_id, count, _name in chat_ids:
|
||||
rows = []
|
||||
for offset in range(count):
|
||||
rows.append(
|
||||
(
|
||||
str(uuid.uuid4()),
|
||||
chat_id,
|
||||
user_id,
|
||||
't',
|
||||
base + timedelta(microseconds=offset),
|
||||
)
|
||||
)
|
||||
cur.executemany(
|
||||
'INSERT INTO messages (id, chat_id, sender_id, text, created_at) '
|
||||
'VALUES (%s, %s, %s, %s, %s)',
|
||||
rows,
|
||||
)
|
||||
total += count
|
||||
conn.commit()
|
||||
actual = conn.execute('SELECT count(*) FROM messages').fetchone()[0]
|
||||
hot = conn.execute(
|
||||
"SELECT count(*) FROM messages m JOIN chats c ON c.id = m.chat_id WHERE c.title = 'track2_hot'"
|
||||
).fetchone()[0]
|
||||
print(f'seeded_messages={total} table_total={actual} hot_chat_messages={hot}')
|
||||
print(f'benchmark_user_id={user_id}')
|
||||
for chat_id, count, name in chat_ids[:3]:
|
||||
print(f'sample_chat {name} id={chat_id} messages={count}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user