blob: 5ff5e44f95dd2394e70ced2bb6c2b7318915383e [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/transport/TServerSocket.h>
30#include <thrift/transport/TSocket.h>
31#include <thrift/transport/TTransportUtils.h>
32#include <thrift/transport/TFileTransport.h>
Roger Meier33eaa0f2012-04-13 09:13:13 +000033#include <thrift/TLogging.h>
James E. King, III82ae9572017-08-05 12:23:54 -040034#include <thrift/stdcxx.h>
Aditya Agarwal3950f472006-10-11 02:50:15 +000035
36#include "Service.h"
Marc Slemko3ea00332006-08-17 01:11:13 +000037#include <iostream>
38#include <set>
39#include <stdexcept>
40#include <sstream>
Mark Sleeb9ff32a2006-11-16 01:00:24 +000041#include <map>
Jake Farrell5d02b802014-01-07 21:42:01 -050042#if _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010043#include <thrift/windows/TWinsockSingleton.h>
Jake Farrell5d02b802014-01-07 21:42:01 -050044#endif
Mark Sleeb9ff32a2006-11-16 01:00:24 +000045
Marc Slemko3ea00332006-08-17 01:11:13 +000046using namespace std;
47
T Jake Lucianib5e62212009-01-31 22:36:20 +000048using namespace apache::thrift;
49using namespace apache::thrift::protocol;
50using namespace apache::thrift::transport;
51using namespace apache::thrift::server;
52using namespace apache::thrift::concurrency;
Marc Slemko3ea00332006-08-17 01:11:13 +000053
54using namespace test::stress;
55
Mark Sleeb9ff32a2006-11-16 01:00:24 +000056struct eqstr {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010057 bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) == 0; }
Mark Sleeb9ff32a2006-11-16 01:00:24 +000058};
59
60struct ltstr {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010061 bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; }
Mark Sleeb9ff32a2006-11-16 01:00:24 +000062};
63
David Reiss0c90f6f2008-02-06 22:18:40 +000064// typedef hash_map<const char*, int, hash<const char*>, eqstr> count_map;
65typedef map<const char*, int, ltstr> count_map;
Mark Sleeb9ff32a2006-11-16 01:00:24 +000066
Aditya Agarwal3950f472006-10-11 02:50:15 +000067class Server : public ServiceIf {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010068public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000069 Server() {}
70
71 void count(const char* method) {
David Reiss46379902008-02-04 19:23:57 +000072 Guard m(lock_);
Mark Sleeb9ff32a2006-11-16 01:00:24 +000073 int ct = counts_[method];
74 counts_[method] = ++ct;
75 }
76
77 void echoVoid() {
78 count("echoVoid");
79 return;
80 }
81
82 count_map getCount() {
David Reiss46379902008-02-04 19:23:57 +000083 Guard m(lock_);
Mark Sleeb9ff32a2006-11-16 01:00:24 +000084 return counts_;
85 }
86
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087 int8_t echoByte(const int8_t arg) { return arg; }
88 int32_t echoI32(const int32_t arg) { return arg; }
89 int64_t echoI64(const int64_t arg) { return arg; }
90 void echoString(string& out, const string& arg) {
Aditya Agarwale9ef8d72006-12-08 23:52:57 +000091 if (arg != "hello") {
Roger Meiera8cef6e2011-07-17 18:55:59 +000092 T_ERROR_ABORT("WRONG STRING (%s)!!!!", arg.c_str());
Aditya Agarwale9ef8d72006-12-08 23:52:57 +000093 }
Mark Slee739dbe52007-02-01 22:55:30 +000094 out = arg;
Aditya Agarwale9ef8d72006-12-08 23:52:57 +000095 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +010096 void echoList(vector<int8_t>& out, const vector<int8_t>& arg) { out = arg; }
97 void echoSet(set<int8_t>& out, const set<int8_t>& arg) { out = arg; }
98 void echoMap(map<int8_t, int8_t>& out, const map<int8_t, int8_t>& arg) { out = arg; }
Mark Sleeb9ff32a2006-11-16 01:00:24 +000099
100private:
101 count_map counts_;
102 Mutex lock_;
Marc Slemko3ea00332006-08-17 01:11:13 +0000103};
104
ben-craig02bade12015-07-17 08:40:48 -0500105enum TransportOpenCloseBehavior {
106 OpenAndCloseTransportInThread,
107 DontOpenAndCloseTransportInThread
108};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100109class ClientThread : public Runnable {
Marc Slemko3ea00332006-08-17 01:11:13 +0000110public:
James E. King, III82ae9572017-08-05 12:23:54 -0400111 ClientThread(stdcxx::shared_ptr<TTransport> transport,
112 stdcxx::shared_ptr<ServiceIf> client,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100113 Monitor& monitor,
114 size_t& workerCount,
115 size_t loopCount,
ben-craig02bade12015-07-17 08:40:48 -0500116 TType loopType,
117 TransportOpenCloseBehavior behavior)
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100118 : _transport(transport),
119 _client(client),
120 _monitor(monitor),
121 _workerCount(workerCount),
122 _loopCount(loopCount),
ben-craig02bade12015-07-17 08:40:48 -0500123 _loopType(loopType),
124 _behavior(behavior) {}
Marc Slemko3ea00332006-08-17 01:11:13 +0000125
126 void run() {
127
David Reiss0c90f6f2008-02-06 22:18:40 +0000128 // Wait for all worker threads to start
Marc Slemko3ea00332006-08-17 01:11:13 +0000129
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100130 {
131 Synchronized s(_monitor);
132 while (_workerCount == 0) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000133 _monitor.wait();
134 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000135 }
136
137 _startTime = Util::currentTime();
ben-craig02bade12015-07-17 08:40:48 -0500138 if(_behavior == OpenAndCloseTransportInThread) {
139 _transport->open();
140 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000141
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100142 switch (_loopType) {
143 case T_VOID:
144 loopEchoVoid();
145 break;
146 case T_BYTE:
147 loopEchoByte();
148 break;
149 case T_I32:
150 loopEchoI32();
151 break;
152 case T_I64:
153 loopEchoI64();
154 break;
155 case T_STRING:
156 loopEchoString();
157 break;
158 default:
159 cerr << "Unexpected loop type" << _loopType << endl;
160 break;
Marc Slemko3ea00332006-08-17 01:11:13 +0000161 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000162
Marc Slemko3ea00332006-08-17 01:11:13 +0000163 _endTime = Util::currentTime();
164
ben-craig02bade12015-07-17 08:40:48 -0500165 if(_behavior == OpenAndCloseTransportInThread) {
166 _transport->close();
167 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000168
Marc Slemko3ea00332006-08-17 01:11:13 +0000169 _done = true;
David Reiss0c90f6f2008-02-06 22:18:40 +0000170
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100171 {
172 Synchronized s(_monitor);
Marc Slemko3ea00332006-08-17 01:11:13 +0000173
174 _workerCount--;
David Reiss0c90f6f2008-02-06 22:18:40 +0000175
Mark Slee3e5d2d72007-06-15 01:45:56 +0000176 if (_workerCount == 0) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000177
178 _monitor.notify();
Marc Slemko3ea00332006-08-17 01:11:13 +0000179 }
180 }
181 }
Marc Slemkod97eb612006-08-24 23:37:36 +0000182
183 void loopEchoVoid() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000184 for (size_t ix = 0; ix < _loopCount; ix++) {
Marc Slemkod97eb612006-08-24 23:37:36 +0000185 _client->echoVoid();
186 }
187 }
188
189 void loopEchoByte() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000190 for (size_t ix = 0; ix < _loopCount; ix++) {
Aditya Agarwal3950f472006-10-11 02:50:15 +0000191 int8_t arg = 1;
192 int8_t result;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100193 result = _client->echoByte(arg);
Jake Farrell5d02b802014-01-07 21:42:01 -0500194 (void)result;
Marc Slemkod97eb612006-08-24 23:37:36 +0000195 assert(result == arg);
196 }
197 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000198
Marc Slemkod97eb612006-08-24 23:37:36 +0000199 void loopEchoI32() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000200 for (size_t ix = 0; ix < _loopCount; ix++) {
Aditya Agarwal3950f472006-10-11 02:50:15 +0000201 int32_t arg = 1;
202 int32_t result;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100203 result = _client->echoI32(arg);
Jake Farrell5d02b802014-01-07 21:42:01 -0500204 (void)result;
Marc Slemkod97eb612006-08-24 23:37:36 +0000205 assert(result == arg);
206 }
207 }
208
209 void loopEchoI64() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000210 for (size_t ix = 0; ix < _loopCount; ix++) {
Aditya Agarwal3950f472006-10-11 02:50:15 +0000211 int64_t arg = 1;
212 int64_t result;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100213 result = _client->echoI64(arg);
Jake Farrell5d02b802014-01-07 21:42:01 -0500214 (void)result;
Marc Slemkod97eb612006-08-24 23:37:36 +0000215 assert(result == arg);
216 }
217 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000218
Marc Slemkod97eb612006-08-24 23:37:36 +0000219 void loopEchoString() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000220 for (size_t ix = 0; ix < _loopCount; ix++) {
Marc Slemkod97eb612006-08-24 23:37:36 +0000221 string arg = "hello";
222 string result;
Mark Slee739dbe52007-02-01 22:55:30 +0000223 _client->echoString(result, arg);
Marc Slemkod97eb612006-08-24 23:37:36 +0000224 assert(result == arg);
225 }
226 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000227
James E. King, III82ae9572017-08-05 12:23:54 -0400228 stdcxx::shared_ptr<TTransport> _transport;
229 stdcxx::shared_ptr<ServiceIf> _client;
Marc Slemko3ea00332006-08-17 01:11:13 +0000230 Monitor& _monitor;
231 size_t& _workerCount;
232 size_t _loopCount;
Marc Slemkod97eb612006-08-24 23:37:36 +0000233 TType _loopType;
Roger Meier5f9614c2010-11-21 16:59:05 +0000234 int64_t _startTime;
235 int64_t _endTime;
Marc Slemko3ea00332006-08-17 01:11:13 +0000236 bool _done;
237 Monitor _sleep;
ben-craig02bade12015-07-17 08:40:48 -0500238 TransportOpenCloseBehavior _behavior;
Marc Slemko3ea00332006-08-17 01:11:13 +0000239};
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000240
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100241class TStartObserver : public apache::thrift::server::TServerEventHandler {
Jake Farrell5d02b802014-01-07 21:42:01 -0500242public:
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100243 TStartObserver() : awake_(false) {}
244 virtual void preServe() {
245 apache::thrift::concurrency::Synchronized s(m_);
246 awake_ = true;
247 m_.notifyAll();
248 }
249 void waitForService() {
250 apache::thrift::concurrency::Synchronized s(m_);
251 while (!awake_)
252 m_.waitForever();
253 }
254
255private:
256 apache::thrift::concurrency::Monitor m_;
257 bool awake_;
Jake Farrell5d02b802014-01-07 21:42:01 -0500258};
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000259
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100260int main(int argc, char** argv) {
Jake Farrell5d02b802014-01-07 21:42:01 -0500261#if _WIN32
262 transport::TWinsockSingleton::create();
263#endif
Marc Slemko3ea00332006-08-17 01:11:13 +0000264
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000265 int port = 9091;
ben-craig02bade12015-07-17 08:40:48 -0500266 string clientType = "regular";
Marc Slemko3ea00332006-08-17 01:11:13 +0000267 string serverType = "thread-pool";
268 string protocolType = "binary";
269 size_t workerCount = 4;
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000270 size_t clientCount = 20;
271 size_t loopCount = 50000;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100272 TType loopType = T_VOID;
Marc Slemkod97eb612006-08-24 23:37:36 +0000273 string callName = "echoVoid";
Marc Slemkob09f5882006-08-23 22:03:34 +0000274 bool runServer = true;
Aditya Agarwal3950f472006-10-11 02:50:15 +0000275 bool logRequests = false;
276 string requestLogPath = "./requestlog.tlog";
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000277 bool replayRequests = false;
Marc Slemko3ea00332006-08-17 01:11:13 +0000278
Marc Slemkob09f5882006-08-23 22:03:34 +0000279 ostringstream usage;
Marc Slemko3ea00332006-08-17 01:11:13 +0000280
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100281 usage << argv[0] << " [--port=<port number>] [--server] [--server-type=<server-type>] "
282 "[--protocol-type=<protocol-type>] [--workers=<worker-count>] "
ben-craig02bade12015-07-17 08:40:48 -0500283 "[--clients=<client-count>] [--loop=<loop-count>] "
284 "[--client-type=<client-type>]" << endl
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100285 << "\tclients Number of client threads to create - 0 implies no clients, i.e. "
ben-craig02bade12015-07-17 08:40:48 -0500286 "server only. Default is " << clientCount << endl
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100287 << "\thelp Prints this help text." << endl
288 << "\tcall Service method to call. Default is " << callName << endl
James E. King, III82ae9572017-08-05 12:23:54 -0400289 << "\tloop The number of remote thrift calls each client makes. Default is " << loopCount << endl
ben-craig02bade12015-07-17 08:40:48 -0500290 << "\tport The port the server and clients should bind to "
291 "for thrift network connections. Default is " << port << endl
James E. King, III82ae9572017-08-05 12:23:54 -0400292 << "\tserver Run the Thrift server in this process. Default is " << runServer << endl
ben-craig02bade12015-07-17 08:40:48 -0500293 << "\tserver-type Type of server, \"simple\" or \"thread-pool\". Default is " << serverType << endl
294 << "\tprotocol-type Type of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl
James E. King, III82ae9572017-08-05 12:23:54 -0400295 << "\tlog-request Log all request to ./requestlog.tlog. Default is " << logRequests << endl
296 << "\treplay-request Replay requests from log file (./requestlog.tlog) Default is " << replayRequests << endl
ben-craig02bade12015-07-17 08:40:48 -0500297 << "\tworkers Number of thread pools workers. Only valid "
298 "for thread-pool server type. Default is " << workerCount << endl
299 << "\tclient-type Type of client, \"regular\" or \"concurrent\". Default is " << clientType << endl
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100300 << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +0000301
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100302 map<string, string> args;
David Reiss0c90f6f2008-02-06 22:18:40 +0000303
Mark Slee3e5d2d72007-06-15 01:45:56 +0000304 for (int ix = 1; ix < argc; ix++) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000305
306 string arg(argv[ix]);
307
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100308 if (arg.compare(0, 2, "--") == 0) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000309
310 size_t end = arg.find_first_of("=", 2);
311
Marc Slemko056f9ba2006-08-17 02:59:05 +0000312 string key = string(arg, 2, end - 2);
313
Mark Slee3e5d2d72007-06-15 01:45:56 +0000314 if (end != string::npos) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000315 args[key] = string(arg, end + 1);
Marc Slemko3ea00332006-08-17 01:11:13 +0000316 } else {
David Reiss0c90f6f2008-02-06 22:18:40 +0000317 args[key] = "true";
Marc Slemko3ea00332006-08-17 01:11:13 +0000318 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000319 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100320 throw invalid_argument("Unexcepted command line token: " + arg);
Marc Slemko3ea00332006-08-17 01:11:13 +0000321 }
322 }
323
324 try {
325
Mark Slee3e5d2d72007-06-15 01:45:56 +0000326 if (!args["clients"].empty()) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000327 clientCount = atoi(args["clients"].c_str());
328 }
329
Mark Slee3e5d2d72007-06-15 01:45:56 +0000330 if (!args["help"].empty()) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000331 cerr << usage.str();
332 return 0;
333 }
334
Mark Slee3e5d2d72007-06-15 01:45:56 +0000335 if (!args["loop"].empty()) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000336 loopCount = atoi(args["loop"].c_str());
337 }
338
Mark Slee3e5d2d72007-06-15 01:45:56 +0000339 if (!args["call"].empty()) {
Marc Slemkod97eb612006-08-24 23:37:36 +0000340 callName = args["call"];
341 }
342
Mark Slee3e5d2d72007-06-15 01:45:56 +0000343 if (!args["port"].empty()) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000344 port = atoi(args["port"].c_str());
345 }
346
Mark Slee3e5d2d72007-06-15 01:45:56 +0000347 if (!args["server"].empty()) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000348 runServer = args["server"] == "true";
349 }
350
Mark Slee3e5d2d72007-06-15 01:45:56 +0000351 if (!args["log-request"].empty()) {
Aditya Agarwal3950f472006-10-11 02:50:15 +0000352 logRequests = args["log-request"] == "true";
353 }
354
Mark Slee3e5d2d72007-06-15 01:45:56 +0000355 if (!args["replay-request"].empty()) {
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000356 replayRequests = args["replay-request"] == "true";
357 }
358
Mark Slee3e5d2d72007-06-15 01:45:56 +0000359 if (!args["server-type"].empty()) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000360 serverType = args["server-type"];
David Reiss0c90f6f2008-02-06 22:18:40 +0000361
Mark Slee3e5d2d72007-06-15 01:45:56 +0000362 if (serverType == "simple") {
Marc Slemko3ea00332006-08-17 01:11:13 +0000363
Mark Slee3e5d2d72007-06-15 01:45:56 +0000364 } else if (serverType == "thread-pool") {
Marc Slemko3ea00332006-08-17 01:11:13 +0000365
Mark Slee3e5d2d72007-06-15 01:45:56 +0000366 } else if (serverType == "threaded") {
Mark Slee739dbe52007-02-01 22:55:30 +0000367
Marc Slemko3ea00332006-08-17 01:11:13 +0000368 } else {
369
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100370 throw invalid_argument("Unknown server type " + serverType);
Marc Slemko3ea00332006-08-17 01:11:13 +0000371 }
372 }
ben-craig02bade12015-07-17 08:40:48 -0500373 if (!args["client-type"].empty()) {
374 clientType = args["client-type"];
Marc Slemko3ea00332006-08-17 01:11:13 +0000375
ben-craig02bade12015-07-17 08:40:48 -0500376 if (clientType == "regular") {
377
378 } else if (clientType == "concurrent") {
379
380 } else {
381
382 throw invalid_argument("Unknown client type " + clientType);
383 }
384 }
Mark Slee3e5d2d72007-06-15 01:45:56 +0000385 if (!args["workers"].empty()) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000386 workerCount = atoi(args["workers"].c_str());
387 }
388
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100389 } catch (std::exception& e) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000390 cerr << e.what() << endl;
Jake Farrell5d02b802014-01-07 21:42:01 -0500391 cerr << usage.str();
Marc Slemko3ea00332006-08-17 01:11:13 +0000392 }
393
James E. King, III82ae9572017-08-05 12:23:54 -0400394 stdcxx::shared_ptr<PlatformThreadFactory> threadFactory
395 = stdcxx::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko3ea00332006-08-17 01:11:13 +0000396
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000397 // Dispatcher
James E. King, III82ae9572017-08-05 12:23:54 -0400398 stdcxx::shared_ptr<Server> serviceHandler(new Server());
Marc Slemko3ea00332006-08-17 01:11:13 +0000399
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000400 if (replayRequests) {
James E. King, III82ae9572017-08-05 12:23:54 -0400401 stdcxx::shared_ptr<Server> serviceHandler(new Server());
402 stdcxx::shared_ptr<ServiceProcessor> serviceProcessor(new ServiceProcessor(serviceHandler));
David Reiss0c90f6f2008-02-06 22:18:40 +0000403
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000404 // Transports
James E. King, III82ae9572017-08-05 12:23:54 -0400405 stdcxx::shared_ptr<TFileTransport> fileTransport(new TFileTransport(requestLogPath));
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000406 fileTransport->setChunkSize(2 * 1024 * 1024);
407 fileTransport->setMaxEventSize(1024 * 16);
408 fileTransport->seekToEnd();
409
410 // Protocol Factory
James E. King, III82ae9572017-08-05 12:23:54 -0400411 stdcxx::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000412
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100413 TFileProcessor fileProcessor(serviceProcessor, protocolFactory, fileTransport);
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000414
David Reiss0c90f6f2008-02-06 22:18:40 +0000415 fileProcessor.process(0, true);
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000416 exit(0);
417 }
418
Mark Slee3e5d2d72007-06-15 01:45:56 +0000419 if (runServer) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000420
James E. King, III82ae9572017-08-05 12:23:54 -0400421 stdcxx::shared_ptr<ServiceProcessor> serviceProcessor(new ServiceProcessor(serviceHandler));
Marc Slemko3ea00332006-08-17 01:11:13 +0000422
Marc Slemkob09f5882006-08-23 22:03:34 +0000423 // Transport
James E. King, III82ae9572017-08-05 12:23:54 -0400424 stdcxx::shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
Marc Slemko3ea00332006-08-17 01:11:13 +0000425
Aditya Agarwal3950f472006-10-11 02:50:15 +0000426 // Transport Factory
James E. King, III82ae9572017-08-05 12:23:54 -0400427 stdcxx::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
Aditya Agarwal3950f472006-10-11 02:50:15 +0000428
Mark Sleed7173472006-10-25 19:52:10 +0000429 // Protocol Factory
James E. King, III82ae9572017-08-05 12:23:54 -0400430 stdcxx::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Aditya Agarwal3950f472006-10-11 02:50:15 +0000431
432 if (logRequests) {
433 // initialize the log file
James E. King, III82ae9572017-08-05 12:23:54 -0400434 stdcxx::shared_ptr<TFileTransport> fileTransport(new TFileTransport(requestLogPath));
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000435 fileTransport->setChunkSize(2 * 1024 * 1024);
436 fileTransport->setMaxEventSize(1024 * 16);
David Reiss0c90f6f2008-02-06 22:18:40 +0000437
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100438 transportFactory
James E. King, III82ae9572017-08-05 12:23:54 -0400439 = stdcxx::shared_ptr<TTransportFactory>(new TPipedTransportFactory(fileTransport));
Aditya Agarwal3950f472006-10-11 02:50:15 +0000440 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000441
James E. King, III82ae9572017-08-05 12:23:54 -0400442 stdcxx::shared_ptr<TServer> server;
Marc Slemko3ea00332006-08-17 01:11:13 +0000443
Mark Slee3e5d2d72007-06-15 01:45:56 +0000444 if (serverType == "simple") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000445
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100446 server.reset(
447 new TSimpleServer(serviceProcessor, serverSocket, transportFactory, protocolFactory));
David Reiss0c90f6f2008-02-06 22:18:40 +0000448
Mark Slee739dbe52007-02-01 22:55:30 +0000449 } else if (serverType == "threaded") {
450
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100451 server.reset(
452 new TThreadedServer(serviceProcessor, serverSocket, transportFactory, protocolFactory));
David Reiss0c90f6f2008-02-06 22:18:40 +0000453
Mark Slee3e5d2d72007-06-15 01:45:56 +0000454 } else if (serverType == "thread-pool") {
Marc Slemkob09f5882006-08-23 22:03:34 +0000455
James E. King, III82ae9572017-08-05 12:23:54 -0400456 stdcxx::shared_ptr<ThreadManager> threadManager
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100457 = ThreadManager::newSimpleThreadManager(workerCount);
Marc Slemkob09f5882006-08-23 22:03:34 +0000458
459 threadManager->threadFactory(threadFactory);
David Reiss0c90f6f2008-02-06 22:18:40 +0000460 threadManager->start();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100461 server.reset(new TThreadPoolServer(serviceProcessor,
462 serverSocket,
463 transportFactory,
464 protocolFactory,
465 threadManager));
Marc Slemkob09f5882006-08-23 22:03:34 +0000466 }
467
James E. King, III82ae9572017-08-05 12:23:54 -0400468 stdcxx::shared_ptr<TStartObserver> observer(new TStartObserver);
Jake Farrell5d02b802014-01-07 21:42:01 -0500469 server->setServerEventHandler(observer);
James E. King, III82ae9572017-08-05 12:23:54 -0400470 stdcxx::shared_ptr<Thread> serverThread = threadFactory->newThread(server);
Jake Farrell5d02b802014-01-07 21:42:01 -0500471
Marc Slemkob09f5882006-08-23 22:03:34 +0000472 cerr << "Starting the server on port " << port << endl;
473
474 serverThread->start();
Jake Farrell5d02b802014-01-07 21:42:01 -0500475 observer->waitForService();
David Reiss0c90f6f2008-02-06 22:18:40 +0000476
Marc Slemkob09f5882006-08-23 22:03:34 +0000477 // If we aren't running clients, just wait forever for external clients
Mark Sleed7173472006-10-25 19:52:10 +0000478 if (clientCount == 0) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000479 serverThread->join();
480 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000481 }
482
ben-craig02bade12015-07-17 08:40:48 -0500483 if (clientCount > 0) { //FIXME: start here for client type?
Marc Slemko3ea00332006-08-17 01:11:13 +0000484
Marc Slemkob09f5882006-08-23 22:03:34 +0000485 Monitor monitor;
Marc Slemko3ea00332006-08-17 01:11:13 +0000486
Marc Slemkob09f5882006-08-23 22:03:34 +0000487 size_t threadCount = 0;
Marc Slemko3ea00332006-08-17 01:11:13 +0000488
James E. King, III82ae9572017-08-05 12:23:54 -0400489 set<stdcxx::shared_ptr<Thread> > clientThreads;
Marc Slemko3ea00332006-08-17 01:11:13 +0000490
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100491 if (callName == "echoVoid") {
492 loopType = T_VOID;
493 } else if (callName == "echoByte") {
494 loopType = T_BYTE;
495 } else if (callName == "echoI32") {
496 loopType = T_I32;
497 } else if (callName == "echoI64") {
498 loopType = T_I64;
499 } else if (callName == "echoString") {
500 loopType = T_STRING;
501 } else {
502 throw invalid_argument("Unknown service call " + callName);
503 }
Marc Slemkod97eb612006-08-24 23:37:36 +0000504
ben-craig02bade12015-07-17 08:40:48 -0500505 if(clientType == "regular") {
506 for (size_t ix = 0; ix < clientCount; ix++) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000507
James E. King, III82ae9572017-08-05 12:23:54 -0400508 stdcxx::shared_ptr<TSocket> socket(new TSocket("127.0.0.1", port));
509 stdcxx::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket, 2048));
510 stdcxx::shared_ptr<TProtocol> protocol(new TBinaryProtocol(bufferedSocket));
511 stdcxx::shared_ptr<ServiceClient> serviceClient(new ServiceClient(protocol));
ben-craig02bade12015-07-17 08:40:48 -0500512
James E. King, III82ae9572017-08-05 12:23:54 -0400513 clientThreads.insert(threadFactory->newThread(stdcxx::shared_ptr<ClientThread>(
ben-craig02bade12015-07-17 08:40:48 -0500514 new ClientThread(socket, serviceClient, monitor, threadCount, loopCount, loopType, OpenAndCloseTransportInThread))));
515 }
516 } else if(clientType == "concurrent") {
James E. King, III82ae9572017-08-05 12:23:54 -0400517 stdcxx::shared_ptr<TSocket> socket(new TSocket("127.0.0.1", port));
518 stdcxx::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket, 2048));
519 stdcxx::shared_ptr<TProtocol> protocol(new TBinaryProtocol(bufferedSocket));
520 //stdcxx::shared_ptr<ServiceClient> serviceClient(new ServiceClient(protocol));
521 stdcxx::shared_ptr<ServiceConcurrentClient> serviceClient(new ServiceConcurrentClient(protocol));
ben-craig02bade12015-07-17 08:40:48 -0500522 socket->open();
523 for (size_t ix = 0; ix < clientCount; ix++) {
James E. King, III82ae9572017-08-05 12:23:54 -0400524 clientThreads.insert(threadFactory->newThread(stdcxx::shared_ptr<ClientThread>(
ben-craig02bade12015-07-17 08:40:48 -0500525 new ClientThread(socket, serviceClient, monitor, threadCount, loopCount, loopType, DontOpenAndCloseTransportInThread))));
526 }
Marc Slemkob09f5882006-08-23 22:03:34 +0000527 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000528
James E. King, III82ae9572017-08-05 12:23:54 -0400529 for (std::set<stdcxx::shared_ptr<Thread> >::const_iterator thread = clientThreads.begin();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100530 thread != clientThreads.end();
531 thread++) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000532 (*thread)->start();
533 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000534
Roger Meier5f9614c2010-11-21 16:59:05 +0000535 int64_t time00;
536 int64_t time01;
David Reiss0c90f6f2008-02-06 22:18:40 +0000537
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100538 {
539 Synchronized s(monitor);
Marc Slemkob09f5882006-08-23 22:03:34 +0000540 threadCount = clientCount;
David Reiss0c90f6f2008-02-06 22:18:40 +0000541
ben-craig02bade12015-07-17 08:40:48 -0500542 cerr << "Launch " << clientCount << " " << clientType << " client threads" << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +0000543
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100544 time00 = Util::currentTime();
David Reiss0c90f6f2008-02-06 22:18:40 +0000545
Marc Slemkob09f5882006-08-23 22:03:34 +0000546 monitor.notifyAll();
David Reiss0c90f6f2008-02-06 22:18:40 +0000547
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100548 while (threadCount > 0) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000549 monitor.wait();
Marc Slemkob09f5882006-08-23 22:03:34 +0000550 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000551
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100552 time01 = Util::currentTime();
Marc Slemko3ea00332006-08-17 01:11:13 +0000553 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000554
Roger Meier5f9614c2010-11-21 16:59:05 +0000555 int64_t firstTime = 9223372036854775807LL;
556 int64_t lastTime = 0;
Marc Slemko3ea00332006-08-17 01:11:13 +0000557
Marc Slemkob09f5882006-08-23 22:03:34 +0000558 double averageTime = 0;
Roger Meier5f9614c2010-11-21 16:59:05 +0000559 int64_t minTime = 9223372036854775807LL;
560 int64_t maxTime = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000561
James E. King, III82ae9572017-08-05 12:23:54 -0400562 for (set<stdcxx::shared_ptr<Thread> >::iterator ix = clientThreads.begin();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100563 ix != clientThreads.end();
564 ix++) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000565
James E. King, III82ae9572017-08-05 12:23:54 -0400566 stdcxx::shared_ptr<ClientThread> client
567 = stdcxx::dynamic_pointer_cast<ClientThread>((*ix)->runnable());
David Reiss0c90f6f2008-02-06 22:18:40 +0000568
Roger Meier5f9614c2010-11-21 16:59:05 +0000569 int64_t delta = client->_endTime - client->_startTime;
David Reiss0c90f6f2008-02-06 22:18:40 +0000570
Marc Slemkob09f5882006-08-23 22:03:34 +0000571 assert(delta > 0);
Marc Slemko056f9ba2006-08-17 02:59:05 +0000572
Mark Slee3e5d2d72007-06-15 01:45:56 +0000573 if (client->_startTime < firstTime) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000574 firstTime = client->_startTime;
Marc Slemkob09f5882006-08-23 22:03:34 +0000575 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000576
Mark Slee3e5d2d72007-06-15 01:45:56 +0000577 if (client->_endTime > lastTime) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000578 lastTime = client->_endTime;
Marc Slemkob09f5882006-08-23 22:03:34 +0000579 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000580
Mark Slee3e5d2d72007-06-15 01:45:56 +0000581 if (delta < minTime) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000582 minTime = delta;
Marc Slemkob09f5882006-08-23 22:03:34 +0000583 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000584
Mark Slee3e5d2d72007-06-15 01:45:56 +0000585 if (delta > maxTime) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000586 maxTime = delta;
Marc Slemkob09f5882006-08-23 22:03:34 +0000587 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000588
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100589 averageTime += delta;
Marc Slemkob09f5882006-08-23 22:03:34 +0000590 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000591
Marc Slemkob09f5882006-08-23 22:03:34 +0000592 averageTime /= clientCount;
David Reiss0c90f6f2008-02-06 22:18:40 +0000593
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100594 cout << "workers :" << workerCount << ", client : " << clientCount << ", loops : " << loopCount
595 << ", rate : " << (clientCount * loopCount * 1000) / ((double)(time01 - time00)) << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +0000596
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000597 count_map count = serviceHandler->getCount();
598 count_map::iterator iter;
599 for (iter = count.begin(); iter != count.end(); ++iter) {
600 printf("%s => %d\n", iter->first, iter->second);
601 }
Marc Slemkob09f5882006-08-23 22:03:34 +0000602 cerr << "done." << endl;
Marc Slemko056f9ba2006-08-17 02:59:05 +0000603 }
Marc Slemko056f9ba2006-08-17 02:59:05 +0000604
Marc Slemko3ea00332006-08-17 01:11:13 +0000605 return 0;
606}