forked from mozilla-services/ailoads-syncto
/
loadtest.py
186 lines (144 loc) · 5.36 KB
/
loadtest.py
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
import json
import os
import uuid
from ailoads.fmwk import scenario, requests
# Read configuration from env
SERVER_URL = os.getenv('SYNCTO_SERVER_URL',
"https://syncto.stage.mozaws.net:443").rstrip('/')
FXA_BROWSERID_ASSERTION = os.getenv("FXA_BROWSERID_ASSERTION")
FXA_CLIENT_STATE = os.getenv("FXA_CLIENT_STATE")
if not FXA_BROWSERID_ASSERTION and not FXA_CLIENT_STATE:
raise ValueError("Please define FXA_BROWSERID_ASSERTION "
"and FXA_CLIENT_STATE env variables.")
_CONNECTIONS = {}
def get_connection(conn_id=None):
if conn_id is None or conn_id not in _CONNECTIONS:
conn = SynctoConnection(uuid.uuid4().hex)
_CONNECTIONS[conn_id] = conn
return _CONNECTIONS[conn_id]
class SynctoConnection(object):
def __init__(self, id):
self.id = id
self.headers = {
"Authorization": "BrowserID %s" % FXA_BROWSERID_ASSERTION,
"X-Client-State": FXA_CLIENT_STATE
}
self.timeout = 300000
def get(self, endpoint):
return requests.get(
SERVER_URL + endpoint,
headers=self.headers,
timeout=self.timeout)
def post(self, endpoint, data):
return requests.post(
SERVER_URL + endpoint,
json=data,
headers=self.headers,
timeout=self.timeout)
def put(self, endpoint, data):
return requests.put(
SERVER_URL + endpoint,
json=data,
headers=self.headers,
timeout=self.timeout)
def delete(self, endpoint):
return requests.delete(
SERVER_URL + endpoint,
headers=self.headers,
timeout=self.timeout)
@scenario(20)
def readonly_crypto():
"""Syncing history from an existing account"""
conn = get_connection('user1')
# 1. Connecting to get back the crypto key
r = conn.get('/v1/buckets/syncto/collections/crypto/records')
r.raise_for_status()
body = r.json()
assert "data" in body, "data not found in body"
@scenario(20)
def readonly_meta():
"""Syncing history from an existing account"""
conn = get_connection('user1')
# 2. Connecting to get back the meta informations
r = conn.get('/v1/buckets/syncto/collections/meta/records')
r.raise_for_status()
body = r.json()
assert "data" in body, "data not found in body"
@scenario(20)
def readonly_bookmarks():
"""Syncing history from an existing account"""
conn = get_connection('user1')
# 3. Connecting to get the bookmarks (for TV)
r = conn.get('/v1/buckets/syncto/collections/bookmarks/records')
r.raise_for_status()
body = r.json()
assert "data" in body, "data not found in body"
@scenario(20)
def readonly_history():
"""Syncing history from an existing account"""
conn = get_connection('user1')
# 4. Connecting to get the history
r = conn.get('/v1/buckets/syncto/collections/history/records')
r.raise_for_status()
body = r.json()
assert "data" in body, "data not found in body"
@scenario(20)
def readonly_passwords():
"""Syncing history from an existing account"""
conn = get_connection('user1')
# 5. Connecting to get the passwords
r = conn.get('/v1/buckets/syncto/collections/passwords/records')
r.raise_for_status()
body = r.json()
assert "data" in body, "data not found in body"
@scenario(30)
def write_history():
"""Adding some history data."""
conn = get_connection('user1')
PAYLOAD = {
"ciphertext": ("75IcW3P4WxUJipehWryevc+ygK5vojh3nOadu7YSX9"
"zJSm3eBHu5lNIg1UtDyt3b"),
"IV": "Sj3U2Nkk2IjE2S59hv0m7Q==",
"hmac": ("c6a530f3486142d1069f80bfaff907e0cc077a892cf7f9bd"
"62f943b68b610351")
}
payload = {"data": {"payload": json.dumps(PAYLOAD), "sortindex": 2000}}
# Adding some history.
r = conn.put('/v1/buckets/syncto/collections/history/records/d2X1O6-DyeFS',
payload)
r.raise_for_status()
body = r.json()
assert "data" in body, "data not found in body"
# Removing some history
r = conn.delete('/v1/buckets/syncto/collections/history'
'/records/d2X1O6-DyeFS')
r.raise_for_status()
@scenario(30)
def batch_write_history():
"""Adding some history data."""
conn = get_connection('user1')
PAYLOAD = {
"ciphertext": ("75IcW3P4WxUJipehWryevc+ygK5vojh3nOadu7YSX9"
"zJSm3eBHu5lNIg1UtDyt3b"),
"IV": "Sj3U2Nkk2IjE2S59hv0m7Q==",
"hmac": ("c6a530f3486142d1069f80bfaff907e0cc077a892cf7f9bd"
"62f943b68b610351")
}
payload = {"requests": [{
"method": "PUT",
"path": "/buckets/syncto/collections/history/records/zpbhM1shjTMx",
"body": {"data": {"payload": json.dumps(PAYLOAD), "sortindex": 2000}}
}]}
# Adding some history.
r = conn.post('/v1/batch', payload)
r.raise_for_status()
body = r.json()
assert "responses" in body, "responses not found in body"
error_msg = "responses length is %d instead of 1" % len(body['responses'])
assert len(body['responses']) == 1, error_msg
status = body['responses'][0]['status']
assert status == 200, "sub request status was %d: %s" % (status, body)
# Removing some history
r = conn.delete('/v1/buckets/syncto/collections/history'
'/records/zpbhM1shjTMx')
r.raise_for_status()