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
|
= Tunable Loops =
Some large jobs have to be done in smaller chunks, e.g. because you want to
report progress to a user, or you're holding locks that others may be waiting
for. But you can't always afford to stop and look at your watch every
iteration of your loop: sometimes you need to decide how big your chunk will
be before you start processing it.
One way to solve this is to measure performance of your loop, and pick a chunk
size that will give you decent performance while also stopping at more or less
regular intervals to update your progress display, refresh your locks, or
whatever else you want to do. But what if the Moon is in the House of Uranus
and the server slows down? Conversely, you may be wasting time if you stop too
often.
The LoopTuner solves this. You tell it what you want to do and how long each
chunk should take, and it will figure out how many items you need to process
per chunk to get close to your ideal time between stops. Chunk sizes will
adjust themselves dynamically to actual performance.
>>> import math
>>> from zope.interface import implements
>>> from lp.services.looptuner import ITunableLoop
>>> from lp.services.looptuner import LoopTuner
The LoopTuner requires the operation you define for it to implement the
ITunableLoop interface.
>>> class NotATunableLoop:
... def isDone(self):
... return True
... def __call__(self, chunk_size):
... "This never gets called"
>>> LoopTuner(NotATunableLoop(), 1, 10)
Traceback (most recent call last):
...
AssertionError
== Chunk Size Convergence ==
Here's a very simple ITunableLoop. It receives a list of timings to simulate
for its iterations (it finishes when the list runs out), and for every
iteration, prints out what happened to its chunk size parameter.
The printout includes a logarithmic order-of-magnitude indication, where "no
change" is zero, decreases are negative numbers, and increases show up as
positive numbers. The expectations for these numbers are based on
observation, and could in principle still vary a bit with different CPU
architectures, compilers used for the Python interpreter, etc.
>>> goal_seconds = 10.0
>>> def print_change(last_chunk_size, new_chunk_size):
... if last_chunk_size is None:
... print "start"
... return
... change = "same"
... if new_chunk_size > last_chunk_size:
... change = "increased"
... elif new_chunk_size < last_chunk_size:
... change = "decreased"
... ratio = new_chunk_size / last_chunk_size
... order_of_magnitude = math.log10(ratio)
... print "%s (%.1f)" % (change, order_of_magnitude)
>>> class PlannedLoop:
... implements(ITunableLoop)
... def __init__(self, timings):
... self.last_chunk_size = None
... self.iteration = 0
... self.timings = timings
... self.clock = 0
...
... def isDone(self):
... done = self.iteration >= len(self.timings)
... if done:
... print "done"
... return done
...
... def __call__(self, chunk_size):
... print_change(self.last_chunk_size, chunk_size)
... self.last_chunk_size = chunk_size
... self.clock += self.timings[self.iteration]
... self.iteration += 1
In combination with that, we tweak LoopTuner to simulate the timings we gave
the PlannedLoop. This is for testing only; in normal use you wouldn't need to
subclass the LoopTuner.
>>> class TestTuner(LoopTuner):
... def _time(self):
... return float(self.operation.clock)
=== Trivial Case ===
If there's nothing to do, nothing happens.
>>> body = PlannedLoop([])
>>> loop = TestTuner(body, goal_seconds, 100)
>>> loop.run()
done
=== Ideal Case ===
A typical run using the ITunableLoop follows this pattern: the LoopTuner very
conservatively starts out with the minimum chunk size, finds that its first
iteration finishes well within its time goal, and starts jacking up the work
per iteration until it nears the ideal time per iteration. Due to practical
variations, it keeps oscillating a bit.
>>> body = PlannedLoop([5, 7, 8, 9, 10, 11, 10, 9, 10, 9, 10, 11, 10])
>>> loop = TestTuner(body, goal_seconds, 100)
>>> loop.run()
start
increased (0.2)
increased (0.1)
increased (0.1)
increased (0.0)
same (0.0)
decreased (-0.0)
same (0.0)
increased (0.0)
same (0.0)
increased (0.0)
same (0.0)
decreased (-0.0)
done
=== Slow Run ===
If our iterations consistently exceed their time goal, we stay stuck at the
minimum chunk size.
>>> body = PlannedLoop([15, 11, 16, 20, 14, 15, 10, 12, 15])
>>> loop = TestTuner(body, goal_seconds, 100)
>>> loop.run()
start
same (0.0)
same (0.0)
same (0.0)
same (0.0)
same (0.0)
same (0.0)
same (0.0)
same (0.0)
done
=== Typical Run ===
What happens usually is that performance is relatively stable, so chunk size
converges to a steady state, but there are occasional spikes. When one chunk
is suddenly very slow, the algorithm compensates for that so that if the drop
in performance was a fluke, the next chunk falls well short of its time goal.
>>> body = PlannedLoop([5, 7, 8, 9, 10, 11, 9, 20, 7, 10, 10])
>>> loop = TestTuner(body, goal_seconds, 100)
>>> loop.run()
start
increased (0.2)
increased (0.1)
increased (0.1)
increased (0.0)
same (0.0)
decreased (-0.0)
increased (0.0)
decreased (-0.1)
increased (0.1)
same (0.0)
done
== Cost Functions ==
It's up to the ITunableLoop to define what "chunk size" really means. It's an
arbitrary unit of work. The only requirement for LoopTuner to do useful work
is that on the whole, performance should tend to increase with chunk size.
Here we illustrate how a tuned loop behaves with different cost functions
governing the relationship between chunk size and chunk processing time.
This variant of the LoopTuner simulates an overridable cost function:
>>> class CostedLoop:
... implements(ITunableLoop)
... def __init__(self, cost_function, counter):
... self.last_chunk_size = None
... self.iteration = 0
... self.cost_function = cost_function
... self.counter = counter
... self.clock = 0
...
... def isDone(self):
... done = (self.iteration >= self.counter)
... if done:
... print "done"
... return done
...
... def __call__(self, chunk_size):
... print_change(self.last_chunk_size, chunk_size)
... self.last_chunk_size = chunk_size
... self.iteration += 1
...
... def computeCost(self):
... return self.cost_function(self.last_chunk_size)
>>> class CostedTuner(LoopTuner):
... def __init__(self, *argl, **argv):
... self.clock = 0
... LoopTuner.__init__(self, *argl, **argv)
...
... def _time(self):
... if self.operation.last_chunk_size is not None:
... self.clock += self.operation.computeCost()
... return self.clock
Below we'll see how the loop tuner adapts to various cost functions.
=== Constant Cost ===
We've already seen a constant-time loop body where every iteration took too
much time, and we got stuck on the minimum chunk size. Now we look at the
converse case.
If iterations consistently take less than the ideal time, the algorithm will
"push the boundary," jacking up the workload until it manages to fill up the
per-iteration goal time. This is good if, for instance, the cost function is
very flat, increasing very little with chunk size but with a relatively large
constant overhead. In that case, doing more work per iteration means more
work done for every time we pay that constant overhead. And usually, fewer
iterations overall.
Another case where chunk size may keep increasing is where chunk size turns
out not to affect performance at all. Chunk size is capped to stop it from
spinning into infinity in that case, or if for some reason execution time
should turn out to vary inversely with chunk size.
>>> body = CostedLoop((lambda c: goal_seconds/2), 20)
>>> loop = CostedTuner(body, goal_seconds, 100, 1000)
>>> loop.run()
start
increased (0.2)
increased (0.2)
increased (0.2)
...
same (0.0)
same (0.0)
same (0.0)
done
=== Linear Cost ===
The model behind LoopTuner assumes that the cost of an iteration will tend to
increase as a linear function of chunk size. Constant cost is a degenerate
case of that; here we look at more meaningful linear functions.
==== Without Constant ====
If cost function is purely linear with zero overhead, we approach our time
goal asymptotically. In principle we never quite get there.
>>> body = CostedLoop((lambda c: c/20), 10)
>>> loop = CostedTuner(body, goal_seconds, 100)
>>> loop.run()
start
increased (0.2)
increased (0.1)
increased (0.0)
...
increased (0.0)
increased (0.0)
done
>>> body.computeCost() < goal_seconds
True
>>> body.computeCost() > goal_seconds*0.9
True
==== With Constant ====
Here's a variant with a relatively flat linear cost function (25 units of work
per second), plus a large constant overhead of half the time goal. It does
not achieve equilibrium in 10 iterations:
>>> body = CostedLoop((lambda c: goal_seconds/2+c/25), 10)
>>> loop = CostedTuner(body, goal_seconds, 100)
>>> loop.run()
start
increased (0.0)
increased (0.0)
increased (0.0)
...
increased (0.0)
done
>>> body.computeCost() < goal_seconds
True
But once again it does get pretty close:
>>> body.computeCost() > goal_seconds*0.9
True
=== Exponential Cost ===
What if the relationship between chunk size and iteration time is much more
radical?
==== Low Exponent ====
Due to the way LoopTuner's approximation function works, an exponential cost
function will cause some oscillation where iteration time overshoots the goal,
compensates, then finally converges towards it.
If the cost function is highly regular and predictable, the oscillation will
be a neat alternation of oversized and undersized chunks.
We use math.pow here, since __builtin__.pow is overridden by twisted.conch.
>>> body = CostedLoop(lambda c: math.pow(1.2, c), 50)
>>> loop = CostedTuner(body, goal_seconds, 1)
>>> loop.run()
start
increased (0.7)
increased (0.4)
...
decreased (-0.0)
increased (0.0)
decreased (-0.0)
increased (0.0)
...
same (0.0)
...
same (0.0)
...
done
==== High Exponent ====
With more extreme exponential behaviour, the overshoot increases but the
effect remains the same:
We use math.pow here, since __builtin__.pow is overridden by twisted.conch.
>>> body = CostedLoop(lambda c: math.pow(3, c), 50)
>>> loop = CostedTuner(body, goal_seconds, 1)
>>> loop.run()
start
increased (0.3)
...
decreased (-0.0)
increased (0.0)
decreased (-0.0)
increased (0.0)
...
same (0.0)
...
same (0.0)
...
done
Most practical algorithms will be closer to the linear cost function than they
are to the exponential one.
== Loop cooldown ==
LoopTuner allows inserting a delay between two consecutive operation runs.
Overriding _coolDown method can be used to avoid an actual cooldown,
but still print out what would happen.
>>> class CooldownTuner(LoopTuner):
... def _coolDown(self, bedtime):
... if self.cooldown_time is None or self.cooldown_time <= 0.0:
... print "No cooldown"
... else:
... print "Cooldown for %.1f seconds." % self.cooldown_time
... return bedtime
SimpleLoop is a loop that does a constant number of iterations, regardless
of the actual run-time.
>>> class SimpleLoop:
... implements(ITunableLoop)
... def __init__(self, iterations):
... self.total_iterations = iterations
... self.iteration = 0
... self.clock = 0
...
... def isDone(self):
... done = (self.iteration >= self.total_iterations)
... if done:
... print "done"
... return done
...
... def __call__(self, chunk_size):
... print "Processing %d items." % (chunk_size)
... self.iteration += 1
Aim for a low goal_seconds (to reduce test runtime), and only 3 iterations.
>>> goal_seconds = 0.01
>>> body = SimpleLoop(3)
>>> loop = CooldownTuner(body, goal_seconds, 1, cooldown_time=0.1)
>>> loop.run()
Processing...
Cooldown for 0.1 seconds.
Processing...
Cooldown for 0.1 seconds.
Processing...
Cooldown for 0.1 seconds.
done
=== Cooldown bedtime ===
A private _coolDown method on LoopTuner sleeps for cooldown_time, and
returns time after sleep is done.
>>> import time
>>> cooldown_loop = LoopTuner(body, goal_seconds, cooldown_time=0.2)
>>> old_time = time.time()
>>> new_time = cooldown_loop._coolDown(old_time)
>>> print new_time > old_time
True
If no cooldown_time is specified, there's no sleep, and exactly the same
time is returned.
>>> no_cooldown_loop = LoopTuner(body, goal_seconds, cooldown_time=None)
>>> old_time = time.time()
>>> new_time = no_cooldown_loop._coolDown(old_time)
>>> print new_time == old_time
True
== Abort Timeout ==
LoopTuner allows a timeout to be specified. If the loop runs for longer
than this timeout, it is aborted and a WARNING logged.
>>> body = PlannedLoop([5, 7, 8, 9, 10, 11, 9, 20, 7, 10, 10])
>>> loop = TestTuner(body, goal_seconds, 100, abort_time=20)
>>> loop.run()
start
same (0.0)
same (0.0)
WARNING:...:Task aborted after 20 seconds.
== Cleanup ==
Loops can define a clean up hook to clean up opened resources.
We need this because loops can be aborted mid run, so we cannot rely
on clean up code in the isDone() method, and __del__ is fragile and
can never be relied on.
>>> class PlannedLoopWithCleanup(PlannedLoop):
... def cleanUp(self):
... print 'clean up'
>>> body = PlannedLoopWithCleanup([])
>>> loop = TestTuner(body, goal_seconds, 100)
>>> loop.run()
done
clean up
>>> body = PlannedLoopWithCleanup([5, 7, 8, 9, 10, 11, 9, 20, 7, 10, 10])
>>> loop = TestTuner(body, goal_seconds, 100, abort_time=20)
>>> loop.run()
start
same (0.0)
same (0.0)
WARNING:...:Task aborted after 20 seconds.
clean up
|