blob: 1ebb9e0ca54bf1ca470e7f1f025cede28d72e42c [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 */
19
Roger Meier49ff8b12012-04-13 09:12:31 +000020#include <thrift/concurrency/ThreadManager.h>
21#include <thrift/concurrency/PlatformThreadFactory.h>
22#include <thrift/concurrency/Monitor.h>
23#include <thrift/concurrency/Util.h>
24#include <thrift/concurrency/Mutex.h>
25#include <thrift/protocol/TBinaryProtocol.h>
26#include <thrift/server/TSimpleServer.h>
27#include <thrift/server/TThreadPoolServer.h>
28#include <thrift/server/TThreadedServer.h>
29#include <thrift/server/TNonblockingServer.h>
30#include <thrift/transport/TServerSocket.h>
31#include <thrift/transport/TSocket.h>
Divya Thaluru808d1432017-08-06 16:36:36 -070032#include <thrift/transport/TNonblockingServerSocket.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000033#include <thrift/transport/TTransportUtils.h>
34#include <thrift/transport/TFileTransport.h>
Roger Meier33eaa0f2012-04-13 09:13:13 +000035#include <thrift/TLogging.h>
Mark Sleee02385b2007-06-09 01:21:16 +000036
37#include "Service.h"
38
39#include <boost/shared_ptr.hpp>
40
41#include <iostream>
42#include <set>
43#include <stdexcept>
44#include <sstream>
Mark Sleee02385b2007-06-09 01:21:16 +000045#include <map>
Jake Farrell5d02b802014-01-07 21:42:01 -050046#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010047#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050048#endif
Mark Sleee02385b2007-06-09 01:21:16 +000049
50using namespace std;
Mark Sleee02385b2007-06-09 01:21:16 +000051
T Jake Lucianib5e62212009-01-31 22:36:20 +000052using namespace apache::thrift;
53using namespace apache::thrift::protocol;
54using namespace apache::thrift::transport;
55using namespace apache::thrift::server;
56using namespace apache::thrift::concurrency;
Mark Sleee02385b2007-06-09 01:21:16 +000057
58using namespace test::stress;
59
60struct eqstr {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010061 bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) == 0; }
Mark Sleee02385b2007-06-09 01:21:16 +000062};
63
64struct ltstr {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065 bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; }
Mark Sleee02385b2007-06-09 01:21:16 +000066};
67
Mark Sleee02385b2007-06-09 01:21:16 +000068// typedef hash_map<const char*, int, hash<const char*>, eqstr> count_map;
69typedef map<const char*, int, ltstr> count_map;
70
71class Server : public ServiceIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010072public:
Mark Sleee02385b2007-06-09 01:21:16 +000073 Server() {}
74
75 void count(const char* method) {
Mark Slee79b16942007-11-26 19:05:29 +000076 Guard m(lock_);
Mark Sleee02385b2007-06-09 01:21:16 +000077 int ct = counts_[method];
78 counts_[method] = ++ct;
79 }
80
81 void echoVoid() {
82 count("echoVoid");
Mark Slee3e5d2d72007-06-15 01:45:56 +000083 // Sleep to simulate work
Jake Farrell5d02b802014-01-07 21:42:01 -050084 THRIFT_SLEEP_USEC(1);
Mark Sleee02385b2007-06-09 01:21:16 +000085 return;
86 }
87
88 count_map getCount() {
Mark Slee79b16942007-11-26 19:05:29 +000089 Guard m(lock_);
Mark Sleee02385b2007-06-09 01:21:16 +000090 return counts_;
91 }
92
Konrad Grochowski16a23a62014-11-13 15:33:38 +010093 int8_t echoByte(const int8_t arg) { return arg; }
94 int32_t echoI32(const int32_t arg) { return arg; }
95 int64_t echoI64(const int64_t arg) { return arg; }
96 void echoString(string& out, const string& arg) {
Mark Sleee02385b2007-06-09 01:21:16 +000097 if (arg != "hello") {
Roger Meiera8cef6e2011-07-17 18:55:59 +000098 T_ERROR_ABORT("WRONG STRING (%s)!!!!", arg.c_str());
Mark Sleee02385b2007-06-09 01:21:16 +000099 }
100 out = arg;
101 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100102 void echoList(vector<int8_t>& out, const vector<int8_t>& arg) { out = arg; }
103 void echoSet(set<int8_t>& out, const set<int8_t>& arg) { out = arg; }
104 void echoMap(map<int8_t, int8_t>& out, const map<int8_t, int8_t>& arg) { out = arg; }
Mark Sleee02385b2007-06-09 01:21:16 +0000105
106private:
107 count_map counts_;
108 Mutex lock_;
Mark Sleee02385b2007-06-09 01:21:16 +0000109};
110
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111class ClientThread : public Runnable {
Mark Sleee02385b2007-06-09 01:21:16 +0000112public:
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100113 ClientThread(boost::shared_ptr<TTransport> transport,
114 boost::shared_ptr<ServiceClient> client,
115 Monitor& monitor,
116 size_t& workerCount,
117 size_t loopCount,
118 TType loopType)
119 : _transport(transport),
120 _client(client),
121 _monitor(monitor),
122 _workerCount(workerCount),
123 _loopCount(loopCount),
124 _loopType(loopType) {}
Mark Sleee02385b2007-06-09 01:21:16 +0000125
126 void run() {
127
128 // Wait for all worker threads to start
129
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100130 {
131 Synchronized s(_monitor);
132 while (_workerCount == 0) {
133 _monitor.wait();
134 }
Mark Sleee02385b2007-06-09 01:21:16 +0000135 }
136
137 _startTime = Util::currentTime();
138
139 _transport->open();
140
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100141 switch (_loopType) {
142 case T_VOID:
143 loopEchoVoid();
144 break;
145 case T_BYTE:
146 loopEchoByte();
147 break;
148 case T_I32:
149 loopEchoI32();
150 break;
151 case T_I64:
152 loopEchoI64();
153 break;
154 case T_STRING:
155 loopEchoString();
156 break;
157 default:
158 cerr << "Unexpected loop type" << _loopType << endl;
159 break;
Mark Sleee02385b2007-06-09 01:21:16 +0000160 }
161
162 _endTime = Util::currentTime();
163
164 _transport->close();
165
166 _done = true;
167
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100168 {
169 Synchronized s(_monitor);
Mark Sleee02385b2007-06-09 01:21:16 +0000170
171 _workerCount--;
172
Mark Slee3e5d2d72007-06-15 01:45:56 +0000173 if (_workerCount == 0) {
Mark Sleee02385b2007-06-09 01:21:16 +0000174
175 _monitor.notify();
176 }
177 }
178 }
179
180 void loopEchoVoid() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000181 for (size_t ix = 0; ix < _loopCount; ix++) {
Mark Sleee02385b2007-06-09 01:21:16 +0000182 _client->echoVoid();
183 }
184 }
185
186 void loopEchoByte() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000187 for (size_t ix = 0; ix < _loopCount; ix++) {
Mark Sleee02385b2007-06-09 01:21:16 +0000188 int8_t arg = 1;
189 int8_t result;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100190 result = _client->echoByte(arg);
Jake Farrell5d02b802014-01-07 21:42:01 -0500191 (void)result;
Mark Sleee02385b2007-06-09 01:21:16 +0000192 assert(result == arg);
193 }
194 }
195
196 void loopEchoI32() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000197 for (size_t ix = 0; ix < _loopCount; ix++) {
Mark Sleee02385b2007-06-09 01:21:16 +0000198 int32_t arg = 1;
199 int32_t result;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100200 result = _client->echoI32(arg);
Jake Farrell5d02b802014-01-07 21:42:01 -0500201 (void)result;
Mark Sleee02385b2007-06-09 01:21:16 +0000202 assert(result == arg);
203 }
204 }
205
206 void loopEchoI64() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000207 for (size_t ix = 0; ix < _loopCount; ix++) {
Mark Sleee02385b2007-06-09 01:21:16 +0000208 int64_t arg = 1;
209 int64_t result;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100210 result = _client->echoI64(arg);
Jake Farrell5d02b802014-01-07 21:42:01 -0500211 (void)result;
Mark Sleee02385b2007-06-09 01:21:16 +0000212 assert(result == arg);
213 }
214 }
215
216 void loopEchoString() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000217 for (size_t ix = 0; ix < _loopCount; ix++) {
Mark Sleee02385b2007-06-09 01:21:16 +0000218 string arg = "hello";
219 string result;
220 _client->echoString(result, arg);
221 assert(result == arg);
222 }
223 }
224
Roger Meier611f90c2011-12-11 22:08:51 +0000225 boost::shared_ptr<TTransport> _transport;
226 boost::shared_ptr<ServiceClient> _client;
Mark Sleee02385b2007-06-09 01:21:16 +0000227 Monitor& _monitor;
228 size_t& _workerCount;
229 size_t _loopCount;
230 TType _loopType;
Roger Meier5f9614c2010-11-21 16:59:05 +0000231 int64_t _startTime;
232 int64_t _endTime;
Mark Sleee02385b2007-06-09 01:21:16 +0000233 bool _done;
234 Monitor _sleep;
235};
236
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100237int main(int argc, char** argv) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500238#if _WIN32
239 transport::TWinsockSingleton::create();
240#endif
Mark Sleee02385b2007-06-09 01:21:16 +0000241
242 int port = 9091;
243 string serverType = "simple";
244 string protocolType = "binary";
Jake Farrell5d02b802014-01-07 21:42:01 -0500245 uint32_t workerCount = 4;
246 uint32_t clientCount = 20;
247 uint32_t loopCount = 1000;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100248 TType loopType = T_VOID;
Mark Sleee02385b2007-06-09 01:21:16 +0000249 string callName = "echoVoid";
250 bool runServer = true;
251 bool logRequests = false;
252 string requestLogPath = "./requestlog.tlog";
253 bool replayRequests = false;
254
255 ostringstream usage;
256
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100257 usage << argv[0] << " [--port=<port number>] [--server] [--server-type=<server-type>] "
258 "[--protocol-type=<protocol-type>] [--workers=<worker-count>] "
259 "[--clients=<client-count>] [--loop=<loop-count>]" << endl
260 << "\tclients Number of client threads to create - 0 implies no clients, i.e. "
261 "server only. Default is " << clientCount << endl
262 << "\thelp Prints this help text." << endl
263 << "\tcall Service method to call. Default is " << callName << endl
264 << "\tloop The number of remote thrift calls each client makes. Default is "
265 << loopCount << endl << "\tport The port the server and clients should bind to "
266 "for thrift network connections. Default is " << port << endl
267 << "\tserver Run the Thrift server in this process. Default is " << runServer
268 << endl << "\tserver-type Type of server, \"simple\" or \"thread-pool\". Default is "
269 << serverType << endl
270 << "\tprotocol-type Type of protocol, \"binary\", \"ascii\", or \"xml\". Default is "
271 << protocolType << endl
272 << "\tlog-request Log all request to ./requestlog.tlog. Default is " << logRequests
273 << endl << "\treplay-request Replay requests from log file (./requestlog.tlog) Default is "
274 << replayRequests << endl << "\tworkers Number of thread pools workers. Only valid "
275 "for thread-pool server type. Default is " << workerCount
276 << endl;
Mark Sleee02385b2007-06-09 01:21:16 +0000277
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100278 map<string, string> args;
Mark Sleee02385b2007-06-09 01:21:16 +0000279
Mark Slee3e5d2d72007-06-15 01:45:56 +0000280 for (int ix = 1; ix < argc; ix++) {
Mark Sleee02385b2007-06-09 01:21:16 +0000281
282 string arg(argv[ix]);
283
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100284 if (arg.compare(0, 2, "--") == 0) {
Mark Sleee02385b2007-06-09 01:21:16 +0000285
286 size_t end = arg.find_first_of("=", 2);
287
288 string key = string(arg, 2, end - 2);
289
Mark Slee3e5d2d72007-06-15 01:45:56 +0000290 if (end != string::npos) {
Mark Sleee02385b2007-06-09 01:21:16 +0000291 args[key] = string(arg, end + 1);
292 } else {
293 args[key] = "true";
294 }
295 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100296 throw invalid_argument("Unexcepted command line token: " + arg);
Mark Sleee02385b2007-06-09 01:21:16 +0000297 }
298 }
299
300 try {
301
Mark Slee3e5d2d72007-06-15 01:45:56 +0000302 if (!args["clients"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000303 clientCount = atoi(args["clients"].c_str());
304 }
305
Mark Slee3e5d2d72007-06-15 01:45:56 +0000306 if (!args["help"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000307 cerr << usage.str();
308 return 0;
309 }
310
Mark Slee3e5d2d72007-06-15 01:45:56 +0000311 if (!args["loop"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000312 loopCount = atoi(args["loop"].c_str());
313 }
314
Mark Slee3e5d2d72007-06-15 01:45:56 +0000315 if (!args["call"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000316 callName = args["call"];
317 }
318
Mark Slee3e5d2d72007-06-15 01:45:56 +0000319 if (!args["port"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000320 port = atoi(args["port"].c_str());
321 }
322
Mark Slee3e5d2d72007-06-15 01:45:56 +0000323 if (!args["server"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000324 runServer = args["server"] == "true";
325 }
326
Mark Slee3e5d2d72007-06-15 01:45:56 +0000327 if (!args["log-request"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000328 logRequests = args["log-request"] == "true";
329 }
330
Mark Slee3e5d2d72007-06-15 01:45:56 +0000331 if (!args["replay-request"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000332 replayRequests = args["replay-request"] == "true";
333 }
334
Mark Slee3e5d2d72007-06-15 01:45:56 +0000335 if (!args["server-type"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000336 serverType = args["server-type"];
337 }
338
Mark Slee3e5d2d72007-06-15 01:45:56 +0000339 if (!args["workers"].empty()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000340 workerCount = atoi(args["workers"].c_str());
341 }
342
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100343 } catch (std::exception& e) {
Mark Sleee02385b2007-06-09 01:21:16 +0000344 cerr << e.what() << endl;
Jake Farrell5d02b802014-01-07 21:42:01 -0500345 cerr << usage.str();
Mark Sleee02385b2007-06-09 01:21:16 +0000346 }
347
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100348 boost::shared_ptr<PlatformThreadFactory> threadFactory
349 = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Mark Sleee02385b2007-06-09 01:21:16 +0000350
351 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000352 boost::shared_ptr<Server> serviceHandler(new Server());
Mark Sleee02385b2007-06-09 01:21:16 +0000353
354 if (replayRequests) {
Roger Meier611f90c2011-12-11 22:08:51 +0000355 boost::shared_ptr<Server> serviceHandler(new Server());
356 boost::shared_ptr<ServiceProcessor> serviceProcessor(new ServiceProcessor(serviceHandler));
Mark Sleee02385b2007-06-09 01:21:16 +0000357
358 // Transports
Roger Meier611f90c2011-12-11 22:08:51 +0000359 boost::shared_ptr<TFileTransport> fileTransport(new TFileTransport(requestLogPath));
Mark Sleee02385b2007-06-09 01:21:16 +0000360 fileTransport->setChunkSize(2 * 1024 * 1024);
361 fileTransport->setMaxEventSize(1024 * 16);
362 fileTransport->seekToEnd();
363
364 // Protocol Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000365 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Mark Sleee02385b2007-06-09 01:21:16 +0000366
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100367 TFileProcessor fileProcessor(serviceProcessor, protocolFactory, fileTransport);
Mark Sleee02385b2007-06-09 01:21:16 +0000368
369 fileProcessor.process(0, true);
370 exit(0);
371 }
372
Mark Slee3e5d2d72007-06-15 01:45:56 +0000373 if (runServer) {
Mark Sleee02385b2007-06-09 01:21:16 +0000374
Roger Meier611f90c2011-12-11 22:08:51 +0000375 boost::shared_ptr<ServiceProcessor> serviceProcessor(new ServiceProcessor(serviceHandler));
Mark Sleee02385b2007-06-09 01:21:16 +0000376
377 // Protocol Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000378 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Mark Sleee02385b2007-06-09 01:21:16 +0000379
380 // Transport Factory
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100381 boost::shared_ptr<TTransportFactory> transportFactory;
Mark Sleee02385b2007-06-09 01:21:16 +0000382
383 if (logRequests) {
384 // initialize the log file
Roger Meier611f90c2011-12-11 22:08:51 +0000385 boost::shared_ptr<TFileTransport> fileTransport(new TFileTransport(requestLogPath));
Mark Sleee02385b2007-06-09 01:21:16 +0000386 fileTransport->setChunkSize(2 * 1024 * 1024);
387 fileTransport->setMaxEventSize(1024 * 16);
388
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100389 transportFactory
390 = boost::shared_ptr<TTransportFactory>(new TPipedTransportFactory(fileTransport));
Mark Sleee02385b2007-06-09 01:21:16 +0000391 }
392
Roger Meier611f90c2011-12-11 22:08:51 +0000393 boost::shared_ptr<Thread> serverThread;
394 boost::shared_ptr<Thread> serverThread2;
Divya Thaluru808d1432017-08-06 16:36:36 -0700395 boost::shared_ptr<transport::TNonblockingServerSocket> nbSocket1, nbSocket2;
Mark Sleee02385b2007-06-09 01:21:16 +0000396
Mark Slee3e5d2d72007-06-15 01:45:56 +0000397 if (serverType == "simple") {
Divya Thaluru808d1432017-08-06 16:36:36 -0700398 nbSocket1.reset(new transport::TNonblockingServerSocket(port));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100399 serverThread = threadFactory->newThread(boost::shared_ptr<TServer>(
Divya Thaluru808d1432017-08-06 16:36:36 -0700400 new TNonblockingServer(serviceProcessor, protocolFactory, nbSocket1)));
401 nbSocket2.reset(new transport::TNonblockingServerSocket(port + 1));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100402 serverThread2 = threadFactory->newThread(boost::shared_ptr<TServer>(
Divya Thaluru808d1432017-08-06 16:36:36 -0700403 new TNonblockingServer(serviceProcessor, protocolFactory, nbSocket2)));
Mark Sleee02385b2007-06-09 01:21:16 +0000404
Mark Slee3e5d2d72007-06-15 01:45:56 +0000405 } else if (serverType == "thread-pool") {
Mark Sleee02385b2007-06-09 01:21:16 +0000406
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100407 boost::shared_ptr<ThreadManager> threadManager
408 = ThreadManager::newSimpleThreadManager(workerCount);
Mark Sleee02385b2007-06-09 01:21:16 +0000409
410 threadManager->threadFactory(threadFactory);
411 threadManager->start();
Divya Thaluru808d1432017-08-06 16:36:36 -0700412 nbSocket1.reset(new transport::TNonblockingServerSocket(port));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100413 serverThread = threadFactory->newThread(boost::shared_ptr<TServer>(
Divya Thaluru808d1432017-08-06 16:36:36 -0700414 new TNonblockingServer(serviceProcessor, protocolFactory, nbSocket1, threadManager)));
415 nbSocket2.reset(new transport::TNonblockingServerSocket(port + 1));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100416 serverThread2 = threadFactory->newThread(boost::shared_ptr<TServer>(
Divya Thaluru808d1432017-08-06 16:36:36 -0700417 new TNonblockingServer(serviceProcessor, protocolFactory, nbSocket2, threadManager)));
Mark Sleee02385b2007-06-09 01:21:16 +0000418 }
419
Mark Slee79b16942007-11-26 19:05:29 +0000420 cerr << "Starting the server on port " << port << " and " << (port + 1) << endl;
Mark Sleee02385b2007-06-09 01:21:16 +0000421 serverThread->start();
Mark Slee79b16942007-11-26 19:05:29 +0000422 serverThread2->start();
Mark Sleee02385b2007-06-09 01:21:16 +0000423
424 // If we aren't running clients, just wait forever for external clients
425
426 if (clientCount == 0) {
427 serverThread->join();
Mark Slee79b16942007-11-26 19:05:29 +0000428 serverThread2->join();
Mark Sleee02385b2007-06-09 01:21:16 +0000429 }
430 }
Jake Farrell5d02b802014-01-07 21:42:01 -0500431 THRIFT_SLEEP_SEC(1);
Mark Sleee02385b2007-06-09 01:21:16 +0000432
433 if (clientCount > 0) {
434
435 Monitor monitor;
436
437 size_t threadCount = 0;
438
Roger Meier611f90c2011-12-11 22:08:51 +0000439 set<boost::shared_ptr<Thread> > clientThreads;
Mark Sleee02385b2007-06-09 01:21:16 +0000440
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100441 if (callName == "echoVoid") {
442 loopType = T_VOID;
443 } else if (callName == "echoByte") {
444 loopType = T_BYTE;
445 } else if (callName == "echoI32") {
446 loopType = T_I32;
447 } else if (callName == "echoI64") {
448 loopType = T_I64;
449 } else if (callName == "echoString") {
450 loopType = T_STRING;
451 } else {
452 throw invalid_argument("Unknown service call " + callName);
453 }
Mark Sleee02385b2007-06-09 01:21:16 +0000454
Jake Farrell5d02b802014-01-07 21:42:01 -0500455 for (uint32_t ix = 0; ix < clientCount; ix++) {
Mark Sleee02385b2007-06-09 01:21:16 +0000456
Roger Meier611f90c2011-12-11 22:08:51 +0000457 boost::shared_ptr<TSocket> socket(new TSocket("127.0.0.1", port + (ix % 2)));
458 boost::shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket));
459 boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(framedSocket));
460 boost::shared_ptr<ServiceClient> serviceClient(new ServiceClient(protocol));
Mark Sleee02385b2007-06-09 01:21:16 +0000461
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100462 clientThreads.insert(threadFactory->newThread(boost::shared_ptr<ClientThread>(
463 new ClientThread(socket, serviceClient, monitor, threadCount, loopCount, loopType))));
Mark Sleee02385b2007-06-09 01:21:16 +0000464 }
465
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100466 for (std::set<boost::shared_ptr<Thread> >::const_iterator thread = clientThreads.begin();
467 thread != clientThreads.end();
468 thread++) {
Mark Sleee02385b2007-06-09 01:21:16 +0000469 (*thread)->start();
470 }
471
Roger Meier5f9614c2010-11-21 16:59:05 +0000472 int64_t time00;
473 int64_t time01;
Mark Sleee02385b2007-06-09 01:21:16 +0000474
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100475 {
476 Synchronized s(monitor);
Mark Sleee02385b2007-06-09 01:21:16 +0000477 threadCount = clientCount;
478
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100479 cerr << "Launch " << clientCount << " client threads" << endl;
Mark Sleee02385b2007-06-09 01:21:16 +0000480
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100481 time00 = Util::currentTime();
Mark Sleee02385b2007-06-09 01:21:16 +0000482
483 monitor.notifyAll();
484
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100485 while (threadCount > 0) {
Mark Sleee02385b2007-06-09 01:21:16 +0000486 monitor.wait();
487 }
488
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100489 time01 = Util::currentTime();
Mark Sleee02385b2007-06-09 01:21:16 +0000490 }
491
Roger Meier5f9614c2010-11-21 16:59:05 +0000492 int64_t firstTime = 9223372036854775807LL;
493 int64_t lastTime = 0;
Mark Sleee02385b2007-06-09 01:21:16 +0000494
495 double averageTime = 0;
Roger Meier5f9614c2010-11-21 16:59:05 +0000496 int64_t minTime = 9223372036854775807LL;
497 int64_t maxTime = 0;
Mark Sleee02385b2007-06-09 01:21:16 +0000498
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100499 for (set<boost::shared_ptr<Thread> >::iterator ix = clientThreads.begin();
500 ix != clientThreads.end();
501 ix++) {
Mark Sleee02385b2007-06-09 01:21:16 +0000502
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100503 boost::shared_ptr<ClientThread> client
504 = boost::dynamic_pointer_cast<ClientThread>((*ix)->runnable());
Mark Sleee02385b2007-06-09 01:21:16 +0000505
Roger Meier5f9614c2010-11-21 16:59:05 +0000506 int64_t delta = client->_endTime - client->_startTime;
Mark Sleee02385b2007-06-09 01:21:16 +0000507
508 assert(delta > 0);
509
Mark Slee3e5d2d72007-06-15 01:45:56 +0000510 if (client->_startTime < firstTime) {
Mark Sleee02385b2007-06-09 01:21:16 +0000511 firstTime = client->_startTime;
512 }
513
Mark Slee3e5d2d72007-06-15 01:45:56 +0000514 if (client->_endTime > lastTime) {
Mark Sleee02385b2007-06-09 01:21:16 +0000515 lastTime = client->_endTime;
516 }
517
Mark Slee3e5d2d72007-06-15 01:45:56 +0000518 if (delta < minTime) {
Mark Sleee02385b2007-06-09 01:21:16 +0000519 minTime = delta;
520 }
521
Mark Slee3e5d2d72007-06-15 01:45:56 +0000522 if (delta > maxTime) {
Mark Sleee02385b2007-06-09 01:21:16 +0000523 maxTime = delta;
524 }
525
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100526 averageTime += delta;
Mark Sleee02385b2007-06-09 01:21:16 +0000527 }
528
529 averageTime /= clientCount;
530
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100531 cout << "workers :" << workerCount << ", client : " << clientCount << ", loops : " << loopCount
532 << ", rate : " << (clientCount * loopCount * 1000) / ((double)(time01 - time00)) << endl;
Mark Sleee02385b2007-06-09 01:21:16 +0000533
534 count_map count = serviceHandler->getCount();
535 count_map::iterator iter;
536 for (iter = count.begin(); iter != count.end(); ++iter) {
537 printf("%s => %d\n", iter->first, iter->second);
538 }
539 cerr << "done." << endl;
540 }
541
542 return 0;
543}