Skip to content

Commit 2aa6bdb

Browse files
committed
Add performance test
1 parent 8557d9d commit 2aa6bdb

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

tests/app/performance_test.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import subprocess
2+
import time
3+
import os
4+
import shutil
5+
import sys
6+
7+
8+
def get_cell(seconds):
9+
cell = ', { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], ' \
10+
'"source": [ "time.sleep(' + str(seconds) + ')\\n", "t1 = time.time()\\n", "log += ' \
11+
'str(t1 - t0) + \'\\\\n\'\\n", "t0 = t1" ] }'
12+
return cell
13+
14+
15+
def get_ipynb(cell_nb, sleep_per_cell, result_dir):
16+
ipynb = '{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs"' \
17+
': [], "source": [ "import uuid\\n", "import time\\n", "fname = str(uuid.uuid4()) + \'' \
18+
'.log\'\\n", "log = \'\'\\n", "t0 = time.time()" ] }'
19+
for i in range(cell_nb):
20+
cell = get_cell(sleep_per_cell)
21+
ipynb += cell
22+
ipynb += ', { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], ' \
23+
'"source": [ "with open(\'' + result_dir + '/\' + fname, \'w\') as f:\\n", " ' \
24+
'f.write(log)" ] }'
25+
ipynb += ' ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", ' \
26+
'"name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", ' \
27+
'"version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python"' \
28+
', "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.1" } }' \
29+
', "nbformat": 4, "nbformat_minor": 4 }'
30+
return ipynb
31+
32+
33+
def test_performance():
34+
sleep_per_cell = 0.1 # each cell sleeps for that amount of seconds
35+
cell_nb = 100 # each notebook consists of so many cells
36+
client_nb = 10 # number of clients (kernels) launched in parallel
37+
ipynb_path = 'tests/notebooks/sleep.ipynb' # generated notebook, results are stored in the directory without ".ipynb"
38+
39+
ipynb_dname, ipynb_fname = os.path.split(ipynb_path)
40+
os.chdir(ipynb_dname)
41+
result_dir = ipynb_fname[:-6]
42+
43+
if os.path.exists(result_dir):
44+
shutil.rmtree(result_dir)
45+
os.makedirs(result_dir)
46+
47+
ipynb = get_ipynb(cell_nb, sleep_per_cell, result_dir)
48+
49+
# generate notebook
50+
with open(ipynb_fname, 'w') as f:
51+
f.write(ipynb)
52+
53+
# launch voila
54+
voila = subprocess.Popen(('voila --no-browser ' + ipynb_fname).split())
55+
time.sleep(1)
56+
57+
# launch clients
58+
clients = [subprocess.Popen('wget -O/dev/null -q http://localhost:8866'.split()) for i in range(client_nb)]
59+
60+
# wait for all notebooks to execute
61+
t0 = time.time()
62+
done = False
63+
timeout = False
64+
i = 0
65+
exec_time = cell_nb * sleep_per_cell # notebook execution time
66+
launch_time = client_nb * 0.5 # kernel takes about 0.5s to launch
67+
# min_time = launch_time + exec_time # theoretical time for all notebooks to execute
68+
timeout_time = launch_time + 2 * exec_time # timeout allows slow machines to finish
69+
while not done:
70+
time.sleep(1)
71+
done = True
72+
i += 1
73+
# print(i, '/', min_time)
74+
for client in clients:
75+
if client.poll() is None:
76+
done = False
77+
if not done:
78+
if time.time() - t0 > timeout_time:
79+
done = True
80+
timeout = True
81+
82+
# stop voila and all clients
83+
voila.kill()
84+
[client.kill() for client in clients]
85+
86+
if timeout:
87+
raise TimeoutError
88+
89+
# analyze data
90+
fnames = os.listdir(result_dir)
91+
data = []
92+
for fname in fnames:
93+
with open(result_dir + '/' + fname) as f:
94+
data += [float(d) for d in f.read().split()]
95+
96+
meantime_per_cell = sum(data) / len(data)
97+
exceed_pct = 50 # allowing for time budget exceedance (%)
98+
maxtime_per_cell = sleep_per_cell * (exceed_pct / 100 + 1)
99+
if meantime_per_cell > maxtime_per_cell:
100+
print('Mean time per cell', meantime_per_cell, '>', sleep_per_cell, '(with', exceed_pct, '% margin)')
101+
sys.exit(1)

0 commit comments

Comments
 (0)