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 | |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 5 | import logging |
| 6 | import sys |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 7 | import threading |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 8 | import time |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 9 | |
| 10 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 11 | class BaseStressor(threading.Thread): |
| 12 | """ |
| 13 | Implements common functionality for *Stressor classes. |
| 14 | |
| 15 | @var stressor: callable which performs a single stress event. |
| 16 | """ |
Craig Harrison | 9a04f95 | 2012-11-20 15:49:46 -0800 | [diff] [blame] | 17 | def __init__(self, stressor, on_exit=None, escalate_exceptions=True): |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 18 | """ |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 19 | Initialize the ControlledStressor. |
| 20 | |
| 21 | @param stressor: callable which performs a single stress event. |
Craig Harrison | 9a04f95 | 2012-11-20 15:49:46 -0800 | [diff] [blame] | 22 | @param on_exit: callable which will be called when the thread finishes. |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 23 | @param escalate_exceptions: whether to escalate exceptions to the parent |
| 24 | thread; defaults to True. |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 25 | """ |
| 26 | super(BaseStressor, self).__init__() |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 27 | self.daemon = True |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 28 | self.stressor = stressor |
Craig Harrison | 9a04f95 | 2012-11-20 15:49:46 -0800 | [diff] [blame] | 29 | self.on_exit = on_exit |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 30 | self._escalate_exceptions = escalate_exceptions |
| 31 | self._exc_info = None |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def start(self, start_condition=None): |
| 35 | """ |
| 36 | Creates a new thread which will call the run() method. |
| 37 | |
| 38 | Optionally takes a wait condition before the stressor loop. Returns |
| 39 | immediately. |
| 40 | |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 41 | @param start_condition: the new thread will wait until this optional |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 42 | callable returns True before running the stressor. |
| 43 | """ |
| 44 | self._start_condition = start_condition |
| 45 | super(BaseStressor, self).start() |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def run(self): |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 49 | """ |
| 50 | Introduce a delay then start the stressor loop. |
| 51 | |
| 52 | Overloaded from threading.Thread. This is run in a separate thread when |
| 53 | start() is called. |
| 54 | """ |
| 55 | if self._start_condition: |
| 56 | while not self._start_condition(): |
| 57 | time.sleep(1) |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 58 | try: |
| 59 | self._loop_stressor() |
| 60 | except Exception as e: |
| 61 | if self._escalate_exceptions: |
| 62 | self._exc_info = sys.exc_info() |
| 63 | raise |
Craig Harrison | 9a04f95 | 2012-11-20 15:49:46 -0800 | [diff] [blame] | 64 | finally: |
| 65 | if self.on_exit: |
| 66 | self.on_exit() |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 67 | |
| 68 | |
| 69 | def _loop_stressor(self): |
| 70 | """ |
| 71 | Apply stressor in a loop. |
| 72 | |
| 73 | Overloaded by the particular *Stressor. |
| 74 | """ |
| 75 | raise NotImplementedError |
| 76 | |
| 77 | |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 78 | def reraise(self): |
| 79 | """ |
| 80 | Reraise an exception raised in the thread's stress loop. |
| 81 | |
| 82 | This is a No-op if no exception was raised. |
| 83 | """ |
| 84 | if self._exc_info: |
| 85 | exc_info = self._exc_info |
| 86 | self._exc_info = None |
| 87 | raise exc_info[0], exc_info[1], exc_info[2] |
| 88 | |
| 89 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 90 | class ControlledStressor(BaseStressor): |
| 91 | """ |
| 92 | Run a stressor in loop on a separate thread. |
| 93 | |
| 94 | Creates a new thread and calls |stressor| in a loop until stop() is called. |
| 95 | """ |
Craig Harrison | 9a04f95 | 2012-11-20 15:49:46 -0800 | [diff] [blame] | 96 | def __init__(self, stressor, on_exit=None, escalate_exceptions=True): |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 97 | """ |
| 98 | Initialize the ControlledStressor. |
| 99 | |
| 100 | @param stressor: callable which performs a single stress event. |
Craig Harrison | 9a04f95 | 2012-11-20 15:49:46 -0800 | [diff] [blame] | 101 | @param on_exit: callable which will be called when the thread finishes. |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 102 | @param escalate_exceptions: whether to escalate exceptions to the parent |
| 103 | thread; defaults to True. |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 104 | """ |
| 105 | self._complete = threading.Event() |
Craig Harrison | 9a04f95 | 2012-11-20 15:49:46 -0800 | [diff] [blame] | 106 | super(ControlledStressor, self).__init__(stressor, on_exit, |
| 107 | escalate_exceptions) |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 108 | |
| 109 | |
| 110 | def _loop_stressor(self): |
| 111 | """Overloaded from parent.""" |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 112 | iteration_num = 0 |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 113 | while not self._complete.is_set(): |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 114 | iteration_num += 1 |
| 115 | logging.info('Stressor iteration: %d' % iteration_num) |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 116 | self.stressor() |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 117 | |
| 118 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 119 | def start(self, start_condition=None): |
| 120 | """Start applying the stressor. |
| 121 | |
| 122 | Overloaded from parent. |
| 123 | |
| 124 | @param start_condition: the new thread will wait to until this optional |
| 125 | callable returns True before running the stressor. |
| 126 | """ |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 127 | self._complete.clear() |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 128 | super(ControlledStressor, self).start(start_condition) |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 129 | |
| 130 | |
| 131 | def stop(self, timeout=45): |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 132 | """ |
| 133 | Stop applying the stressor. |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 134 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 135 | @param timeout: maximum time to wait for a single run of the stressor to |
| 136 | complete, defaults to 45 seconds. |
| 137 | """ |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 138 | self._complete.set() |
| 139 | self.join(timeout) |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 140 | self.reraise() |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 141 | |
| 142 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 143 | class CountedStressor(BaseStressor): |
| 144 | """ |
| 145 | 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] | 146 | |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 147 | Creates a new thread and calls |stressor| in a loop |iterations| times. The |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 148 | calling thread can use wait() to block until the loop completes. |
| 149 | """ |
| 150 | def _loop_stressor(self): |
| 151 | """Overloaded from parent.""" |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 152 | for iteration_num in xrange(1, self._iterations + 1): |
| 153 | logging.info('Stressor iteration: %d of %d' % (iteration_num, |
| 154 | self._iterations)) |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 155 | self.stressor() |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 156 | |
| 157 | |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 158 | def start(self, iterations, start_condition=None): |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 159 | """ |
| 160 | Apply the stressor a given number of times. |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 161 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 162 | Overloaded from parent. |
| 163 | |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 164 | @param iterations: number of times to apply the stressor. |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 165 | @param start_condition: the new thread will wait to until this optional |
| 166 | callable returns True before running the stressor. |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 167 | """ |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 168 | self._iterations = iterations |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 169 | super(CountedStressor, self).start(start_condition) |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 170 | |
| 171 | |
| 172 | def wait(self, timeout=None): |
| 173 | """Wait until the stressor completes. |
| 174 | |
Craig Harrison | fc459df | 2012-10-04 13:49:48 -0700 | [diff] [blame] | 175 | @param timeout: maximum time for the thread to complete, by default |
| 176 | never times out. |
Craig Harrison | e3ea8f2 | 2012-10-01 13:55:21 -0700 | [diff] [blame] | 177 | """ |
| 178 | self.join(timeout) |
Craig Harrison | 922fcb9 | 2012-10-31 09:06:22 -0700 | [diff] [blame] | 179 | self.reraise() |