-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbleachermark.py
628 lines (517 loc) · 21.6 KB
/
bleachermark.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
r"""
The Bleachermark package
A Bleachermark is a collection of benchmarks. A benchmark is a single pipeline
of functions, often randomized in one or more functions, whose performance and
data one wishes to perform statistics on.
EXAMPLE::
>>> from bleachermark import *
>>> import math, random
>>> def data_gen(i):
... random.seed(i)
... return random.random()
>>> def f1(x):
... return x*x - x
>>> pipeline = [data_gen, f1, math.cos, f1, f1, math.sin]
>>> B = Benchmark(pipeline, label='benchmark 1')
>>> zero_pipeline = [lambda i: i, lambda i: 0]
>>> B2 = Benchmark(zero_pipeline, label='stupid benchmark', fun_labels=['identity', 'zero'])
>>> BB = Bleachermark((B, B2))
>>> BB.run(2)
>>> BB.fetch_data("dict")
{'benchmark 1': [[0, (1.6000000000016e-05, 0.8444218515250481),
(1.0000000000287557e-06, -0.1313735881920577),
(8.000000000008e-06, 0.9913828944313534),
(0.0, -0.008542851060265422),
(0.0, 0.0086158313645033),
(0.0, 0.00861572476904337)],
[1, (1.0999999999983245e-05, 0.13436424411240122),
(1.0000000000287557e-06, -0.11631049401650427),
(0.0, 0.9932435564834237),
(0.0, -0.006710793987583674),
(1.0000000000287557e-06, 0.006755828743527464),
(1.0000000000287557e-06, 0.006755777352931481)]],
'stupid benchmark': [[0, (1.0000000000287557e-06, 0),
(9.999999999177334e-07, 0)],
[1, (0.0, 1), (0.0, 0)]]}
>>> BB.fetch_data("flat")
[('benchmark 1', '0', 0, 1.7000000000044757e-05, 0.8444218515250481),
('benchmark 1', '1', 0, 9.999999999177334e-07, -0.1313735881920577),
('benchmark 1', '2', 0, 6.000000000061512e-06, 0.9913828944313534),
('benchmark 1', '3', 0, 0.0, -0.008542851060265422),
('benchmark 1', '4', 0, 9.999999999177334e-07, 0.0086158313645033),
('benchmark 1', '5', 0, 1.0000000000287557e-06, 0.00861572476904337),
('benchmark 1', '0', 1, 2.8000000000028002e-05, 0.13436424411240122),
('benchmark 1', '1', 1, 1.0000000000287557e-06, -0.11631049401650427),
('benchmark 1', '2', 1, 9.999999999177334e-07, 0.9932435564834237),
('benchmark 1', '3', 1, 1.0000000000287557e-06, -0.006710793987583674),
('benchmark 1', '4', 1, 2.0000000000575113e-06, 0.006755828743527464),
('benchmark 1', '5', 1, 0.0, 0.006755777352931481),
('stupid benchmark', 'identity', 0, 9.999999999177334e-07, 0),
('stupid benchmark', 'zero', 0, 0.0, 0),
('stupid benchmark', 'identity', 1, 0.0, 1),
('stupid benchmark', 'zero', 1, 0.0, 0)]
"""
from time import clock
from copy import copy
#This part handles the ctrl-c interruption.
#class CTRLC(Exception):
#def __init__(self):
#pass
import signal
def signal_ctrl_c(signal, frame):
raise KeyboardInterrupt
signal.signal(signal.SIGINT, signal_ctrl_c)
class Benchmark():
r"""
A Benchmark is a pipeline of functions.
"""
def __init__(self, pipeline, label=None, fun_labels = None):
r"""
Initializes the Benchmark
INPUT:
- ``l`` - the list of functions in the Benchmark.
- ``label`` - the name of the benchmark.
- ``fun_labels`` - the names of the functions. If it is nit given,
they will be named by their index.
"""
if not isinstance(pipeline, (list, tuple)):
raise TypeError("Pipeline must be a list or tuple of functions")
self._pipeline = tuple(pipeline)
self._label = label
if fun_labels is None:
self._fun_labels = tuple(str(i) for i in range(len(self._pipeline)))
else:
if not isinstance(fun_labels, (list, tuple)):
raise TypeError("The labels of the functions must be given in a list or tuple")
if len(fun_labels) != len(self._pipeline):
raise ValueError("There must be as many labels as functions")
self._fun_labels = tuple(fun_labels)
def __repr__(self):
if self.label():
return 'Benchmark {}'.format(self.label())
else:
return 'Benchmark for a pipeline of {} functions'.format(len(self._pipeline))
def label(self):
r"""
Return the label (name) for this benchmark.
"""
return self._label
def _set_label(self, label):
self._label = label
def function_labels(self):
r"""
Return the functions' labels for this benchmark.
"""
return self._fun_labels
def pipeline(self):
r"""
Return the pipeline of functions of this benchmark.
"""
return self._pipeline
def run(self, i):
r"""
Run the pipeline and return the timings and values produced.
INPUT:
- ``i`` - The input fed to the first element of the pipeline. Typically
an identifier of the running instance.
OUTPUT:
- A list whose first element is the input passed to the first function
of the pipeline. The rest of the elements are pairs, with one pair for
each part of the pipeline. The first element of each pair is the time
that this part of the pipeline took, and the second is the value it output.
"""
time_vals = [i]
intervalue = i
for fun in self._pipeline:
tim = clock()
intervalue = fun(intervalue)
time_vals.append( (clock()-tim, intervalue) )
return time_vals
def _make_autolabel(n):
r"""
Return a generic label for a benchmark with count n
"""
return "[%s]" % n
_autolabel_regex = r"\[[0-9]*\]"
class Bleachermark:
def __init__(self, benchmarks):
r"""
INPUT:
- ``benchmarks`` - A benchmark, or a list or tuple of them
"""
if isinstance(benchmarks, Benchmark):
self._benchmarks = (benchmarks, )
elif isinstance(benchmarks, (list, tuple)) and all([isinstance(i, Benchmark) for i in benchmarks]):
self._benchmarks = tuple(benchmarks)
elif isinstance(benchmarks, (list, tuple)) and all([isinstance(i, (list, tuple)) for i in benchmarks]):
self._benchmarks = tuple(Benchmark(i) for i in benchmarks)
else:
self._benchmarks = (Benchmark(benchmarks),)
for n, b in zip(range(self.size()), self._benchmarks):
if b.label() is None:
b._set_label(_make_autolabel(n)) # WARNING!!! This could have side effects if the same benchmark is in more than one bleachermark!!!
# benchmark label -> ((timing, value) list) list
# (time, value) = self._measurements[bm.label()][run_no][pipeline_part]
self.clear()
self._current_runner = None
def __repr__(self):
return 'Collection of {} benchmarks'.format(self.size())
def size(self):
return len(self._benchmarks)
def __iter__(self): # Implement iterator behaviour
return self
def next(self): # Should we store the result?
return self._current_runner.next()
__next__ = next
def set_runner(self, runner, *args, **kwargs):
r"""
Set the runner to be used when the bleachermark is used as an iterator:
INPUT:
- ``runner`` - the constructor of the runner to be set.
- ``*args`` - the arguments to be passed to the constructor of the runner.
- ``**kwargs`` - the keyword arguments to be passed to the constructor of the runner.
EXAMPLES::
>>> from bleachermark import *
>>> import math, random
>>> def data_gen(i):
... random.seed(i)
... return random.random()
>>> def f1(x):
... return x*x - x
>>> pipeline = [data_gen, f1, math.cos]
>>> B = Benchmark(pipeline, label='benchmark 1')
>>> zero_pipeline = [lambda i: i, lambda i: 0]
>>> B2 = Benchmark(zero_pipeline, label='stupid benchmark', fun_labels=['identity', 'zero'])
>>> BB = Bleachermark((B, B2))
>>> BB.set_runner(SerialRunner, 100)
>>> BB.next()
(0, (0, (2.2999999999884224e-05, 0.8444218515250481),
(2.0000000000575113e-06, -0.1313735881920577),
(2.9999999999752447e-06, 0.9913828944313534)))
>>> BB.next()
(1, (0, (3.999999999892978e-06, 0), (2.9999999999752447e-06, 0)))
This way, the bleachermark can be used as part of a bigger pipeline (for instance,
feeding output to another engine that makes statistical analyisis, or plots.
"""
runner = runner(self, *args, **kwargs)
self._current_runner = runner
def run(self, nruns = 100): # Refactored to the runnners model
# This function should create a runner according to the passed parameters and run it
# For the moment it just uses the serial runner with the parameter nruns
# No automatic tweaking, no parallelism, no nothing
r"""
Runs the benchmarks in the collection and stores the returned data.
TODO: Alias of `resume`?
INPUT:
- ``nruns`` - The number of times each
"""
if self._current_runner is None:
runner = SerialRunner(self, nruns)
self._current_runner = runner
self.resume()
def resume(self):
r"""
Resumes the run of the current runner and stores the measurements produced.
"""
labels = [ l if l is not None else str(i) for (i,l) in
zip(range(self.size()),[ b.label() for b in self._benchmarks]) ]
measurements = self._measurements
while True:
try:
r = self._current_runner.next()
label = labels[r[0]]
measurements[label].append(r[1])
except (StopIteration, KeyboardInterrupt):
return
def clear(self):
r"""
Forget all measurements.
"""
self._measurements = { b.label(): [] for b in self._benchmarks } # benchmark label -> ((timing, value) list) list
def fetch_data(self, format="dict"):
r"""
Return all the measured data.
INPUT:
- ``format`` - (optional, default: "dict") specify the format to return
the data in. If set to "dict", return a dictionary from Benchmark
label to list of measurements, each measurement being as the output of
``Benchmark.run``. If set to "flat", return a list of tuples of the
format ``(benchmark label, run-no, pipeline-part, timing, output)``.
"""
measurements = self._measurements
if format == "dict":
#TODO: Really copy?
return copy(measurements)
elif format == "flat":
data = []
for benchmark in self._benchmarks:
label = benchmark.label()
fun_labels = benchmark.function_labels()
for run in measurements[label]:
for i in range(len(benchmark.pipeline())):
m = run[i+1]
data.append( (label, fun_labels[i], run[0], m[0], m[1]) )
return data
else:
raise ValueError("Invalid argument to format: %s".format(format))
def timings(self, transposed=False):
r"""
Return all measured timings.
INPUT:
- ``transposed`` - (default = False), determines wether the data must
be transposed. TODO: Explain what this means.
OUTPUT:
- a dictionary whose keys are the labels of the benchmarks.
The value for each benchmark is a list corresponding to the runs.
For each run, there is a list of the timings of the different
components of the pipeline.
"""
di = self.fetch_data()
res = {bm:[[t[0] for t in run[1:]] for run in di[bm]] for bm in di.keys()}
if not transposed:
return res
return {bm:[[row[i] for row in res[bm]] for i in range(len(res[bm][0]))] for bm in res.keys()}
def averages(self):
r"""
Return the averages of the timings.
OUTPUT:
- A dictionary whose keys are the benchmarks. The value for each benchmark
is a list with the averages of the corresponding parts of the pipeline.
"""
timings = self.timings(transposed=True)
res = {}
for bm in timings.keys():
totals = map(lambda a: sum(a)/len(a), timings[bm])
res[bm] = totals
return res
def variances(self):
r"""
Return the variances of the timings of the benchmarks
"""
timings = self.timings(transposed=True)
averages = self.averages()
res = {}
for bm in timings.keys():
timbm = timings[bm]
avgbm = averages[bm]
lbm = []
for (timpart, avgpart) in zip(timbm, avgbm):
deviations = [(l - avgpart)**2 for l in timpart]
lbm.append(sum(deviations)/len(deviations))
res[bm] = lbm
return res
def stdvs(self):
r"""
Return the standard deviations of the timings.
TODO: Better name?
"""
variances = self.variances()
import math
return {bm:map(math.sqrt, variances[bm]) for bm in variances}
def maxes(self):
r"""
Return the maximum running times of the benchmarks run.
"""
timings = self.timings(transposed=True)
return {bm:map(max, timings[bm]) for bm in timings}
def mins(self):
r"""
Return the minimum running times of the benchmarks run.
"""
timings = self.timings(transposed=True)
return {bm:map(min, timings[bm]) for bm in timings}
def pipeline_data(self):
r"""
Get the data through the pipeline of all benchmarks and runs.
"""
raise NotImplementedError
def __add__(self, other):
r"""
Add two Bleachermarks, concatenating their benchmarks.
Note that this does not lose existing measurement data.
"""
raise NotImplementedError("Not verified since Runner system")
import re
my_labels = set( b.label() for b in self._benchmarks )
ot_labels = set( b.label() for b in other._benchmarks )
collisions = my_labels.intersect(ot_labels)
if collisions:
autolabel = re.compile(_autolabel_regex)
counter = self.size()
for label in collisions:
if autolabel.match(label):
#Change name of other's benchmark
other.benchmark(label)._set_label(_make_autolabel(counter))
counter += 1
else:
raise ValueError("Collision on label %s" % label)
#Now benchmarks can just be concatenated
self._benchmarks.extend(other._benchmarks)
for b in other._benchmarks:
assert not b.label() in self._measurements
self._measurements[b.label()] = copy(other._measurements[b.label()]) #TODO: deepcopy?
return self
#RUNNERS
# Runners are essentially iterators that produce the data that the bleachermark will store.
#They should support the following interface:
# They are created by passing the bleachermark that created them, and the specific parameters of the runner
# They act as iterators that yield the results of the benchmarks.
# - The format of the return is a tuple (index, results), where
# - index is the index of the benchmark
# - results is the result of running the benchmark
# It is the runners work to decide how to order the benchmarks, call them in parallel and so on
class SerialRunner:
r"""
Example of Runner. It just runs the each benchmark of the bleachermark as many times as indicated.
It most likely will be the default one
"""
def __init__(self, bleachermark, iterations):
r"""
INPUT:
- ``bleachermark`` - The bleachermark that calls it.
- ``iterations`` - The number of
"""
self._bleachermark = bleachermark
self._niter = iterations
self._benchmarks = bleachermark._benchmarks
self._current_iter = 0
self._current_benchmark = 0
def __iter__(self):
return self
def __repr__(self):
return 'Serial Runner of {} instances for {}'.format(self._niter, self._bleachermark)
def next(self):
if self._current_iter >= self._niter:
raise StopIteration()
else:
runid = self._current_iter
benchmarkid = self._current_benchmark
res = (benchmarkid, self._benchmarks[benchmarkid].run(runid))
if self._current_benchmark == len(self._benchmarks) - 1:
self._current_benchmark = 0
self._current_iter += 1
else:
self._current_benchmark += 1
return res
__next__ = next
class ListRunner:
r"""
Runner based on a list. You just pass a list of the runid's you want to pass
to your benchmarks, and it takes care of it.
EXAMPLE::
"""
def __init__(self, bleachermark, runids):
r"""
INPUT:
- ``bleachermark`` - the bleachermark that calls this runner
- ``runids`` - a list, tuple or other iterable with the runids to
be passed to the benchmarks.
"""
self._bleachermark = bleachermark
self._idqueue = runids.__iter__()
self._benchmarks = bleachermark._benchmarks
self._nbench = len(self._benchmarks) - 1
self._currentid = None
def __iter__(self):
return self
def __repr__(self):
return 'List Runner for {}'.format(self._bleachermark)
def next(self):
if self._nbench == len(self._benchmarks) - 1:
self._nbench = 0
self._currentid = self._idqueue.next()
else:
self._nbench += 1
return (self._nbench, self._benchmarks[self._nbench].run(self._currentid))
__next__ = next
class ParallelRunner:
r"""
This runner uses sage parallel utility.
It profiles each benchmark and decides how many runs can fit in a chunk of about two
seconds. Then it computes these chunks in parallel.
As input, it takes a list or tuple of the inputs that will be given to the benchmarks.
"""
def __init__(self, bleachermark, runs):
from sage.parallel.decorate import parallel
from sage.functions.other import ceil, floor
self._benchmarks = bleachermark._benchmarks
# profiling we run each benchmark once
self._totaltime = reduce(lambda a,b: a+b, [r[0] for bm in self._benchmarks for r in bm.run(runs[0])[1:]])
#divide the runs in chunks
self._chunksize = ceil(2.0 / self._totaltime)
self._nchunks = floor(len(runs)/self._chunksize)
self._chunks = [runs[i*self._chunksize:(i+1)*self._chunksize] for i in range(self._nchunks)]
if (self._nchunks)*self._chunksize < len(runs):
self._chunks.append(runs[(self._nchunks)*self._chunksize:])
# we define the parallel function
@parallel
def f(indices):
results = []
for frun in indices:
for i in range(len(self._benchmarks)):
bm = self._benchmarks[i]
res = bm.run(frun)
results.append((i, res))
return results
self._getchunks = f(self._chunks)
self._currentchunk = []
def __iter__(self):
return self
def next(self):
if not self._currentchunk:
self._currentchunk = self._getchunks.next()[1]
res = self._currentchunk.pop()
return res
__next__ = next
class AdaptativeRunner:
r"""
Runner that decides the runs to make adaptitvely to produce
the best possible overview of how does the timing of each benchmark
deppend on its input.
It is assumed that the runid will correspond to the size of the input
For the moment, the strategy followed is the following: try the measures to
expand aa range as big as possible, but making sure that two consecutive runid's
have timings that don't differ by a factor bigger than 1.5
(unless they are consecutive integers).
"""
def __init__(self, bleachermark, totruns = 100):
self._benchmarks=bleachermark._benchmarks
self._timings = {i:[] for i in range(len(self._benchmarks))}
self._curbm = 0
self._pendingruns = totruns
def repr(self):
return "Adaptative Runner for {} benchmarks".format(len(self._benchmarks))
def __iter__(self):
return self
def next(self):
if self._pendingruns < 0:
raise StopIteration
self._pendingruns -= 1
i = self._curbm
# this part decides j, which is the next runid to run
if not self._timings[i]:
j = 1
ii = 0
elif len(self._timings[i]) == 1: # we need at least values for 1 and 2
j = 2
ii = 1
else: #now we check if two consecutive runids have enough space between
for ii in range(len(self._timings[i])):
if ii == len(self._timings[i])-1:
j = 2 * self._timings[i][-1][0]
else:
i0, t0 = self._timings[i][ii]
i1, t1 = self._timings[i][ii+1]
if (i1 > i0+1) and (t1/t0 > 1.5 or t0/t1 > 1.5):
j = int((i0 + i1)/2)
break
# now we run, store and return it
res = self._benchmarks[i].run(j)
tottime = sum([r[0] for r in res[1:]])
self._timings[i].insert(ii+1, (j, tottime))
self._curbm = (i+1) % len(self._benchmarks)
return (i, res)
__next__ = next
if __name__ == "__main__":
import doctest
doctest.testmod()