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