blob: f2b0111e34c1cba3d75ecd43791bfdf076ef7b98 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Marc Slemko8a40a762006-07-19 17:46:50 +000020#include <iostream>
Marc Slemkoc7782972006-07-25 02:26:35 +000021#include <vector>
Marc Slemko8a40a762006-07-19 17:46:50 +000022#include <string>
23
24#include "ThreadFactoryTests.h"
25#include "TimerManagerTests.h"
Marc Slemkod466b212006-07-20 00:04:18 +000026#include "ThreadManagerTests.h"
Marc Slemko8a40a762006-07-19 17:46:50 +000027
James E. King, III7d211b82017-09-06 10:12:02 -070028// 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 III3ae30422018-03-12 07:33:22 -040030static int WEIGHT = 10;
James E. King, III7d211b82017-09-06 10:12:02 -070031
Marc Slemko8a40a762006-07-19 17:46:50 +000032int main(int argc, char** argv) {
33
34 std::string arg;
35
Konrad Grochowski16a23a62014-11-13 15:33:38 +010036 std::vector<std::string> args(argc - 1 > 1 ? argc - 1 : 1);
Marc Slemko8a40a762006-07-19 17:46:50 +000037
Marc Slemkoc7782972006-07-25 02:26:35 +000038 args[0] = "all";
Marc Slemko8a40a762006-07-19 17:46:50 +000039
Mark Sleef5f2be42006-09-05 21:05:31 +000040 for (int ix = 1; ix < argc; ix++) {
Marc Slemkoc7782972006-07-25 02:26:35 +000041 args[ix - 1] = std::string(argv[ix]);
Marc Slemko8a40a762006-07-19 17:46:50 +000042 }
43
Sebastian Zenker042580f2019-01-29 15:48:12 +010044 if (getenv("VALGRIND") != nullptr) {
James E. King, III7d211b82017-09-06 10:12:02 -070045 // lower the scale of every test
46 WEIGHT = 1;
47 }
Francois Ferrand69603702017-09-11 12:09:40 +020048
Marc Slemkoc7782972006-07-25 02:26:35 +000049 bool runAll = args[0].compare("all") == 0;
Marc Slemko8a40a762006-07-19 17:46:50 +000050
Mark Sleef5f2be42006-09-05 21:05:31 +000051 if (runAll || args[0].compare("thread-factory") == 0) {
Marc Slemko8a40a762006-07-19 17:46:50 +000052
53 ThreadFactoryTests threadFactoryTests;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000054
Marc Slemko8a40a762006-07-19 17:46:50 +000055 std::cout << "ThreadFactory tests..." << std::endl;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000056
James E. King, III7d211b82017-09-06 10:12:02 -070057 int reapLoops = 2 * WEIGHT;
58 int reapCount = 100 * WEIGHT;
James E. King, IIIdf899132016-11-12 15:16:30 -050059 size_t floodLoops = 3;
James E. King, III7d211b82017-09-06 10:12:02 -070060 size_t floodCount = 500 * WEIGHT;
Marc Slemko8a40a762006-07-19 17:46:50 +000061
James E. King, IIIdf899132016-11-12 15:16:30 -050062 std::cout << "\t\tThreadFactory reap N threads test: N = " << reapLoops << "x" << reapCount << std::endl;
Marc Slemko03dedd92006-07-20 00:58:47 +000063
James E. King, IIIdf899132016-11-12 15:16:30 -050064 if (!threadFactoryTests.reapNThreads(reapLoops, reapCount)) {
65 std::cerr << "\t\ttThreadFactory reap N threads FAILED" << std::endl;
66 return 1;
67 }
Marc Slemko8a40a762006-07-19 17:46:50 +000068
James E. King, IIIdf899132016-11-12 15:16:30 -050069 std::cout << "\t\tThreadFactory flood N threads test: N = " << floodLoops << "x" << floodCount << std::endl;
Marc Slemkoa6479032007-06-05 22:20:14 +000070
James E. King, IIIdf899132016-11-12 15:16:30 -050071 if (!threadFactoryTests.floodNTest(floodLoops, floodCount)) {
72 std::cerr << "\t\ttThreadFactory flood N threads FAILED" << std::endl;
73 return 1;
74 }
Marc Slemkoa6479032007-06-05 22:20:14 +000075
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000076 std::cout << "\t\tThreadFactory synchronous start test" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +000077
James E. King, IIIdf899132016-11-12 15:16:30 -050078 if (!threadFactoryTests.synchStartTest()) {
79 std::cerr << "\t\ttThreadFactory synchronous start FAILED" << std::endl;
80 return 1;
81 }
Marc Slemkoc7782972006-07-25 02:26:35 +000082
83 std::cout << "\t\tThreadFactory monitor timeout test" << std::endl;
84
James E. King, IIIdf899132016-11-12 15:16:30 -050085 if (!threadFactoryTests.monitorTimeoutTest()) {
86 std::cerr << "\t\ttThreadFactory monitor timeout FAILED" << std::endl;
87 return 1;
88 }
Marc Slemko8a40a762006-07-19 17:46:50 +000089 }
90
Mark Sleef5f2be42006-09-05 21:05:31 +000091 if (runAll || args[0].compare("util") == 0) {
Marc Slemkoc7782972006-07-25 02:26:35 +000092
93 std::cout << "Util tests..." << std::endl;
94
95 std::cout << "\t\tUtil minimum time" << std::endl;
96
cyybfdbd032019-01-12 14:38:28 +080097 int64_t time00 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
98 int64_t time01 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
Marc Slemkoc7782972006-07-25 02:26:35 +000099
100 std::cout << "\t\t\tMinimum time: " << time01 - time00 << "ms" << std::endl;
101
cyybfdbd032019-01-12 14:38:28 +0800102 time00 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
Marc Slemkoc7782972006-07-25 02:26:35 +0000103 time01 = time00;
104 size_t count = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000105
Mark Sleef5f2be42006-09-05 21:05:31 +0000106 while (time01 < time00 + 10) {
Marc Slemkoc7782972006-07-25 02:26:35 +0000107 count++;
cyybfdbd032019-01-12 14:38:28 +0800108 time01 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
Marc Slemkoc7782972006-07-25 02:26:35 +0000109 }
110
111 std::cout << "\t\t\tscall per ms: " << count / (time01 - time00) << std::endl;
112 }
113
Mark Sleef5f2be42006-09-05 21:05:31 +0000114 if (runAll || args[0].compare("timer-manager") == 0) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000115
116 std::cout << "TimerManager tests..." << std::endl;
117
118 std::cout << "\t\tTimerManager test00" << std::endl;
119
120 TimerManagerTests timerManagerTests;
121
James E. King, IIIdf899132016-11-12 15:16:30 -0500122 if (!timerManagerTests.test00()) {
123 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
124 return 1;
125 }
Francois Ferrandcc2d5582017-08-25 09:01:26 +0200126
127 std::cout << "\t\tTimerManager test01" << std::endl;
128
129 if (!timerManagerTests.test01()) {
130 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
131 return 1;
132 }
133
134 std::cout << "\t\tTimerManager test02" << std::endl;
135
136 if (!timerManagerTests.test02()) {
137 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
138 return 1;
139 }
Francois Ferrand69603702017-09-11 12:09:40 +0200140
141 std::cout << "\t\tTimerManager test03" << std::endl;
142
143 if (!timerManagerTests.test03()) {
144 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
145 return 1;
146 }
147
148 std::cout << "\t\tTimerManager test04" << std::endl;
149
150 if (!timerManagerTests.test04()) {
151 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
152 return 1;
153 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000154 }
Marc Slemkod466b212006-07-20 00:04:18 +0000155
Mark Sleef5f2be42006-09-05 21:05:31 +0000156 if (runAll || args[0].compare("thread-manager") == 0) {
Marc Slemkod466b212006-07-20 00:04:18 +0000157
158 std::cout << "ThreadManager tests..." << std::endl;
159
Marc Slemkoc7782972006-07-25 02:26:35 +0000160 {
James E. King, III7d211b82017-09-06 10:12:02 -0700161 size_t workerCount = 10 * WEIGHT;
162 size_t taskCount = 500 * WEIGHT;
Mark Slee9b82d272007-05-23 05:16:07 +0000163 int64_t delay = 10LL;
Marc Slemkod466b212006-07-20 00:04:18 +0000164
James E. King, IIIdf899132016-11-12 15:16:30 -0500165 ThreadManagerTests threadManagerTests;
166
167 std::cout << "\t\tThreadManager api test:" << std::endl;
168
169 if (!threadManagerTests.apiTest()) {
170 std::cerr << "\t\tThreadManager apiTest FAILED" << std::endl;
171 return 1;
172 }
173
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100174 std::cout << "\t\tThreadManager load test: worker count: " << workerCount
175 << " task count: " << taskCount << " delay: " << delay << std::endl;
Marc Slemkod466b212006-07-20 00:04:18 +0000176
James E. King, IIIdf899132016-11-12 15:16:30 -0500177 if (!threadManagerTests.loadTest(taskCount, delay, workerCount)) {
178 std::cerr << "\t\tThreadManager loadTest FAILED" << std::endl;
179 return 1;
180 }
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000181
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100182 std::cout << "\t\tThreadManager block test: worker count: " << workerCount
183 << " delay: " << delay << std::endl;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000184
James E. King, IIIdf899132016-11-12 15:16:30 -0500185 if (!threadManagerTests.blockTest(delay, workerCount)) {
186 std::cerr << "\t\tThreadManager blockTest FAILED" << std::endl;
187 return 1;
188 }
Marc Slemkoc7782972006-07-25 02:26:35 +0000189 }
190 }
191
Mark Sleef5f2be42006-09-05 21:05:31 +0000192 if (runAll || args[0].compare("thread-manager-benchmark") == 0) {
Marc Slemkoc7782972006-07-25 02:26:35 +0000193
194 std::cout << "ThreadManager benchmark tests..." << std::endl;
195
196 {
197
198 size_t minWorkerCount = 2;
199
James E. King, III7d211b82017-09-06 10:12:02 -0700200 size_t maxWorkerCount = 8;
Marc Slemkoc7782972006-07-25 02:26:35 +0000201
James E. King, III7d211b82017-09-06 10:12:02 -0700202 size_t tasksPerWorker = 100 * WEIGHT;
Marc Slemkoc7782972006-07-25 02:26:35 +0000203
James E. King, IIIdf899132016-11-12 15:16:30 -0500204 int64_t delay = 5LL;
Marc Slemkoc7782972006-07-25 02:26:35 +0000205
James E. King, III7d211b82017-09-06 10:12:02 -0700206 for (size_t workerCount = minWorkerCount; workerCount <= maxWorkerCount; workerCount *= 4) {
Marc Slemkoc7782972006-07-25 02:26:35 +0000207
David Reiss96d23882007-07-26 21:10:32 +0000208 size_t taskCount = workerCount * tasksPerWorker;
Marc Slemkoc7782972006-07-25 02:26:35 +0000209
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100210 std::cout << "\t\tThreadManager load test: worker count: " << workerCount
211 << " task count: " << taskCount << " delay: " << delay << std::endl;
Marc Slemkoc7782972006-07-25 02:26:35 +0000212
David Reiss96d23882007-07-26 21:10:32 +0000213 ThreadManagerTests threadManagerTests;
Marc Slemkoc7782972006-07-25 02:26:35 +0000214
James E. King, IIIdf899132016-11-12 15:16:30 -0500215 if (!threadManagerTests.loadTest(taskCount, delay, workerCount))
216 {
217 std::cerr << "\t\tThreadManager loadTest FAILED" << std::endl;
218 return 1;
219 }
Marc Slemkoc7782972006-07-25 02:26:35 +0000220 }
221 }
Marc Slemkod466b212006-07-20 00:04:18 +0000222 }
James E. King, IIIdf899132016-11-12 15:16:30 -0500223
224 std::cout << "ALL TESTS PASSED" << std::endl;
225 return 0;
Marc Slemko8a40a762006-07-19 17:46:50 +0000226}