This repository was archived by the owner on Feb 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathloadtest.py
More file actions
208 lines (176 loc) · 7.05 KB
/
loadtest.py
File metadata and controls
208 lines (176 loc) · 7.05 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import sys; sys.path.append('.') # NOQA
import os
import base64
import time
import random
import json
from storage import StorageClient
from molotov import setup_session, teardown_session, scenario
_PAYLOAD = """\
This is the metaglobal payload which contains
some client data that doesnt look much
like this
"""
_WEIGHTS = {'metaglobal': [40, 60, 0, 0, 0],
'distribution': [80, 15, 4, 1],
'count_distribution': [71, 15, 7, 4, 3],
'post_count_distribution': [67, 18, 9, 4, 2],
'delete_count_distribution': [99, 1, 0, 0, 0]}
_PROBS = {'get': .1, 'post': .2, 'deleteall': 0.01}
_COLLS = ['bookmarks', 'forms', 'passwords', 'history', 'prefs']
_BATCH_MAX_COUNT = 100
_DISABLE_DELETES = (os.environ.get('DISABLE_DELETES', 'false').lower()
in ('true', '1'))
def should_do(name):
return random.random() <= _PROBS[name]
def get_num_requests(name):
weights = _WEIGHTS[name]
i = random.randint(1, sum(weights))
count = 0
base = 0
for weight in weights:
base += weight
if i <= base:
break
count += 1
return count
@setup_session()
async def _session(worker_num, session):
exc = []
def _run():
try:
session.storage = StorageClient(session)
except Exception as e:
exc.append(e)
# XXX code will be migrated to Molotov
# see https://github.com/loads/molotov/issues/100
import threading
t = threading.Thread(target=_run)
t.start()
t.join()
if len(exc) > 0:
raise exc[0]
@teardown_session()
async def _teardown_session(worker_num, session):
if hasattr(session, 'storage') and session.storage:
session.storage.cleanup()
@scenario(1)
async def test(session):
storage = session.storage
# Respect the server limits.
_, config = await storage.get("/info/configuration")
# print("Config: {}".format(json.dumps(config, indent=3)))
# fix up consts
payload = _PAYLOAD[:config.get("max_record_payload_bytes")]
# GET requests to meta/global
num_requests = min(get_num_requests('metaglobal'),
config.get("max_post_records"))
batch_max_count = min(_BATCH_MAX_COUNT, config.get("max_total_records"))
# Always GET info/collections
# This is also a good opportunity to correct for timeskew.
url = "/info/collections"
resp, _ = await storage.get(url, (200, 404))
url = "/storage/meta/global"
for x in range(num_requests):
resp, __ = await storage.get(url, (200, 404))
if resp.status == 404:
data = json.dumps({"id": "global", "payload": payload})
await storage.put(url, data=data, statuses=(200,))
# Occasional reads of client records.
if should_do('get'):
url = "/storage/clients"
newer = int(time.time() - random.randint(3600, 360000))
params = {"full": "1", "newer": str(newer)}
await storage.get(url, params=params, statuses=(200, 404))
# Occasional updates to client records.
if should_do('post'):
cid = str(get_num_requests('distribution'))
url = "/storage/clients"
wbo = {'id': 'client' + cid, 'payload': cid * 300}
data = json.dumps([wbo])
resp, result = await storage.post(url, data=data, statuses=(200,))
assert len(result["success"]) == 1, "No success records"
assert len(result["failed"]) == 0, "Found failed record"
# GET requests to individual collections.
num_requests = get_num_requests('count_distribution')
cols = random.sample(_COLLS, num_requests)
for x in range(num_requests):
url = "/storage/" + cols[x]
newer = int(time.time() - random.randint(3600, 360000))
params = {"full": "1", "newer": str(newer)}
await storage.get(url, params=params, statuses=(200, 404))
# POST requests with several WBOs batched together
num_requests = get_num_requests('post_count_distribution')
# Let's do roughly 50% transactional batches.
transact = random.randint(0, 1)
batch_id = None
committing = False
# Collections should be a single static entry if we're "transactional"
if transact:
col = random.sample(_COLLS, 1)[0]
cols = [col for x in range(num_requests)]
else:
cols = random.sample(_COLLS, num_requests)
for x in range(num_requests):
url = "/storage/" + cols[x]
data = []
# Random batch size, skewed slightly towards the upper limit.
items_per_batch = min(random.randint(20, batch_max_count + 80),
batch_max_count)
for _i in range(items_per_batch):
randomness = os.urandom(10)
id = base64.urlsafe_b64encode(randomness).rstrip(b"=")
id = id.decode('utf8')
id += str(int((time.time() % 100) * 100000))
# Random payload length. They can be big, but skew small.
# This gives min=300, mean=450, max=config.max_record_payload_bytes
payload_length = min(
int(random.paretovariate(3) * 300),
config.get("max_record_payload_bytes"))
# XXX should be in the class
token = storage.auth_token.decode('utf8')
payload_chunks = int((payload_length / len(token)) + 1)
payload = (token * payload_chunks)[:payload_length]
wbo = {'id': id, 'payload': payload}
data.append(wbo)
data = json.dumps(data)
status = 200
if transact:
# Batch uploads only return a 200 on commit. An Accepted(202)
# is returned for batch creation & appends
status = 202
if x == 0:
committing = False
url += "?batch=true"
elif x == num_requests - 1:
url += "?commit=true&batch=%s" % batch_id
committing = True
batch_id = None
status = 200
else:
url += "?batch=%s" % batch_id
resp, result = await storage.post(url, data=data, statuses=(status,))
assert len(result["success"]) == items_per_batch, (
"Result success did not have expected number of"
"items in batch {}".format(result)
)
assert len(result["failed"]) == 0, (
"Result contained failed records: {}".format(result)
)
if transact and not committing:
batch_id = result["batch"]
if not _DISABLE_DELETES:
# DELETE requests.
# We might choose to delete some individual collections, or to
# do a full reset and delete all the data. Never both in the
# same run.
num_requests = get_num_requests('delete_count_distribution')
if num_requests:
cols = random.sample(_COLLS, num_requests)
for x in range(num_requests):
url = "/storage/" + cols[x]
resp, result = await storage.delete(url, statuses=(200, 204))
else:
if should_do('deleteall'):
url = "/storage"
resp, result = await storage.delete(url, statuses=(200,))