Skip to content

Commit bfcb551

Browse files
committed
add big users file benchmark
1 parent 4457ef7 commit bfcb551

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

tests/benchmark/run_big_file.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import dictdatabase as DDB
2+
import random
3+
import time
4+
5+
6+
def make_random_posts(count):
7+
posts = {}
8+
for _ in range(count):
9+
id = str(random.randint(0, 999_999_999))
10+
title_length = random.randint(10, 100)
11+
content_length = random.randint(200, 500)
12+
posts[id] = {
13+
'id': id,
14+
'title': "".join(random.choices(" abcdefghijklmnopqrstuvwxyz,.", k=title_length)),
15+
'content': "".join(random.choices(" abcdefghijklmnopqrstuvwxyz,.", k=content_length)),
16+
}
17+
return posts
18+
19+
20+
21+
def make_users(count):
22+
all_users = {}
23+
for i in range(count):
24+
all_users[str(i)] = {
25+
"id": str(i),
26+
"name": "".join(random.choices("abcdefghijklmnopqrstuvwxyz", k=5)),
27+
"surname": "".join(random.choices("abcdefghijklmnopqrstuvwxyz", k=20)),
28+
"age": random.randint(20, 80),
29+
"posts": make_random_posts(random.randint(200, 300)),
30+
}
31+
return all_users
32+
33+
34+
35+
36+
def read_specific_users():
37+
accessed_users = sorted([str(i * 100) for i in range(100)], key=lambda x: random.random())
38+
t1 = time.monotonic()
39+
for user_id in accessed_users:
40+
print(f"Accessing user {user_id}")
41+
u = DDB.at("big_users", key=user_id).read()
42+
print(f"User {user_id} has {len(u['posts'])} posts and is {u['age']} years old")
43+
t2 = time.monotonic()
44+
print(f"Time taken: {(t2 - t1) * 1000}ms")
45+
46+
47+
48+
def write_specific_users():
49+
accessed_users = sorted([str(i * 100) for i in range(100)], key=lambda x: random.random())
50+
t1 = time.monotonic()
51+
for user_id in accessed_users:
52+
print(f"Accessing user {user_id}")
53+
54+
with DDB.at("big_users", key=user_id).session() as (session, user):
55+
user["surname"] = "".join(random.choices("abcdefghijklmnopqrstuvwxyz", k=random.randint(3, 50)))
56+
session.write()
57+
t2 = time.monotonic()
58+
print(f"Time taken: {(t2 - t1) * 1000}ms")
59+
60+
61+
62+
63+
def random_access_users(write_read_ratio=0.1, count=500):
64+
accessed_users = [str(i * 100) for i in [random.randint(0, 99) for _ in range(count)]]
65+
t1 = time.monotonic()
66+
for user_id in accessed_users:
67+
68+
if random.random() < write_read_ratio:
69+
with DDB.at("big_users", key=user_id).session() as (session, user):
70+
user["surname"] = "".join(random.choices("abcdefghijklmnopqrstuvwxyz", k=random.randint(3, 50)))
71+
session.write()
72+
print(f"Accessed user {user_id} for writing")
73+
else:
74+
u = DDB.at("big_users", key=user_id).read()
75+
print(f"User {user_id} has {len(u['posts'])} posts and is {u['age']} years old")
76+
77+
t2 = time.monotonic()
78+
print(f"Time taken: {t2 - t1}s")
79+
80+
81+
82+
83+
84+
85+
# DDB.at("big_users").create(make_users(20_000), force_overwrite=True) # 2500MB
86+
87+
# random_access_users()
88+
# write_specific_users()
89+
read_specific_users()

0 commit comments

Comments
 (0)