forked from mtusi2026/mtusi2026lab2
Implement lab2 PostgreSQL authentication and Redis sessions
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
"""Create synthetic 10k-message history and print query plan. Execute inside api container.
|
||||
Only generates isolated synthetic UUID user/chat; never truncates real data.
|
||||
"""
|
||||
import os
|
||||
import uuid
|
||||
import psycopg
|
||||
|
||||
|
||||
def main():
|
||||
uid, cid = uuid.uuid4(), uuid.uuid4()
|
||||
with psycopg.connect(os.environ['DATABASE_URL']) as conn:
|
||||
conn.execute("INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s)",
|
||||
(uid, 'ex_' + uuid.uuid4().hex[:12], b'EXPLAIN fixture', 'synthetic-not-for-login'))
|
||||
conn.execute("INSERT INTO chats(id,title) VALUES (%s,%s)", (cid,b'EXPLAIN synthetic history'))
|
||||
conn.execute("INSERT INTO chat_members(chat_id,user_id,position) VALUES (%s,%s,0)", (cid,uid))
|
||||
conn.execute("""
|
||||
INSERT INTO messages(id,chat_id,sender_id,text,created_at)
|
||||
SELECT gen_random_uuid(), %s, %s, convert_to('synthetic-' || n::text, 'UTF8'),
|
||||
clock_timestamp() + (n * interval '1 microsecond')
|
||||
FROM generate_series(1, 10000) AS n
|
||||
""", (cid,uid))
|
||||
middle = conn.execute("""
|
||||
SELECT created_at,id FROM messages WHERE chat_id=%s
|
||||
ORDER BY created_at,id OFFSET 5000 LIMIT 1
|
||||
""", (cid,)).fetchone()
|
||||
conn.execute('ANALYZE messages')
|
||||
count = conn.execute('SELECT count(*) FROM messages WHERE chat_id=%s', (cid,)).fetchone()[0]
|
||||
assert count==10000
|
||||
plan = conn.execute("""
|
||||
EXPLAIN (ANALYZE,BUFFERS)
|
||||
SELECT id,chat_id,sender_id,text,created_at
|
||||
FROM messages WHERE chat_id=%s AND (created_at,id)>(%s,%s)
|
||||
ORDER BY created_at,id LIMIT 50
|
||||
""", (cid,*middle)).fetchall()
|
||||
print('Fixture chat_id:', cid, '; history size:', count)
|
||||
print('\n'.join(row[0] for row in plan))
|
||||
print('Index intended: idx_messages_chat_history (chat_id,created_at,id)')
|
||||
print('Fixture retained to reproduce measurements; clean only in isolated test volume.')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user