Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 7 | #include <config.h> |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 8 | #include <concurrency/ThreadManager.h> |
| 9 | #include <concurrency/PosixThreadFactory.h> |
| 10 | #include <concurrency/Monitor.h> |
| 11 | #include <concurrency/Util.h> |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | #include <set> |
| 15 | #include <iostream> |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 16 | #include <set> |
| 17 | #include <stdint.h> |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 18 | |
| 19 | namespace facebook { namespace thrift { namespace concurrency { namespace test { |
| 20 | |
| 21 | using namespace facebook::thrift::concurrency; |
| 22 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 23 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 24 | * ThreadManagerTests class |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 25 | * |
| 26 | * @author marc |
| 27 | * @version $Id:$ |
| 28 | */ |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 29 | class ThreadManagerTests { |
| 30 | |
| 31 | public: |
| 32 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 33 | static const double ERROR; |
| 34 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 35 | class Task: public Runnable { |
| 36 | |
| 37 | public: |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 38 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 39 | Task(Monitor& monitor, size_t& count, int64_t timeout) : |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 40 | _monitor(monitor), |
| 41 | _count(count), |
| 42 | _timeout(timeout), |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 43 | _done(false) {} |
| 44 | |
| 45 | void run() { |
| 46 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 47 | _startTime = Util::currentTime(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 48 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 49 | { |
| 50 | Synchronized s(_sleep); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 51 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 52 | try { |
| 53 | _sleep.wait(_timeout); |
| 54 | } catch(TimedOutException& e) { |
| 55 | ; |
| 56 | }catch(...) { |
| 57 | assert(0); |
| 58 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 59 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 60 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 61 | _endTime = Util::currentTime(); |
| 62 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 63 | _done = true; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 64 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 65 | { |
| 66 | Synchronized s(_monitor); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 67 | |
| 68 | // std::cout << "Thread " << _count << " completed " << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 69 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 70 | _count--; |
| 71 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 72 | if (_count == 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 73 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 74 | _monitor.notify(); |
| 75 | } |
| 76 | } |
| 77 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 78 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 79 | Monitor& _monitor; |
| 80 | size_t& _count; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 81 | int64_t _timeout; |
| 82 | int64_t _startTime; |
| 83 | int64_t _endTime; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 84 | bool _done; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 85 | Monitor _sleep; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Dispatch count tasks, each of which blocks for timeout milliseconds then |
| 90 | * completes. Verify that all tasks completed and that thread manager cleans |
| 91 | * up properly on delete. |
| 92 | */ |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 93 | bool loadTest(size_t count=100, int64_t timeout=100LL, size_t workerCount=4) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 94 | |
| 95 | Monitor monitor; |
| 96 | |
| 97 | size_t activeCount = count; |
| 98 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 99 | shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 100 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 101 | shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 102 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 103 | threadFactory->setPriority(PosixThreadFactory::HIGHEST); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 104 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 105 | threadManager->threadFactory(threadFactory); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 106 | |
| 107 | threadManager->start(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 108 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 109 | std::set<shared_ptr<ThreadManagerTests::Task> > tasks; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 110 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 111 | for (size_t ix = 0; ix < count; ix++) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 112 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 113 | tasks.insert(shared_ptr<ThreadManagerTests::Task>(new ThreadManagerTests::Task(monitor, activeCount, timeout))); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 116 | int64_t time00 = Util::currentTime(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 117 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 118 | for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 119 | |
| 120 | threadManager->add(*ix); |
| 121 | } |
| 122 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 123 | { |
| 124 | Synchronized s(monitor); |
| 125 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 126 | while(activeCount > 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 127 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 128 | monitor.wait(); |
| 129 | } |
| 130 | } |
| 131 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 132 | int64_t time01 = Util::currentTime(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 133 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 134 | int64_t firstTime = 9223372036854775807LL; |
| 135 | int64_t lastTime = 0; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 136 | |
| 137 | double averageTime = 0; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 138 | int64_t minTime = 9223372036854775807LL; |
| 139 | int64_t maxTime = 0; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 140 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 141 | for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 142 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 143 | shared_ptr<ThreadManagerTests::Task> task = *ix; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 144 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 145 | int64_t delta = task->_endTime - task->_startTime; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 146 | |
| 147 | assert(delta > 0); |
| 148 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 149 | if (task->_startTime < firstTime) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 150 | firstTime = task->_startTime; |
| 151 | } |
| 152 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 153 | if (task->_endTime > lastTime) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 154 | lastTime = task->_endTime; |
| 155 | } |
| 156 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 157 | if (delta < minTime) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 158 | minTime = delta; |
| 159 | } |
| 160 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 161 | if (delta > maxTime) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 162 | maxTime = delta; |
| 163 | } |
| 164 | |
| 165 | averageTime+= delta; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 166 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 167 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 168 | averageTime /= count; |
| 169 | |
| 170 | std::cout << "\t\t\tfirst start: " << firstTime << "ms Last end: " << lastTime << "ms min: " << minTime << "ms max: " << maxTime << "ms average: " << averageTime << "ms" << std::endl; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 171 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 172 | double expectedTime = ((count + (workerCount - 1)) / workerCount) * timeout; |
| 173 | |
| 174 | double error = ((time01 - time00) - expectedTime) / expectedTime; |
| 175 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 176 | if (error < 0) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 177 | error*= -1.0; |
| 178 | } |
| 179 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 180 | bool success = error < ERROR; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 181 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 182 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "! expected time: " << expectedTime << "ms elapsed time: "<< time01 - time00 << "ms error%: " << error * 100.0 << std::endl; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 183 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 184 | return success; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 185 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 186 | |
| 187 | class BlockTask: public Runnable { |
| 188 | |
| 189 | public: |
| 190 | |
| 191 | BlockTask(Monitor& monitor, Monitor& bmonitor, size_t& count) : |
| 192 | _monitor(monitor), |
| 193 | _bmonitor(bmonitor), |
| 194 | _count(count) {} |
| 195 | |
| 196 | void run() { |
| 197 | { |
| 198 | Synchronized s(_bmonitor); |
| 199 | |
| 200 | _bmonitor.wait(); |
| 201 | |
| 202 | } |
| 203 | |
| 204 | { |
| 205 | Synchronized s(_monitor); |
| 206 | |
| 207 | _count--; |
| 208 | |
| 209 | if (_count == 0) { |
| 210 | |
| 211 | _monitor.notify(); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | Monitor& _monitor; |
| 217 | Monitor& _bmonitor; |
| 218 | size_t& _count; |
| 219 | }; |
| 220 | |
| 221 | /** |
| 222 | * Block test. Create pendingTaskCountMax tasks. Verify that we block adding the |
| 223 | * pendingTaskCountMax + 1th task. Verify that we unblock when a task completes */ |
| 224 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 225 | bool blockTest(int64_t timeout=100LL, size_t workerCount=2) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 226 | |
| 227 | bool success = false; |
| 228 | |
| 229 | try { |
| 230 | |
| 231 | Monitor bmonitor; |
| 232 | Monitor monitor; |
| 233 | |
| 234 | size_t pendingTaskMaxCount = workerCount; |
| 235 | |
| 236 | size_t activeCounts[] = {workerCount, pendingTaskMaxCount, 1}; |
| 237 | |
| 238 | shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount, pendingTaskMaxCount); |
| 239 | |
| 240 | shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
| 241 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 242 | threadFactory->setPriority(PosixThreadFactory::HIGHEST); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 243 | |
| 244 | threadManager->threadFactory(threadFactory); |
| 245 | |
| 246 | threadManager->start(); |
| 247 | |
| 248 | std::set<shared_ptr<ThreadManagerTests::BlockTask> > tasks; |
| 249 | |
| 250 | for (size_t ix = 0; ix < workerCount; ix++) { |
| 251 | |
| 252 | tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>(new ThreadManagerTests::BlockTask(monitor, bmonitor,activeCounts[0]))); |
| 253 | } |
| 254 | |
| 255 | for (size_t ix = 0; ix < pendingTaskMaxCount; ix++) { |
| 256 | |
| 257 | tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>(new ThreadManagerTests::BlockTask(monitor, bmonitor,activeCounts[1]))); |
| 258 | } |
| 259 | |
| 260 | for (std::set<shared_ptr<ThreadManagerTests::BlockTask> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) { |
| 261 | threadManager->add(*ix); |
| 262 | } |
| 263 | |
| 264 | if(!(success = (threadManager->totalTaskCount() == pendingTaskMaxCount + workerCount))) { |
| 265 | throw TException("Unexpected pending task count"); |
| 266 | } |
| 267 | |
| 268 | shared_ptr<ThreadManagerTests::BlockTask> extraTask(new ThreadManagerTests::BlockTask(monitor, bmonitor, activeCounts[2])); |
| 269 | |
| 270 | try { |
| 271 | threadManager->add(extraTask, 1); |
| 272 | throw TException("Unexpected success adding task in excess of pending task count"); |
| 273 | } catch(TimedOutException& e) { |
| 274 | } |
| 275 | |
| 276 | std::cout << "\t\t\t" << "Pending tasks " << threadManager->pendingTaskCount() << std::endl; |
| 277 | |
| 278 | { |
| 279 | Synchronized s(bmonitor); |
| 280 | |
| 281 | bmonitor.notifyAll(); |
| 282 | } |
| 283 | |
| 284 | { |
| 285 | Synchronized s(monitor); |
| 286 | |
| 287 | while(activeCounts[0] != 0) { |
| 288 | monitor.wait(); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | std::cout << "\t\t\t" << "Pending tasks " << threadManager->pendingTaskCount() << std::endl; |
| 293 | |
| 294 | try { |
| 295 | threadManager->add(extraTask, 1); |
| 296 | } catch(TimedOutException& e) { |
| 297 | std::cout << "\t\t\t" << "add timed out unexpectedly" << std::endl; |
| 298 | throw TException("Unexpected timeout adding task"); |
| 299 | |
| 300 | } catch(TooManyPendingTasksException& e) { |
| 301 | std::cout << "\t\t\t" << "add encountered too many pending exepctions" << std::endl; |
| 302 | throw TException("Unexpected timeout adding task"); |
| 303 | } |
| 304 | |
| 305 | // Wake up tasks that were pending before and wait for them to complete |
| 306 | |
| 307 | { |
| 308 | Synchronized s(bmonitor); |
| 309 | |
| 310 | bmonitor.notifyAll(); |
| 311 | } |
| 312 | |
| 313 | { |
| 314 | Synchronized s(monitor); |
| 315 | |
| 316 | while(activeCounts[1] != 0) { |
| 317 | monitor.wait(); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // Wake up the extra task and wait for it to complete |
| 322 | |
| 323 | { |
| 324 | Synchronized s(bmonitor); |
| 325 | |
| 326 | bmonitor.notifyAll(); |
| 327 | } |
| 328 | |
| 329 | { |
| 330 | Synchronized s(monitor); |
| 331 | |
| 332 | while(activeCounts[2] != 0) { |
| 333 | monitor.wait(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if(!(success = (threadManager->totalTaskCount() == 0))) { |
| 338 | throw TException("Unexpected pending task count"); |
| 339 | } |
| 340 | |
| 341 | } catch(TException& e) { |
| 342 | } |
| 343 | |
| 344 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << std::endl; |
| 345 | return success; |
| 346 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 347 | }; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 348 | |
| 349 | const double ThreadManagerTests::ERROR = .20; |
| 350 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 351 | }}}} // facebook::thrift::concurrency |
| 352 | |
| 353 | using namespace facebook::thrift::concurrency::test; |
| 354 | |