-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress_test.py
More file actions
73 lines (60 loc) · 2.32 KB
/
stress_test.py
File metadata and controls
73 lines (60 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
import os
import threading
import time
import random
# Add bindings directory to path
sys.path.append(os.path.join(os.getcwd(), "telco_core/bindings"))
from telco_core import TelcoSimulator, TelcoError, QuotaType, QuotaBucket
def stress_test():
db_path = os.path.join(os.getcwd(), "telco_core/simulator.db")
# Clean up old DB for clean test
if os.path.exists(db_path):
os.remove(db_path)
sim = TelcoSimulator("user_123", db_path)
# Buy a YouTube topping (Video)
video_topping = QuotaBucket(
name="YouTube 2GB",
remaining_bytes=2 * 1024 * 1024 * 1024,
category=QuotaType.VIDEO,
expiry=int(time.time()) + 3600
)
sim.buy_topping(video_topping)
stats = {
"success": 0,
"insufficient_balance": 0,
"total_calls": 0,
}
stats_lock = threading.Lock()
def update_stats(kind):
with stats_lock:
stats["total_calls"] += 1
stats[kind] += 1
def user_thread(id):
for _ in range(50):
# 50% chance to use video data, 50% general
cat = QuotaType.VIDEO if random.random() < 0.5 else QuotaType.GENERAL
usage = random.randint(10 * 1024 * 1024, 100 * 1024 * 1024) # 10MB to 100MB
try:
sim.simulate_usage(usage, cat)
update_stats("success")
except TelcoError.InsufficientBalance:
update_stats("insufficient_balance")
time.sleep(0.005)
threads = [threading.Thread(target=user_thread, args=(i,)) for i in range(10)]
print("Starting Multi-Quota Persistence test...")
for t in threads: t.start()
for t in threads: t.join()
print("\n--- Final State from DB ---")
info = sim.get_account_info()
for b in info.buckets:
print(f"Bucket: {b.name} | Category: {b.category} | Remaining: {b.remaining_bytes / (1024*1024):.2f} MB")
# Verify persistence by creating a new simulator instance pointing to same DB
print("\n--- Testing Persistence (New Instance) ---")
sim2 = TelcoSimulator("user_123", db_path)
info2 = sim2.get_account_info()
print(f"Buckets found in new instance: {len(info2.buckets)}")
assert len(info2.buckets) == len(info.buckets)
print("Persistence Verified!")
if __name__ == "__main__":
stress_test()