Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import threading |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 6 | import time |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 7 | |
| 8 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 9 | class BaseStressor(threading.Thread): |
| 10 | """ |
| 11 | Implements common functionality for *Stressor classes. |
| 12 | |
| 13 | @var stressor: callable which performs a single stress event. |
| 14 | """ |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 15 | def __init__(self, stressor): |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 16 | """ |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 17 | Initialize the ControlledStressor. |
| 18 | |
| 19 | @param stressor: callable which performs a single stress event. |
| 20 | """ |
| 21 | super(BaseStressor, self).__init__() |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 22 | self.daemon = True |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 23 | self.stressor = stressor |
| 24 | |
| 25 | |
| 26 | def start(self, start_condition=None): |
| 27 | """ |
| 28 | Creates a new thread which will call the run() method. |
| 29 | |
| 30 | Optionally takes a wait condition before the stressor loop. Returns |
| 31 | immediately. |
| 32 | |
| 33 | @param start_condition: the new thread will wait to until this optional |
| 34 | callable returns True before running the stressor. |
| 35 | """ |
| 36 | self._start_condition = start_condition |
| 37 | super(BaseStressor, self).start() |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def run(self): |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 41 | """ |
| 42 | Introduce a delay then start the stressor loop. |
| 43 | |
| 44 | Overloaded from threading.Thread. This is run in a separate thread when |
| 45 | start() is called. |
| 46 | """ |
| 47 | if self._start_condition: |
| 48 | while not self._start_condition(): |
| 49 | time.sleep(1) |
| 50 | self._loop_stressor() |
| 51 | |
| 52 | |
| 53 | def _loop_stressor(self): |
| 54 | """ |
| 55 | Apply stressor in a loop. |
| 56 | |
| 57 | Overloaded by the particular *Stressor. |
| 58 | """ |
| 59 | raise NotImplementedError |
| 60 | |
| 61 | |
| 62 | class ControlledStressor(BaseStressor): |
| 63 | """ |
| 64 | Run a stressor in loop on a separate thread. |
| 65 | |
| 66 | Creates a new thread and calls |stressor| in a loop until stop() is called. |
| 67 | """ |
| 68 | def __init__(self, stressor): |
| 69 | """ |
| 70 | Initialize the ControlledStressor. |
| 71 | |
| 72 | @param stressor: callable which performs a single stress event. |
| 73 | """ |
| 74 | self._complete = threading.Event() |
| 75 | super(ControlledStressor, self).__init__(stressor) |
| 76 | |
| 77 | |
| 78 | def _loop_stressor(self): |
| 79 | """Overloaded from parent.""" |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 80 | while not self._complete.is_set(): |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 81 | self.stressor() |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 82 | |
| 83 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 84 | def start(self, start_condition=None): |
| 85 | """Start applying the stressor. |
| 86 | |
| 87 | Overloaded from parent. |
| 88 | |
| 89 | @param start_condition: the new thread will wait to until this optional |
| 90 | callable returns True before running the stressor. |
| 91 | """ |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 92 | self._complete.clear() |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 93 | super(ControlledStressor, self).start(start_condition) |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def stop(self, timeout=45): |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 97 | """ |
| 98 | Stop applying the stressor. |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 99 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 100 | @param timeout: maximum time to wait for a single run of the stressor to |
| 101 | complete, defaults to 45 seconds. |
| 102 | """ |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 103 | self._complete.set() |
| 104 | self.join(timeout) |
| 105 | |
| 106 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 107 | class CountedStressor(BaseStressor): |
| 108 | """ |
| 109 | Run a stressor in a loop on a separate thread a given number of times. |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 110 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 111 | Creates a new thread and calls |stressor| in a loop |count| times. The |
| 112 | calling thread can use wait() to block until the loop completes. |
| 113 | """ |
| 114 | def _loop_stressor(self): |
| 115 | """Overloaded from parent.""" |
Craig Harrison | 5cb9121 | 2012-10-22 18:01:48 -0700 | [diff] [blame^] | 116 | for _ in xrange(self._count): |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 117 | self.stressor() |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 118 | |
| 119 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 120 | def start(self, count, start_condition=None): |
| 121 | """ |
| 122 | Apply the stressor a given number of times. |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 123 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 124 | Overloaded from parent. |
| 125 | |
| 126 | @param count: number of times to apply the stressor. |
| 127 | @param start_condition: the new thread will wait to until this optional |
| 128 | callable returns True before running the stressor. |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 129 | """ |
| 130 | self._count = count |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 131 | super(CountedStressor, self).start(start_condition) |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def wait(self, timeout=None): |
| 135 | """Wait until the stressor completes. |
| 136 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 137 | @param timeout: maximum time for the thread to complete, by default |
| 138 | never times out. |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 139 | """ |
| 140 | self.join(timeout) |