"""Synthetic SQLite pagination benchmark. Not ScopeLink code or a production load test.""" import sqlite3, time, statistics, json, pathlib ROOT=pathlib.Path(__file__).parent def run(): db=sqlite3.connect(':memory:') db.execute('CREATE TABLE messages(id INTEGER PRIMARY KEY, account INTEGER NOT NULL, body BLOB NOT NULL)') db.executemany('INSERT INTO messages VALUES (?,?,?)', ((i,1,b'x'*128) for i in range(1,1_000_001))) db.execute('CREATE INDEX ix_account_id ON messages(account,id)'); db.commit() results=[] for offset in [0,10_000,100_000,900_000]: q1='SELECT id,body FROM messages WHERE account=? ORDER BY id LIMIT 50 OFFSET ?' q2='SELECT id,body FROM messages WHERE account=? AND id>? ORDER BY id LIMIT 50' a=db.execute(q1,(1,offset)).fetchall(); b=db.execute(q2,(1,offset)).fetchall(); assert a==b times=[[],[]] for i in range(30): for j in ([0,1] if i%2==0 else [1,0]): t=time.perf_counter_ns(); db.execute([q1,q2][j],(1,offset)).fetchall(); times[j].append((time.perf_counter_ns()-t)/1e6) results.append({'offset':offset,'offset_median_ms':statistics.median(times[0]),'cursor_median_ms':statistics.median(times[1]),'same_rows':True}) db.close() # Deterministic arithmetic model, not network performance. capacity=[{'users':n,'poll_every_s':5,'poll_requests_per_s':n/5,'poll_requests_per_day':n*86400/5,'messages_per_day_at_10_each':n*10} for n in (1000,10000,100000,1000000)] out={'sqlite_version':sqlite3.sqlite_version,'rows':1000000,'page_size':50,'repetitions':30,'storage':'in-memory synthetic one account; warm queries; no concurrent writes','pagination':results,'polling_model':capacity} return out if __name__=='__main__': result=run(); print(json.dumps(result,indent=2)); (ROOT/'investigacao-026-reproducao.json').write_text(json.dumps(result,indent=2)+'\n')