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 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 20 | #include <iostream> |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 21 | #include <vector> |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | #include "ThreadFactoryTests.h" |
| 25 | #include "TimerManagerTests.h" |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 26 | #include "ThreadManagerTests.h" |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 27 | |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 28 | // The test weight, where 10 is 10 times more threads than baseline |
| 29 | // and the baseline is optimized for running in valgrind |
James E. King III | 3ae3042 | 2018-03-12 07:33:22 -0400 | [diff] [blame] | 30 | static int WEIGHT = 10; |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 31 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 32 | int main(int argc, char** argv) { |
| 33 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 34 | std::vector<std::string> args(argc - 1 > 1 ? argc - 1 : 1); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 35 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 36 | args[0] = "all"; |
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 | for (int ix = 1; ix < argc; ix++) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 39 | args[ix - 1] = std::string(argv[ix]); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 42 | if (getenv("VALGRIND") != nullptr) { |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 43 | // lower the scale of every test |
| 44 | WEIGHT = 1; |
| 45 | } |
Francois Ferrand | 6960370 | 2017-09-11 12:09:40 +0200 | [diff] [blame] | 46 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 47 | bool runAll = args[0].compare("all") == 0; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 48 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 49 | if (runAll || args[0].compare("thread-factory") == 0) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 50 | |
| 51 | ThreadFactoryTests threadFactoryTests; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 52 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 53 | std::cout << "ThreadFactory tests..." << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 54 | |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 55 | int reapLoops = 2 * WEIGHT; |
| 56 | int reapCount = 100 * WEIGHT; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 57 | size_t floodLoops = 3; |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 58 | size_t floodCount = 500 * WEIGHT; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 59 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 60 | std::cout << "\t\tThreadFactory reap N threads test: N = " << reapLoops << "x" << reapCount << std::endl; |
Marc Slemko | 03dedd9 | 2006-07-20 00:58:47 +0000 | [diff] [blame] | 61 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 62 | if (!threadFactoryTests.reapNThreads(reapLoops, reapCount)) { |
| 63 | std::cerr << "\t\ttThreadFactory reap N threads FAILED" << std::endl; |
| 64 | return 1; |
| 65 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 66 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 67 | std::cout << "\t\tThreadFactory flood N threads test: N = " << floodLoops << "x" << floodCount << std::endl; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 68 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 69 | if (!threadFactoryTests.floodNTest(floodLoops, floodCount)) { |
| 70 | std::cerr << "\t\ttThreadFactory flood N threads FAILED" << std::endl; |
| 71 | return 1; |
| 72 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 73 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 74 | std::cout << "\t\tThreadFactory synchronous start test" << std::endl; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 75 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 76 | if (!threadFactoryTests.synchStartTest()) { |
| 77 | std::cerr << "\t\ttThreadFactory synchronous start FAILED" << std::endl; |
| 78 | return 1; |
| 79 | } |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 80 | |
| 81 | std::cout << "\t\tThreadFactory monitor timeout test" << std::endl; |
| 82 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 83 | if (!threadFactoryTests.monitorTimeoutTest()) { |
| 84 | std::cerr << "\t\ttThreadFactory monitor timeout FAILED" << std::endl; |
| 85 | return 1; |
| 86 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 89 | if (runAll || args[0].compare("util") == 0) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 90 | |
| 91 | std::cout << "Util tests..." << std::endl; |
| 92 | |
| 93 | std::cout << "\t\tUtil minimum time" << std::endl; |
| 94 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 95 | int64_t time00 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
| 96 | int64_t time01 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 97 | |
| 98 | std::cout << "\t\t\tMinimum time: " << time01 - time00 << "ms" << std::endl; |
| 99 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 100 | time00 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 101 | time01 = time00; |
| 102 | size_t count = 0; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 103 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 104 | while (time01 < time00 + 10) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 105 | count++; |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 106 | time01 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | std::cout << "\t\t\tscall per ms: " << count / (time01 - time00) << std::endl; |
| 110 | } |
| 111 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 112 | if (runAll || args[0].compare("timer-manager") == 0) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 113 | |
| 114 | std::cout << "TimerManager tests..." << std::endl; |
| 115 | |
| 116 | std::cout << "\t\tTimerManager test00" << std::endl; |
| 117 | |
| 118 | TimerManagerTests timerManagerTests; |
| 119 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 120 | if (!timerManagerTests.test00()) { |
| 121 | std::cerr << "\t\tTimerManager tests FAILED" << std::endl; |
| 122 | return 1; |
| 123 | } |
Francois Ferrand | cc2d558 | 2017-08-25 09:01:26 +0200 | [diff] [blame] | 124 | |
| 125 | std::cout << "\t\tTimerManager test01" << std::endl; |
| 126 | |
| 127 | if (!timerManagerTests.test01()) { |
| 128 | std::cerr << "\t\tTimerManager tests FAILED" << std::endl; |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | std::cout << "\t\tTimerManager test02" << std::endl; |
| 133 | |
| 134 | if (!timerManagerTests.test02()) { |
| 135 | std::cerr << "\t\tTimerManager tests FAILED" << std::endl; |
| 136 | return 1; |
| 137 | } |
Francois Ferrand | 6960370 | 2017-09-11 12:09:40 +0200 | [diff] [blame] | 138 | |
| 139 | std::cout << "\t\tTimerManager test03" << std::endl; |
| 140 | |
| 141 | if (!timerManagerTests.test03()) { |
| 142 | std::cerr << "\t\tTimerManager tests FAILED" << std::endl; |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | std::cout << "\t\tTimerManager test04" << std::endl; |
| 147 | |
| 148 | if (!timerManagerTests.test04()) { |
| 149 | std::cerr << "\t\tTimerManager tests FAILED" << std::endl; |
| 150 | return 1; |
| 151 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 152 | } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 153 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 154 | if (runAll || args[0].compare("thread-manager") == 0) { |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 155 | |
| 156 | std::cout << "ThreadManager tests..." << std::endl; |
| 157 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 158 | { |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 159 | size_t workerCount = 10 * WEIGHT; |
| 160 | size_t taskCount = 500 * WEIGHT; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 161 | int64_t delay = 10LL; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 162 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 163 | ThreadManagerTests threadManagerTests; |
| 164 | |
| 165 | std::cout << "\t\tThreadManager api test:" << std::endl; |
| 166 | |
| 167 | if (!threadManagerTests.apiTest()) { |
| 168 | std::cerr << "\t\tThreadManager apiTest FAILED" << std::endl; |
| 169 | return 1; |
| 170 | } |
| 171 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 172 | std::cout << "\t\tThreadManager load test: worker count: " << workerCount |
| 173 | << " task count: " << taskCount << " delay: " << delay << std::endl; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 174 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 175 | if (!threadManagerTests.loadTest(taskCount, delay, workerCount)) { |
| 176 | std::cerr << "\t\tThreadManager loadTest FAILED" << std::endl; |
| 177 | return 1; |
| 178 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 179 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 180 | std::cout << "\t\tThreadManager block test: worker count: " << workerCount |
| 181 | << " delay: " << delay << std::endl; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 182 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 183 | if (!threadManagerTests.blockTest(delay, workerCount)) { |
| 184 | std::cerr << "\t\tThreadManager blockTest FAILED" << std::endl; |
| 185 | return 1; |
| 186 | } |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 190 | if (runAll || args[0].compare("thread-manager-benchmark") == 0) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 191 | |
| 192 | std::cout << "ThreadManager benchmark tests..." << std::endl; |
| 193 | |
| 194 | { |
| 195 | |
| 196 | size_t minWorkerCount = 2; |
| 197 | |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 198 | size_t maxWorkerCount = 8; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 199 | |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 200 | size_t tasksPerWorker = 100 * WEIGHT; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 201 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 202 | int64_t delay = 5LL; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 203 | |
James E. King, III | 7d211b8 | 2017-09-06 10:12:02 -0700 | [diff] [blame] | 204 | for (size_t workerCount = minWorkerCount; workerCount <= maxWorkerCount; workerCount *= 4) { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 205 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 206 | size_t taskCount = workerCount * tasksPerWorker; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 207 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 208 | std::cout << "\t\tThreadManager load test: worker count: " << workerCount |
| 209 | << " task count: " << taskCount << " delay: " << delay << std::endl; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 210 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 211 | ThreadManagerTests threadManagerTests; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 212 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 213 | if (!threadManagerTests.loadTest(taskCount, delay, workerCount)) |
| 214 | { |
| 215 | std::cerr << "\t\tThreadManager loadTest FAILED" << std::endl; |
| 216 | return 1; |
| 217 | } |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 220 | } |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 221 | |
| 222 | std::cout << "ALL TESTS PASSED" << std::endl; |
| 223 | return 0; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 224 | } |