-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathnotebook_tester.py
286 lines (236 loc) · 10.8 KB
/
notebook_tester.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
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
"""Runs notebooks.
See --help text for more information.
"""
import argparse
import nbformat
import numpy
import os
import sys
import time
from collections import defaultdict
from jupyter_client.manager import start_new_kernel
# Exception for problems that occur while executing cell.
class ExecuteException(Exception):
def __init__(self, cell_index):
self.cell_index = cell_index
# There was an error (that did not crash the kernel) while executing the cell.
class ExecuteError(ExecuteException):
def __init__(self, cell_index, reply, stdout):
super(ExecuteError, self).__init__(cell_index)
self.reply = reply
self.stdout = stdout
def __str__(self):
return 'ExecuteError at cell %d, reply:\n%s\n\nstdout:\n%s' % (
self.cell_index, self.reply, self.stdout)
# The kernel crashed while executing the cell.
class ExecuteCrash(ExecuteException):
def __init__(self, cell_index):
super(ExecuteCrash, self).__init__(cell_index)
def __str__(self):
return 'ExecuteCrash at cell %d' % self.cell_index
# Exception for problems that occur during a completion request.
class CompleteException(Exception):
def __init__(self, cell_index, char_index):
self.cell_index = cell_index
self.char_index = char_index
# There was an error (that did not crash the kernel) while processing a
# completion request.
class CompleteError(CompleteException):
def __init__(self, cell_index, char_index):
super(CompleteError, self).__init__(cell_index, char_index)
def __str__(self):
return 'CompleteError at cell %d, char %d' % (self.cell_index,
self.char_index)
# The kernel crashed while processing a completion request.
class CompleteCrash(CompleteException):
def __init__(self, cell_index, char_index):
super(CompleteCrash, self).__init__(cell_index, char_index)
def __str__(self):
return 'CompleteCrash at cell %d, char %d' % (self.cell_index,
self.char_index)
class NotebookTestRunner:
def __init__(self, notebook, char_step=1, repeat_times=1,
execute_timeout=60, complete_timeout=5, verbose=True):
"""
noteboook - path to a notebook to run the test on
char_step - number of chars to step per completion request. 0 disables
repeat_times - run the notebook this many times, in the same kernel
instance
execute_timeout - number of seconds to wait for cell execution
complete_timeout - number of seconds to wait for completion
verbose - print progress, statistics, and errors
"""
self.char_step = char_step
self.repeat_times = repeat_times
self.execute_timeout = execute_timeout
self.complete_timeout = complete_timeout
self.verbose = verbose
notebook_dir = os.path.dirname(notebook)
os.chdir(notebook_dir)
nb = nbformat.read(notebook, as_version=4)
self.code_cells = [cell for cell in nb.cells
if cell.cell_type == 'code' \
and not cell.source.startswith('#@title')]
self.stdout = []
self.unexpected_errors = []
def _execute_cell(self, cell_index):
code = self.code_cells[cell_index].source
self._execute_code(code, cell_index)
def _execute_code(self, code, cell_index=-1):
self.kc.execute(code)
# Consume all the iopub messages that the execution produced.
stdout = ''
while True:
try:
reply = self.kc.get_iopub_msg(timeout=self.execute_timeout)
except TimeoutError:
# Timeout usually means that the kernel has crashed.
raise ExecuteCrash(cell_index)
if reply['header']['msg_type'] == 'stream' and \
reply['content']['name'] == 'stdout':
stdout += reply['content']['text']
if reply['header']['msg_type'] == 'status' and \
reply['content']['execution_state'] == 'idle':
break
# Consume the shell message that the execution produced.
try:
reply = self.kc.get_shell_msg(timeout=self.execute_timeout)
except TimeoutError:
# Timeout usually means that the kernel has crashed.
raise ExecuteCrash(cell_index)
if reply['content']['status'] != 'ok':
raise ExecuteError(cell_index, reply, stdout)
if cell_index >= 0:
self.stdout.append(stdout)
return stdout
def _complete(self, cell_index, char_index):
code = self.code_cells[cell_index].source[:char_index]
try:
reply = self.kc.complete(code, reply=True, timeout=self.complete_timeout)
except TimeoutError:
# Timeout usually means that the kernel has crashed.
raise CompleteCrash(cell_index, char_index)
if reply['content']['status'] != 'ok':
raise CompleteError(cell_index, char_index)
# Consume all the iopub messages that the completion produced.
while True:
try:
reply = self.kc.get_iopub_msg(timeout=self.execute_timeout)
except TimeoutError:
# Timeout usually means that the kernel has crashed.
raise CompleteCrash(cell_index, char_index)
if reply['header']['msg_type'] == 'status' and \
reply['content']['execution_state'] == 'idle':
break
def _init_kernel(self):
km, kc = start_new_kernel(kernel_name='swift')
self.km = km
self.kc = kc
# Runs each code cell in order, asking for completions in each cell along
# the way. Raises an exception if there is an error or crash. Otherwise,
# returns.
def _run_notebook_once(self, failed_completions):
for cell_index, cell in enumerate(self.code_cells):
completion_times = []
# Don't do completions when `char_step` is 0.
# Don't do completions when we already have 3 completion failures
# in this cell.
# Otherwise, ask for a completion every `char_step` chars.
if self.char_step > 0 and \
len(failed_completions[cell_index]) < 3:
for char_index in range(0, len(cell.source), self.char_step):
if char_index in failed_completions[cell_index]:
continue
if self.verbose:
print('Cell %d/%d: completing char %d/%d' % (
cell_index, len(self.code_cells), char_index,
len(cell.source)),
end='\r')
start_time = time.time()
self._complete(cell_index, char_index)
completion_times.append(1000 * (time.time() - start_time))
# Execute the cell.
if self.verbose:
print('Cell %d/%d: executing ' % (
cell_index, len(self.code_cells)),
end='\r')
start_time = time.time()
self._execute_cell(cell_index)
execute_time = 1000 * (time.time() - start_time)
# Report the results.
report = 'Cell %d/%d: done' % (cell_index, len(self.code_cells))
report += ' - execute %.0f ms' % execute_time
if len(failed_completions[cell_index]) > 0:
# Don't report completion timings in cells with failed
# completions, because they might be misleading.
report += ' - completion error(s) occurred'
elif len(completion_times) == 0:
report += ' - no completions performed'
else:
report += ' - complete p50 %.0f ms' % (
numpy.percentile(completion_times, 50))
report += ' - complete p90 %.0f ms' % (
numpy.percentile(completion_times, 90))
report += ' - complete p99 %.0f ms' % (
numpy.percentile(completion_times, 99))
if self.verbose:
print(report)
def _record_error(self, e):
cell = self.code_cells[e.cell_index]
if hasattr(e, 'char_index'):
code = cell.source[:e.char_index]
else:
code = cell.source
error_description = {
'error': e,
'code': code,
}
if self.verbose:
print('ERROR!\n%s\n\nCode:\n%s\n' % (e, code))
self.unexpected_errors.append(error_description)
def run(self):
# map from cell index to set of char indexes where completions failed
failed_completions = defaultdict(set)
while True:
self._init_kernel()
try:
for _ in range(self.repeat_times):
self._run_notebook_once(failed_completions)
break
except ExecuteException as ee:
# Execution exceptions can't be recovered, so take note of the
# error and stop the stress test.
self._record_error(ee)
break
except CompleteException as ce:
# Completion exceptions can be recovered! Restart the kernel
# and don't ask for the broken completion next time.
self._record_error(ce)
failed_completions[ce.cell_index].add(ce.char_index)
finally:
self.km.shutdown_kernel(now=True)
def parse_args():
parser = argparse.ArgumentParser(
description='Executes all the cells in a Jupyter notebook, and '
'requests completions along the way. Records and '
'reports errors and kernel crashes that occur.')
parser.add_argument('notebook',
help='path to a notebook to run the test on')
parser.add_argument('--char-step', type=int, default=1,
help='number of chars to step per completion request. '
'0 disables completion requests')
parser.add_argument('--repeat-times', type=int, default=1,
help='run the notebook this many times, in the same '
'kernel instance')
parser.add_argument('--execute-timeout', type=int, default=15,
help='number of seconds to wait for cell execution')
parser.add_argument('--complete-timeout', type=int, default=5,
help='number of seconds to wait for completion')
return parser.parse_args()
def _main():
args = parse_args()
runner = NotebookTestRunner(**args.__dict__)
runner.run()
print(runner.unexpected_errors)
if __name__ == '__main__':
_main()