David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Roger Meier | ba406d3 | 2013-07-15 22:41:34 +0200 | [diff] [blame] | 20 | #include <thrift/thrift-config.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 21 | #include <thrift/concurrency/Thread.h> |
| 22 | #include <thrift/concurrency/PlatformThreadFactory.h> |
| 23 | #include <thrift/concurrency/Monitor.h> |
| 24 | #include <thrift/concurrency/Util.h> |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 25 | |
| 26 | #include <assert.h> |
| 27 | #include <iostream> |
| 28 | #include <set> |
| 29 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 30 | namespace apache { |
| 31 | namespace thrift { |
| 32 | namespace concurrency { |
| 33 | namespace test { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 34 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 35 | using boost::shared_ptr; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 36 | using namespace apache::thrift::concurrency; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 37 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 38 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 39 | * ThreadManagerTests class |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 40 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 41 | * @version $Id:$ |
| 42 | */ |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 43 | class ThreadFactoryTests { |
| 44 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 45 | public: |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 46 | static const double TEST_TOLERANCE; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 47 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 48 | class Task : public Runnable { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 49 | |
| 50 | public: |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 51 | Task() {} |
| 52 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 53 | void run() { std::cout << "\t\t\tHello World" << std::endl; } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 56 | /** |
| 57 | * Hello world test |
| 58 | */ |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 59 | bool helloWorldTest() { |
| 60 | |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 61 | PlatformThreadFactory threadFactory = PlatformThreadFactory(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 62 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 63 | shared_ptr<Task> task = shared_ptr<Task>(new ThreadFactoryTests::Task()); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 64 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 65 | shared_ptr<Thread> thread = threadFactory.newThread(task); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 66 | |
| 67 | thread->start(); |
| 68 | |
| 69 | thread->join(); |
| 70 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 71 | std::cout << "\t\t\tSuccess!" << std::endl; |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 76 | /** |
| 77 | * Reap N threads |
| 78 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 79 | class ReapNTask : public Runnable { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 80 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 81 | public: |
| 82 | ReapNTask(Monitor& monitor, int& activeCount) : _monitor(monitor), _count(activeCount) {} |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 83 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 84 | void run() { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 85 | Synchronized s(_monitor); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 86 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 87 | _count--; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 88 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 89 | // std::cout << "\t\t\tthread count: " << _count << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 90 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 91 | if (_count == 0) { |
| 92 | _monitor.notify(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | Monitor& _monitor; |
| 97 | |
| 98 | int& _count; |
| 99 | }; |
| 100 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 101 | bool reapNThreads(int loop = 1, int count = 10) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 102 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 103 | PlatformThreadFactory threadFactory = PlatformThreadFactory(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 104 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 105 | Monitor* monitor = new Monitor(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 106 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 107 | for (int lix = 0; lix < loop; lix++) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 108 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 109 | int* activeCount = new int(count); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 110 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 111 | std::set<shared_ptr<Thread> > threads; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 112 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 113 | int tix; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 114 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 115 | for (tix = 0; tix < count; tix++) { |
| 116 | try { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 117 | threads.insert( |
| 118 | threadFactory.newThread(shared_ptr<Runnable>(new ReapNTask(*monitor, *activeCount)))); |
| 119 | } catch (SystemResourceException& e) { |
| 120 | std::cout << "\t\t\tfailed to create " << lix* count + tix << " thread " << e.what() |
| 121 | << std::endl; |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 122 | throw e; |
| 123 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 124 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 125 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 126 | tix = 0; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 127 | for (std::set<shared_ptr<Thread> >::const_iterator thread = threads.begin(); |
| 128 | thread != threads.end(); |
| 129 | tix++, ++thread) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 130 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 131 | try { |
| 132 | (*thread)->start(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 133 | } catch (SystemResourceException& e) { |
| 134 | std::cout << "\t\t\tfailed to start " << lix* count + tix << " thread " << e.what() |
| 135 | << std::endl; |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 136 | throw e; |
| 137 | } |
| 138 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 139 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 140 | { |
| 141 | Synchronized s(*monitor); |
| 142 | while (*activeCount > 0) { |
| 143 | monitor->wait(1000); |
| 144 | } |
| 145 | } |
Roger Meier | 3075ffc | 2011-08-04 22:36:07 +0000 | [diff] [blame] | 146 | delete activeCount; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 147 | std::cout << "\t\t\treaped " << lix* count << " threads" << std::endl; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | std::cout << "\t\t\tSuccess!" << std::endl; |
| 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 155 | class SynchStartTask : public Runnable { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 156 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 157 | public: |
| 158 | enum STATE { UNINITIALIZED, STARTING, STARTED, STOPPING, STOPPED }; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 159 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 160 | SynchStartTask(Monitor& monitor, volatile STATE& state) : _monitor(monitor), _state(state) {} |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 161 | |
| 162 | void run() { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 163 | { |
| 164 | Synchronized s(_monitor); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 165 | if (_state == SynchStartTask::STARTING) { |
| 166 | _state = SynchStartTask::STARTED; |
| 167 | _monitor.notify(); |
| 168 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 171 | { |
| 172 | Synchronized s(_monitor); |
| 173 | while (_state == SynchStartTask::STARTED) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 174 | _monitor.wait(); |
| 175 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 176 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 177 | if (_state == SynchStartTask::STOPPING) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 178 | _state = SynchStartTask::STOPPED; |
| 179 | _monitor.notifyAll(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 180 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 184 | private: |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 185 | Monitor& _monitor; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 186 | volatile STATE& _state; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | bool synchStartTest() { |
| 190 | |
| 191 | Monitor monitor; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 192 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 193 | SynchStartTask::STATE state = SynchStartTask::UNINITIALIZED; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 194 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 195 | shared_ptr<SynchStartTask> task |
| 196 | = shared_ptr<SynchStartTask>(new SynchStartTask(monitor, state)); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 197 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 198 | PlatformThreadFactory threadFactory = PlatformThreadFactory(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 199 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 200 | shared_ptr<Thread> thread = threadFactory.newThread(task); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 201 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 202 | if (state == SynchStartTask::UNINITIALIZED) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 203 | |
| 204 | state = SynchStartTask::STARTING; |
| 205 | |
| 206 | thread->start(); |
| 207 | } |
| 208 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 209 | { |
| 210 | Synchronized s(monitor); |
| 211 | while (state == SynchStartTask::STARTING) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 212 | monitor.wait(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | assert(state != SynchStartTask::STARTING); |
| 217 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 218 | { |
| 219 | Synchronized s(monitor); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 220 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 221 | try { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 222 | monitor.wait(100); |
| 223 | } catch (TimedOutException& e) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 224 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 225 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 226 | if (state == SynchStartTask::STARTED) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 227 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 228 | state = SynchStartTask::STOPPING; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 229 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 230 | monitor.notify(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 231 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 232 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 233 | while (state == SynchStartTask::STOPPING) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 234 | monitor.wait(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | assert(state == SynchStartTask::STOPPED); |
| 239 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 240 | bool success = true; |
| 241 | |
| 242 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "!" << std::endl; |
| 243 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 244 | return true; |
| 245 | } |
| 246 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 247 | /** See how accurate monitor timeout is. */ |
| 248 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 249 | bool monitorTimeoutTest(size_t count = 1000, int64_t timeout = 10) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 250 | |
| 251 | Monitor monitor; |
| 252 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 253 | int64_t startTime = Util::currentTime(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 254 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 255 | for (size_t ix = 0; ix < count; ix++) { |
| 256 | { |
| 257 | Synchronized s(monitor); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 258 | try { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 259 | monitor.wait(timeout); |
| 260 | } catch (TimedOutException& e) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 261 | } |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 265 | int64_t endTime = Util::currentTime(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 266 | |
| 267 | double error = ((endTime - startTime) - (count * timeout)) / (double)(count * timeout); |
| 268 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 269 | if (error < 0.0) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 270 | |
| 271 | error *= 1.0; |
| 272 | } |
| 273 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 274 | bool success = error < ThreadFactoryTests::TEST_TOLERANCE; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 275 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 276 | std::cout << "\t\t\t" << (success ? "Success" : "Failure") |
| 277 | << "! expected time: " << count * timeout |
| 278 | << "ms elapsed time: " << endTime - startTime << "ms error%: " << error * 100.0 |
| 279 | << std::endl; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 280 | |
| 281 | return success; |
| 282 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 283 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 284 | class FloodTask : public Runnable { |
| 285 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 286 | FloodTask(const size_t id) : _id(id) {} |
| 287 | ~FloodTask() { |
| 288 | if (_id % 1000 == 0) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 289 | std::cout << "\t\tthread " << _id << " done" << std::endl; |
| 290 | } |
| 291 | } |
| 292 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 293 | void run() { |
| 294 | if (_id % 1000 == 0) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 295 | std::cout << "\t\tthread " << _id << " started" << std::endl; |
| 296 | } |
| 297 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 298 | THRIFT_SLEEP_USEC(1); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 299 | } |
| 300 | const size_t _id; |
| 301 | }; |
| 302 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 303 | void foo(PlatformThreadFactory* tf) { (void)tf; } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 304 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 305 | bool floodNTest(size_t loop = 1, size_t count = 100000) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 306 | |
| 307 | bool success = false; |
| 308 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 309 | for (size_t lix = 0; lix < loop; lix++) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 310 | |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 311 | PlatformThreadFactory threadFactory = PlatformThreadFactory(); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 312 | threadFactory.setDetached(true); |
| 313 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 314 | for (size_t tix = 0; tix < count; tix++) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 315 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 316 | try { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 317 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 318 | shared_ptr<FloodTask> task(new FloodTask(lix * count + tix)); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 319 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 320 | shared_ptr<Thread> thread = threadFactory.newThread(task); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 321 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 322 | thread->start(); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 323 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 324 | THRIFT_SLEEP_USEC(1); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 325 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 326 | } catch (TException& e) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 327 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 328 | std::cout << "\t\t\tfailed to start " << lix* count + tix << " thread " << e.what() |
| 329 | << std::endl; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 330 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 331 | return success; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 332 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 333 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 334 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 335 | std::cout << "\t\t\tflooded " << (lix + 1) * count << " threads" << std::endl; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 336 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 337 | success = true; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return success; |
| 341 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 342 | }; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 343 | |
Konrad Grochowski | 293a40e | 2014-09-04 17:28:17 +0400 | [diff] [blame] | 344 | const double ThreadFactoryTests::TEST_TOLERANCE = .20; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |
| 348 | } // apache::thrift::concurrency::test |