First push to rauhala gitea.
This commit is contained in:
99
test_slidingsqlite.py
Normal file
99
test_slidingsqlite.py
Normal file
@@ -0,0 +1,99 @@
|
||||
import time
|
||||
import uuid
|
||||
import hashlib
|
||||
import threading
|
||||
import random
|
||||
from datetime import datetime, timezone
|
||||
from SlidingSqlite import SlidingSQLite
|
||||
import logging
|
||||
|
||||
# Set up logging
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format="%(asctime)s - %(levelname)s - %(message)s",
|
||||
handlers=[logging.StreamHandler()],
|
||||
)
|
||||
|
||||
# Configuration
|
||||
NUM_WRITER_THREADS = 4 # Number of writer threads
|
||||
NUM_READER_THREADS = 2 # Number of reader threads
|
||||
TARGET_OPS_PER_SECOND = 10 # Target database operations per second
|
||||
|
||||
# Define a more complex schema
|
||||
db_schema = """
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
username TEXT UNIQUE,
|
||||
created_at TEXT
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS events (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id INTEGER,
|
||||
event_type TEXT,
|
||||
event_timestamp TEXT,
|
||||
hash TEXT,
|
||||
FOREIGN KEY(user_id) REFERENCES users(id)
|
||||
);
|
||||
"""
|
||||
|
||||
# Initialize SlidingSQLite
|
||||
db = SlidingSQLite(
|
||||
db_dir="./databases",
|
||||
schema=db_schema,
|
||||
rotation_interval=10,
|
||||
retention_period=60,
|
||||
cleanup_interval=30,
|
||||
auto_delete_old_dbs=True,
|
||||
)
|
||||
|
||||
def generate_md5():
|
||||
return hashlib.md5(str(uuid.uuid4()).encode()).hexdigest()
|
||||
|
||||
def insert_user():
|
||||
username = f"user_{uuid.uuid4().hex[:8]}"
|
||||
created_at = datetime.now(timezone.utc).isoformat()
|
||||
db.execute_write("INSERT INTO users (username, created_at) VALUES (?, ?)", (username, created_at))
|
||||
return username
|
||||
|
||||
def insert_event():
|
||||
query_id = db.execute_read("SELECT id FROM users ORDER BY RANDOM() LIMIT 1", ())
|
||||
result = db.get_read_result(query_id)
|
||||
if result.success and result.data:
|
||||
user_id = result.data[0][0]
|
||||
event_type = "login" if uuid.uuid4().int % 2 == 0 else "logout"
|
||||
event_timestamp = datetime.now(timezone.utc).isoformat()
|
||||
event_hash = generate_md5()
|
||||
db.execute_write("INSERT INTO events (user_id, event_type, event_timestamp, hash) VALUES (?, ?, ?, ?)", (user_id, event_type, event_timestamp, event_hash))
|
||||
|
||||
def writer_thread():
|
||||
while True:
|
||||
insert_user()
|
||||
insert_event()
|
||||
time.sleep(random.uniform(0.05, 0.15)) # Randomized sleep to target ~10 ops/sec
|
||||
|
||||
def reader_thread():
|
||||
while True:
|
||||
query_id = db.execute_read("SELECT e.event_type, u.username, e.event_timestamp FROM events e JOIN users u ON e.user_id = u.id ORDER BY e.event_timestamp DESC LIMIT 5", ())
|
||||
result = db.get_read_result(query_id)
|
||||
if result.success:
|
||||
logging.info(f"Recent events: {result.data}")
|
||||
time.sleep(random.uniform(0.5, 1.5)) # Randomized sleep for more natural load
|
||||
|
||||
# Start multiple writer and reader threads
|
||||
threads = []
|
||||
for _ in range(NUM_WRITER_THREADS):
|
||||
t = threading.Thread(target=writer_thread, daemon=True)
|
||||
t.start()
|
||||
threads.append(t)
|
||||
for _ in range(NUM_READER_THREADS):
|
||||
t = threading.Thread(target=reader_thread, daemon=True)
|
||||
t.start()
|
||||
threads.append(t)
|
||||
|
||||
try:
|
||||
print("Running multi-threaded SlidingSQLite test. Press Ctrl+C to stop.")
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
print("\nShutting down...")
|
||||
db.shutdown()
|
||||
Reference in New Issue
Block a user