forked from open-lambda/open-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·312 lines (242 loc) · 9.77 KB
/
test.py
File metadata and controls
executable file
·312 lines (242 loc) · 9.77 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python3
''' Various integration tests for the open lambda framwork '''
# pylint: disable=global-statement, too-many-statements, fixme, broad-except, too-many-locals, missing-function-docstring
import argparse
import os
import tempfile
from time import time
from subprocess import call
from multiprocessing import Pool
import requests
from helper import DockerWorker, SockWorker, prepare_open_lambda, setup_config
from helper import get_current_config, TestConfContext, assert_eq
from helper.test import set_test_filter, start_tests, check_test_results, set_worker_type, test
from open_lambda import OpenLambda
# These will be set by argparse in main()
OL_DIR = None
@test
def install_tests():
# we want to make sure we see the expected number of pip installs,
# so we don't want installs lying around from before
return_code = call(['rm', '-rf', f'{OL_DIR}/lambda/packages/*'])
assert_eq(return_code, 0)
open_lambda = OpenLambda()
# try something that doesn't install anything
msg = 'hello world'
jdata = open_lambda.run("echo", msg)
if jdata != msg:
raise ValueError(f"found {jdata} but expected {msg}")
jdata = open_lambda.get_statistics()
installs = jdata.get('pull-package.cnt', 0)
assert_eq(installs, 0)
for pos in range(3):
name = f"install{pos+1}"
result = open_lambda.run(name, {})
assert_eq(result, "imported")
result = open_lambda.get_statistics()
installs = result['pull-package.cnt']
if pos < 2:
# with deps, requests should give us these:
# certifi, charset-normalizer, idna, requests, urllib3
assert_eq(installs, 5)
else:
# requests (and deps) + simplejson
assert_eq(installs, 6)
def check_status_code(req):
if req.status_code != 200:
raise requests.HTTPError(f"STATUS {req.status_code}: {req.text}")
@test
def numpy_test():
open_lambda = OpenLambda()
# try adding the nums in a few different matrixes. Also make sure
# we can have two different numpy versions co-existing.
result = open_lambda.run("numpy23", [1, 2])
assert_eq(result['result'], 3)
assert result['numpy-version'].startswith('1.23')
result = open_lambda.run("numpy24", [[1, 2], [3, 4]])
assert_eq(result['result'], 10)
assert result['numpy-version'].startswith('1.24')
result = open_lambda.run("numpy24", [[[1, 2], [3, 4]], [[1, 2], [3, 4]]])
assert_eq(result['result'], 20)
assert result['numpy-version'].startswith('1.24')
result = open_lambda.run("pandas", [[0, 1, 2], [3, 4, 5]])
assert_eq(result['result'], 15)
assert float(".".join(result['numpy-version'].split('.')[:2])) >= 1.24
result = open_lambda.run("pandas-v1", [[1, 2, 3], [1, 2, 3]])
assert_eq(result['result'], 12)
assert result['numpy-version'].startswith('1.24')
def stress_one_lambda_task(args):
open_lambda = OpenLambda()
start, seconds = args
pos = 0
while time() < start + seconds:
result = open_lambda.run("echo", pos, json=False)
assert_eq(result, str(pos))
pos += 1
return pos
@test
def stress_one_lambda(procs, seconds):
start = time()
with Pool(procs) as pool:
reqs = sum(pool.map(stress_one_lambda_task, [(start, seconds)] * procs, chunksize=1))
return {"reqs_per_sec": reqs/seconds}
@test
def call_each_once_exec(lambda_count, alloc_mb, zygote_provider):
with TestConfContext(features={"import_cache": zygote_provider}):
open_lambda = OpenLambda()
# TODO: do in parallel
start = time()
for pos in range(lambda_count):
result = open_lambda.run(f"L{pos}", {"alloc_mb": alloc_mb}, json=False)
assert_eq(result, str(pos))
seconds = time() - start
return {"reqs_per_sec": lambda_count/seconds}
def call_each_once(lambda_count, alloc_mb=0, zygote_provider="tree"):
with tempfile.TemporaryDirectory() as reg_dir:
# create dummy lambdas
for pos in range(lambda_count):
with open(os.path.join(reg_dir, f"L{pos}.py"), "w", encoding='utf-8') as code:
code.write("def f(event):\n")
code.write(" global s\n")
code.write(f" s = '*' * {alloc_mb} * 1024**2\n")
code.write(f" return {pos}\n")
with TestConfContext(registry=reg_dir):
call_each_once_exec(lambda_count=lambda_count, alloc_mb=alloc_mb,
zygote_provider=zygote_provider)
@test
def fork_bomb():
open_lambda = OpenLambda()
limit = get_current_config()["limits"]["procs"]
result = open_lambda.run("fbomb", {"times": limit*2}, json=False)
# the function returns the number of children that we were able to fork
assert 1 <= int(result) <= limit
@test
def max_mem_alloc():
open_lambda = OpenLambda()
limit = get_current_config()["limits"]["mem_mb"]
result = open_lambda.run("max_mem_alloc", None)
# the function returns the MB that was able to be allocated
assert limit-16 <= int(result) <= limit
@test
def ping_test():
open_lambda = OpenLambda()
pings = 1000
start = time()
for _ in range(pings):
open_lambda.check_status()
seconds = time() - start
return {"pings_per_sec": pings/seconds}
@test
def update_code():
curr_conf = get_current_config()
reg_dir = curr_conf['registry']
cache_seconds = curr_conf['registry_cache_ms'] / 1000
open_lambda = OpenLambda()
for pos in range(3):
# update function code
with open(os.path.join(reg_dir, "version.py"), "w", encoding='utf-8') as code:
code.write("def f(event):\n")
code.write(f" return {pos}\n")
# how long does it take for us to start seeing the latest code?
start = time()
while True:
text = open_lambda.run("version", None)
num = int(text)
assert num >= pos-1
end = time()
# make sure the time to grab new code is about the time
# specified for the registry cache (within ~1 second)
assert end - start <= cache_seconds + 1
if num == pos:
if pos > 0:
assert end - start >= cache_seconds - 1
break
@test
def recursive_kill(depth):
open_lambda = OpenLambda()
parent = ""
for _ in range(depth):
result = open_lambda.create({"code": "", "leaf": False, "parent": parent})
if parent:
# don't need this parent any more, so pause it to get
# memory back (so we can run this test with low memory)
open_lambda.pause(parent)
parent = result.strip()
open_lambda.destroy("1")
stats = open_lambda.get_statistics()
destroys = stats['Destroy():ms.cnt']
assert_eq(destroys, depth)
@test
def flask_test():
url = 'http://localhost:5000/run/flask-test'
print("URL", url)
r = requests.get(url)
print("RESPONSE", r)
# flask apps should have control of status code, headers, and response body
if r.status_code != 418:
raise ValueError(f"expected status code 418, but got {r.status_code}")
if not "A" in r.headers:
raise ValueError(f"'A' not found in headers, as expected: {r.headers}")
if r.headers["A"] != "B":
raise ValueError(f"headers['A'] should be 'B', not {r.headers['A']}")
if r.text != "hi\n":
raise ValueError(f"r.text should be 'hi\n', not {repr(r.text)}")
def run_tests():
ping_test()
# do smoke tests under various configs
with TestConfContext(features={"import_cache": ""}):
install_tests()
with TestConfContext(mem_pool_mb=1000):
install_tests()
# test resource limits
fork_bomb()
max_mem_alloc()
# numpy pip install needs a larger mem cap
with TestConfContext(mem_pool_mb=1000, trace={"cgroups": True}):
numpy_test()
# make sure we can use WSGI apps based on frameworks like Flask
flask_test()
# make sure code updates get pulled within the cache time
with tempfile.TemporaryDirectory() as reg_dir:
with TestConfContext(registry=reg_dir, registry_cache_ms=3000):
update_code()
# test heavy load
with TestConfContext():
stress_one_lambda(procs=1, seconds=15)
stress_one_lambda(procs=2, seconds=15)
stress_one_lambda(procs=8, seconds=15)
with TestConfContext():
call_each_once(lambda_count=10, alloc_mb=1, zygote_provider="tree")
call_each_once(lambda_count=100, alloc_mb=10, zygote_provider="")
call_each_once(lambda_count=100, alloc_mb=10, zygote_provider="tree")
call_each_once(lambda_count=100, alloc_mb=10, zygote_provider="multitree")
def main():
global OL_DIR
parser = argparse.ArgumentParser(description='Run tests for OpenLambda')
parser.add_argument('--worker_type', type=str, default="sock")
parser.add_argument('--test_filter', type=str, default="")
parser.add_argument('--registry', type=str, default="test-registry")
parser.add_argument('--ol_dir', type=str, default="test-dir")
args = parser.parse_args()
set_test_filter([name for name in args.test_filter.split(",") if name != ''])
OL_DIR = args.ol_dir
setup_config(args.ol_dir)
prepare_open_lambda(args.ol_dir)
trace_config = {
"cgroups": True,
"memory": True,
"evictor": True,
"package": True,
}
with TestConfContext(registry=os.path.abspath(args.registry), trace=trace_config):
if args.worker_type == 'docker':
set_worker_type(DockerWorker)
elif args.worker_type == 'sock':
set_worker_type(SockWorker)
else:
raise RuntimeError(f"Invalid worker type {args.worker_type}")
start_tests()
run_tests()
check_test_results()
if __name__ == '__main__':
main()