Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 1 | #include <config.h> |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 2 | #include <concurrency/ThreadManager.h> |
| 3 | #include <concurrency/PosixThreadFactory.h> |
| 4 | #include <concurrency/Monitor.h> |
| 5 | #include <concurrency/Util.h> |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 6 | |
| 7 | #include <assert.h> |
| 8 | #include <set> |
| 9 | #include <iostream> |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <stdint.h> |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 12 | |
| 13 | namespace facebook { namespace thrift { namespace concurrency { namespace test { |
| 14 | |
| 15 | using namespace facebook::thrift::concurrency; |
| 16 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 17 | /** |
| 18 | * ThreadManagerTests class |
| 19 | * |
| 20 | * @author marc |
| 21 | * @version $Id:$ |
| 22 | */ |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 23 | class ThreadManagerTests { |
| 24 | |
| 25 | public: |
| 26 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 27 | static const double ERROR; |
| 28 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 29 | class Task: public Runnable { |
| 30 | |
| 31 | public: |
| 32 | |
| 33 | Task(Monitor& monitor, size_t& count, long long timeout) : |
| 34 | _monitor(monitor), |
| 35 | _count(count), |
| 36 | _timeout(timeout), |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 37 | _done(false) {} |
| 38 | |
| 39 | void run() { |
| 40 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 41 | _startTime = Util::currentTime(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 42 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 43 | { |
| 44 | Synchronized s(_sleep); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 45 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 46 | _sleep.wait(_timeout); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 47 | } |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 48 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 49 | _endTime = Util::currentTime(); |
| 50 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 51 | _done = true; |
| 52 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 53 | { |
| 54 | Synchronized s(_monitor); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 55 | |
| 56 | // std::cout << "Thread " << _count << " completed " << std::endl; |
| 57 | |
| 58 | _count--; |
| 59 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 60 | if (_count == 0) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 61 | |
| 62 | _monitor.notify(); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | Monitor& _monitor; |
| 68 | size_t& _count; |
| 69 | long long _timeout; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 70 | long long _startTime; |
| 71 | long long _endTime; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 72 | bool _done; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 73 | Monitor _sleep; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 76 | /** |
| 77 | * Dispatch count tasks, each of which blocks for timeout milliseconds then |
| 78 | * completes. Verify that all tasks completed and that thread manager cleans |
| 79 | * up properly on delete. |
| 80 | */ |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 81 | bool loadTest(size_t count=100, long long timeout=100LL, size_t workerCount=4) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 82 | |
| 83 | Monitor monitor; |
| 84 | |
| 85 | size_t activeCount = count; |
| 86 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 87 | shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 88 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 89 | shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 90 | |
| 91 | threadFactory->priority(PosixThreadFactory::HIGHEST); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 92 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 93 | threadManager->threadFactory(threadFactory); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 94 | |
| 95 | threadManager->start(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 96 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 97 | std::set<shared_ptr<ThreadManagerTests::Task> > tasks; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 98 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 99 | for (size_t ix = 0; ix < count; ix++) { |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 100 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 101 | tasks.insert(shared_ptr<ThreadManagerTests::Task>(new ThreadManagerTests::Task(monitor, activeCount, timeout))); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 104 | long long time00 = Util::currentTime(); |
| 105 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 106 | 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] | 107 | |
| 108 | threadManager->add(*ix); |
| 109 | } |
| 110 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 111 | { |
| 112 | Synchronized s(monitor); |
| 113 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 114 | while(activeCount > 0) { |
| 115 | |
| 116 | monitor.wait(); |
| 117 | } |
| 118 | } |
| 119 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 120 | long long time01 = Util::currentTime(); |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 121 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 122 | long long firstTime = 9223372036854775807LL; |
| 123 | long long lastTime = 0; |
| 124 | |
| 125 | double averageTime = 0; |
| 126 | long long minTime = 9223372036854775807LL; |
| 127 | long long maxTime = 0; |
| 128 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 129 | for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 130 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 131 | shared_ptr<ThreadManagerTests::Task> task = *ix; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 132 | |
| 133 | long long delta = task->_endTime - task->_startTime; |
| 134 | |
| 135 | assert(delta > 0); |
| 136 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 137 | if (task->_startTime < firstTime) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 138 | firstTime = task->_startTime; |
| 139 | } |
| 140 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 141 | if (task->_endTime > lastTime) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 142 | lastTime = task->_endTime; |
| 143 | } |
| 144 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 145 | if (delta < minTime) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 146 | minTime = delta; |
| 147 | } |
| 148 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 149 | if (delta > maxTime) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 150 | maxTime = delta; |
| 151 | } |
| 152 | |
| 153 | averageTime+= delta; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 154 | } |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 155 | |
| 156 | averageTime /= count; |
| 157 | |
| 158 | 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] | 159 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 160 | double expectedTime = ((count + (workerCount - 1)) / workerCount) * timeout; |
| 161 | |
| 162 | double error = ((time01 - time00) - expectedTime) / expectedTime; |
| 163 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 164 | if (error < 0) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 165 | error*= -1.0; |
| 166 | } |
| 167 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 168 | bool success = error < ERROR; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 169 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 170 | 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] | 171 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 172 | return success; |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 173 | } |
| 174 | }; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 175 | |
| 176 | const double ThreadManagerTests::ERROR = .20; |
| 177 | |
Marc Slemko | 740343d | 2006-07-20 00:31:02 +0000 | [diff] [blame] | 178 | }}}} // facebook::thrift::concurrency |
| 179 | |
| 180 | using namespace facebook::thrift::concurrency::test; |
| 181 | |