blob: 45054a92ad6fd13afaa29e6515caf79a27bc11c3 [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>
Mario Emmenlauerd3276362022-09-25 18:58:55 +020023#include <chrono>
Marc Slemko8a40a762006-07-19 17:46:50 +000024
25#include "ThreadFactoryTests.h"
26#include "TimerManagerTests.h"
Marc Slemkod466b212006-07-20 00:04:18 +000027#include "ThreadManagerTests.h"
Marc Slemko8a40a762006-07-19 17:46:50 +000028
James E. King, III7d211b82017-09-06 10:12:02 -070029// The test weight, where 10 is 10 times more threads than baseline
30// and the baseline is optimized for running in valgrind
James E. King III3ae30422018-03-12 07:33:22 -040031static int WEIGHT = 10;
James E. King, III7d211b82017-09-06 10:12:02 -070032
Marc Slemko8a40a762006-07-19 17:46:50 +000033int main(int argc, char** argv) {
34
Mario Emmenlauerd3276362022-09-25 18:58:55 +020035 std::vector<std::string> args((argc - 1) > 1 ? (argc - 1) : 1);
Marc Slemko8a40a762006-07-19 17:46:50 +000036
Marc Slemkoc7782972006-07-25 02:26:35 +000037 args[0] = "all";
Marc Slemko8a40a762006-07-19 17:46:50 +000038
Mark Sleef5f2be42006-09-05 21:05:31 +000039 for (int ix = 1; ix < argc; ix++) {
Marc Slemkoc7782972006-07-25 02:26:35 +000040 args[ix - 1] = std::string(argv[ix]);
Marc Slemko8a40a762006-07-19 17:46:50 +000041 }
42
Sebastian Zenker042580f2019-01-29 15:48:12 +010043 if (getenv("VALGRIND") != nullptr) {
James E. King, III7d211b82017-09-06 10:12:02 -070044 // lower the scale of every test
45 WEIGHT = 1;
46 }
Francois Ferrand69603702017-09-11 12:09:40 +020047
Mario Emmenlauerd3276362022-09-25 18:58:55 +020048 const bool runAll = args[0].compare("all") == 0;
Marc Slemko8a40a762006-07-19 17:46:50 +000049
Mark Sleef5f2be42006-09-05 21:05:31 +000050 if (runAll || args[0].compare("thread-factory") == 0) {
Marc Slemko8a40a762006-07-19 17:46:50 +000051
52 ThreadFactoryTests threadFactoryTests;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000053
Marc Slemko8a40a762006-07-19 17:46:50 +000054 std::cout << "ThreadFactory tests..." << std::endl;
Marc Slemko3a3b53b2007-05-22 23:59:54 +000055
Mario Emmenlauerd3276362022-09-25 18:58:55 +020056 const int reapLoops = 2 * WEIGHT;
57 const int reapCount = 100 * WEIGHT;
58 const size_t floodLoops = 3;
59 const size_t floodCount = 500 * WEIGHT;
Marc Slemko8a40a762006-07-19 17:46:50 +000060
James E. King, IIIdf899132016-11-12 15:16:30 -050061 std::cout << "\t\tThreadFactory reap N threads test: N = " << reapLoops << "x" << reapCount << std::endl;
Marc Slemko03dedd92006-07-20 00:58:47 +000062
James E. King, IIIdf899132016-11-12 15:16:30 -050063 if (!threadFactoryTests.reapNThreads(reapLoops, reapCount)) {
64 std::cerr << "\t\ttThreadFactory reap N threads FAILED" << std::endl;
65 return 1;
66 }
Marc Slemko8a40a762006-07-19 17:46:50 +000067
James E. King, IIIdf899132016-11-12 15:16:30 -050068 std::cout << "\t\tThreadFactory flood N threads test: N = " << floodLoops << "x" << floodCount << std::endl;
Marc Slemkoa6479032007-06-05 22:20:14 +000069
James E. King, IIIdf899132016-11-12 15:16:30 -050070 if (!threadFactoryTests.floodNTest(floodLoops, floodCount)) {
71 std::cerr << "\t\ttThreadFactory flood N threads FAILED" << std::endl;
72 return 1;
73 }
Marc Slemkoa6479032007-06-05 22:20:14 +000074
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000075 std::cout << "\t\tThreadFactory synchronous start test" << std::endl;
Marc Slemko8a40a762006-07-19 17:46:50 +000076
James E. King, IIIdf899132016-11-12 15:16:30 -050077 if (!threadFactoryTests.synchStartTest()) {
78 std::cerr << "\t\ttThreadFactory synchronous start FAILED" << std::endl;
79 return 1;
80 }
Marc Slemkoc7782972006-07-25 02:26:35 +000081
82 std::cout << "\t\tThreadFactory monitor timeout test" << std::endl;
83
James E. King, IIIdf899132016-11-12 15:16:30 -050084 if (!threadFactoryTests.monitorTimeoutTest()) {
85 std::cerr << "\t\ttThreadFactory monitor timeout FAILED" << std::endl;
86 return 1;
87 }
Marc Slemko8a40a762006-07-19 17:46:50 +000088 }
89
Mark Sleef5f2be42006-09-05 21:05:31 +000090 if (runAll || args[0].compare("util") == 0) {
Marc Slemkoc7782972006-07-25 02:26:35 +000091
92 std::cout << "Util tests..." << std::endl;
93
94 std::cout << "\t\tUtil minimum time" << std::endl;
95
cyybfdbd032019-01-12 14:38:28 +080096 int64_t time00 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
97 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 +000098
99 std::cout << "\t\t\tMinimum time: " << time01 - time00 << "ms" << std::endl;
100
cyybfdbd032019-01-12 14:38:28 +0800101 time00 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
Marc Slemkoc7782972006-07-25 02:26:35 +0000102 time01 = time00;
103 size_t count = 0;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000104
Mark Sleef5f2be42006-09-05 21:05:31 +0000105 while (time01 < time00 + 10) {
Marc Slemkoc7782972006-07-25 02:26:35 +0000106 count++;
cyybfdbd032019-01-12 14:38:28 +0800107 time01 = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
Marc Slemkoc7782972006-07-25 02:26:35 +0000108 }
109
110 std::cout << "\t\t\tscall per ms: " << count / (time01 - time00) << std::endl;
111 }
112
Mark Sleef5f2be42006-09-05 21:05:31 +0000113 if (runAll || args[0].compare("timer-manager") == 0) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000114
115 std::cout << "TimerManager tests..." << std::endl;
116
117 std::cout << "\t\tTimerManager test00" << std::endl;
118
119 TimerManagerTests timerManagerTests;
120
James E. King, IIIdf899132016-11-12 15:16:30 -0500121 if (!timerManagerTests.test00()) {
122 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
123 return 1;
124 }
Francois Ferrandcc2d5582017-08-25 09:01:26 +0200125
126 std::cout << "\t\tTimerManager test01" << std::endl;
127
128 if (!timerManagerTests.test01()) {
129 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
130 return 1;
131 }
132
133 std::cout << "\t\tTimerManager test02" << std::endl;
134
135 if (!timerManagerTests.test02()) {
136 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
137 return 1;
138 }
Francois Ferrand69603702017-09-11 12:09:40 +0200139
140 std::cout << "\t\tTimerManager test03" << std::endl;
141
142 if (!timerManagerTests.test03()) {
143 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
144 return 1;
145 }
146
147 std::cout << "\t\tTimerManager test04" << std::endl;
148
149 if (!timerManagerTests.test04()) {
150 std::cerr << "\t\tTimerManager tests FAILED" << std::endl;
151 return 1;
152 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000153 }
Marc Slemkod466b212006-07-20 00:04:18 +0000154
Mark Sleef5f2be42006-09-05 21:05:31 +0000155 if (runAll || args[0].compare("thread-manager") == 0) {
Marc Slemkod466b212006-07-20 00:04:18 +0000156
157 std::cout << "ThreadManager tests..." << std::endl;
158
Marc Slemkoc7782972006-07-25 02:26:35 +0000159 {
James E. King, III7d211b82017-09-06 10:12:02 -0700160 size_t workerCount = 10 * WEIGHT;
161 size_t taskCount = 500 * WEIGHT;
Mark Slee9b82d272007-05-23 05:16:07 +0000162 int64_t delay = 10LL;
Marc Slemkod466b212006-07-20 00:04:18 +0000163
James E. King, IIIdf899132016-11-12 15:16:30 -0500164 ThreadManagerTests threadManagerTests;
165
166 std::cout << "\t\tThreadManager api test:" << std::endl;
167
168 if (!threadManagerTests.apiTest()) {
169 std::cerr << "\t\tThreadManager apiTest FAILED" << std::endl;
170 return 1;
171 }
172
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100173 std::cout << "\t\tThreadManager load test: worker count: " << workerCount
174 << " task count: " << taskCount << " delay: " << delay << std::endl;
Marc Slemkod466b212006-07-20 00:04:18 +0000175
James E. King, IIIdf899132016-11-12 15:16:30 -0500176 if (!threadManagerTests.loadTest(taskCount, delay, workerCount)) {
177 std::cerr << "\t\tThreadManager loadTest FAILED" << std::endl;
178 return 1;
179 }
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000180
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100181 std::cout << "\t\tThreadManager block test: worker count: " << workerCount
182 << " delay: " << delay << std::endl;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000183
James E. King, IIIdf899132016-11-12 15:16:30 -0500184 if (!threadManagerTests.blockTest(delay, workerCount)) {
185 std::cerr << "\t\tThreadManager blockTest FAILED" << std::endl;
186 return 1;
187 }
Marc Slemkoc7782972006-07-25 02:26:35 +0000188 }
189 }
190
Mark Sleef5f2be42006-09-05 21:05:31 +0000191 if (runAll || args[0].compare("thread-manager-benchmark") == 0) {
Marc Slemkoc7782972006-07-25 02:26:35 +0000192
193 std::cout << "ThreadManager benchmark tests..." << std::endl;
194
195 {
196
197 size_t minWorkerCount = 2;
198
James E. King, III7d211b82017-09-06 10:12:02 -0700199 size_t maxWorkerCount = 8;
Marc Slemkoc7782972006-07-25 02:26:35 +0000200
James E. King, III7d211b82017-09-06 10:12:02 -0700201 size_t tasksPerWorker = 100 * WEIGHT;
Marc Slemkoc7782972006-07-25 02:26:35 +0000202
James E. King, IIIdf899132016-11-12 15:16:30 -0500203 int64_t delay = 5LL;
Marc Slemkoc7782972006-07-25 02:26:35 +0000204
James E. King, III7d211b82017-09-06 10:12:02 -0700205 for (size_t workerCount = minWorkerCount; workerCount <= maxWorkerCount; workerCount *= 4) {
Marc Slemkoc7782972006-07-25 02:26:35 +0000206
David Reiss96d23882007-07-26 21:10:32 +0000207 size_t taskCount = workerCount * tasksPerWorker;
Marc Slemkoc7782972006-07-25 02:26:35 +0000208
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100209 std::cout << "\t\tThreadManager load test: worker count: " << workerCount
210 << " task count: " << taskCount << " delay: " << delay << std::endl;
Marc Slemkoc7782972006-07-25 02:26:35 +0000211
David Reiss96d23882007-07-26 21:10:32 +0000212 ThreadManagerTests threadManagerTests;
Marc Slemkoc7782972006-07-25 02:26:35 +0000213
James E. King, IIIdf899132016-11-12 15:16:30 -0500214 if (!threadManagerTests.loadTest(taskCount, delay, workerCount))
215 {
216 std::cerr << "\t\tThreadManager loadTest FAILED" << std::endl;
217 return 1;
218 }
Marc Slemkoc7782972006-07-25 02:26:35 +0000219 }
220 }
Marc Slemkod466b212006-07-20 00:04:18 +0000221 }
James E. King, IIIdf899132016-11-12 15:16:30 -0500222
223 std::cout << "ALL TESTS PASSED" << std::endl;
224 return 0;
Marc Slemko8a40a762006-07-19 17:46:50 +0000225}