blob: a668fdf3918c8e7c46bf65bae3e07685bb51bc06 [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
Marc Slemko3ea00332006-08-17 01:11:13 +000020#include <concurrency/ThreadManager.h>
Roger Meier3faaedf2011-10-02 10:51:45 +000021#include <concurrency/PlatformThreadFactory.h>
Marc Slemko3ea00332006-08-17 01:11:13 +000022#include <concurrency/Monitor.h>
23#include <concurrency/Util.h>
Mark Sleeb9ff32a2006-11-16 01:00:24 +000024#include <concurrency/Mutex.h>
Marc Slemko3ea00332006-08-17 01:11:13 +000025#include <protocol/TBinaryProtocol.h>
26#include <server/TSimpleServer.h>
27#include <server/TThreadPoolServer.h>
Mark Slee739dbe52007-02-01 22:55:30 +000028#include <server/TThreadedServer.h>
Marc Slemko3ea00332006-08-17 01:11:13 +000029#include <transport/TServerSocket.h>
30#include <transport/TSocket.h>
Mark Sleed7173472006-10-25 19:52:10 +000031#include <transport/TTransportUtils.h>
Aditya Agarwal25b29362006-12-09 00:58:15 +000032#include <transport/TFileTransport.h>
Aditya Agarwale9ef8d72006-12-08 23:52:57 +000033#include <TLogging.h>
Aditya Agarwal3950f472006-10-11 02:50:15 +000034
35#include "Service.h"
Marc Slemko3ea00332006-08-17 01:11:13 +000036
37#include <iostream>
38#include <set>
39#include <stdexcept>
40#include <sstream>
Mark Sleeb9ff32a2006-11-16 01:00:24 +000041#include <map>
Mark Sleeb9ff32a2006-11-16 01:00:24 +000042
Marc Slemko3ea00332006-08-17 01:11:13 +000043using namespace std;
Mark Sleef4f18ec2007-05-16 21:22:34 +000044using namespace boost;
Marc Slemko3ea00332006-08-17 01:11:13 +000045
T Jake Lucianib5e62212009-01-31 22:36:20 +000046using namespace apache::thrift;
47using namespace apache::thrift::protocol;
48using namespace apache::thrift::transport;
49using namespace apache::thrift::server;
50using namespace apache::thrift::concurrency;
Marc Slemko3ea00332006-08-17 01:11:13 +000051
52using namespace test::stress;
53
Mark Sleeb9ff32a2006-11-16 01:00:24 +000054struct eqstr {
55 bool operator()(const char* s1, const char* s2) const {
56 return strcmp(s1, s2) == 0;
57 }
58};
59
60struct ltstr {
61 bool operator()(const char* s1, const char* s2) const {
62 return strcmp(s1, s2) < 0;
63 }
64};
65
66
David Reiss0c90f6f2008-02-06 22:18:40 +000067// typedef hash_map<const char*, int, hash<const char*>, eqstr> count_map;
68typedef map<const char*, int, ltstr> count_map;
Mark Sleeb9ff32a2006-11-16 01:00:24 +000069
Aditya Agarwal3950f472006-10-11 02:50:15 +000070class Server : public ServiceIf {
Marc Slemko3ea00332006-08-17 01:11:13 +000071 public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000072 Server() {}
73
74 void count(const char* method) {
David Reiss46379902008-02-04 19:23:57 +000075 Guard m(lock_);
Mark Sleeb9ff32a2006-11-16 01:00:24 +000076 int ct = counts_[method];
77 counts_[method] = ++ct;
78 }
79
80 void echoVoid() {
81 count("echoVoid");
82 return;
83 }
84
85 count_map getCount() {
David Reiss46379902008-02-04 19:23:57 +000086 Guard m(lock_);
Mark Sleeb9ff32a2006-11-16 01:00:24 +000087 return counts_;
88 }
89
Mark Slee739dbe52007-02-01 22:55:30 +000090 int8_t echoByte(const int8_t arg) {return arg;}
91 int32_t echoI32(const int32_t arg) {return arg;}
92 int64_t echoI64(const int64_t arg) {return arg;}
93 void echoString(string& out, const string &arg) {
Aditya Agarwale9ef8d72006-12-08 23:52:57 +000094 if (arg != "hello") {
Roger Meiera8cef6e2011-07-17 18:55:59 +000095 T_ERROR_ABORT("WRONG STRING (%s)!!!!", arg.c_str());
Aditya Agarwale9ef8d72006-12-08 23:52:57 +000096 }
Mark Slee739dbe52007-02-01 22:55:30 +000097 out = arg;
Aditya Agarwale9ef8d72006-12-08 23:52:57 +000098 }
Mark Slee739dbe52007-02-01 22:55:30 +000099 void echoList(vector<int8_t> &out, const vector<int8_t> &arg) { out = arg; }
100 void echoSet(set<int8_t> &out, const set<int8_t> &arg) { out = arg; }
101 void echoMap(map<int8_t, int8_t> &out, const map<int8_t, int8_t> &arg) { out = arg; }
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000102
103private:
104 count_map counts_;
105 Mutex lock_;
106
Marc Slemko3ea00332006-08-17 01:11:13 +0000107};
108
109class ClientThread: public Runnable {
110public:
111
Roger Meier611f90c2011-12-11 22:08:51 +0000112 ClientThread(boost::shared_ptr<TTransport>transport, boost::shared_ptr<ServiceClient> client, Monitor& monitor, size_t& workerCount, size_t loopCount, TType loopType) :
Marc Slemko3ea00332006-08-17 01:11:13 +0000113 _transport(transport),
114 _client(client),
115 _monitor(monitor),
116 _workerCount(workerCount),
Marc Slemkod97eb612006-08-24 23:37:36 +0000117 _loopCount(loopCount),
118 _loopType(loopType)
Marc Slemko3ea00332006-08-17 01:11:13 +0000119 {}
120
121 void run() {
122
David Reiss0c90f6f2008-02-06 22:18:40 +0000123 // Wait for all worker threads to start
Marc Slemko3ea00332006-08-17 01:11:13 +0000124
125 {Synchronized s(_monitor);
David Reiss0c90f6f2008-02-06 22:18:40 +0000126 while(_workerCount == 0) {
127 _monitor.wait();
128 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000129 }
130
131 _startTime = Util::currentTime();
132
133 _transport->open();
134
Marc Slemkod97eb612006-08-24 23:37:36 +0000135 switch(_loopType) {
136 case T_VOID: loopEchoVoid(); break;
137 case T_BYTE: loopEchoByte(); break;
Marc Slemkod97eb612006-08-24 23:37:36 +0000138 case T_I32: loopEchoI32(); break;
139 case T_I64: loopEchoI64(); break;
Marc Slemkod97eb612006-08-24 23:37:36 +0000140 case T_STRING: loopEchoString(); break;
141 default: cerr << "Unexpected loop type" << _loopType << endl; break;
Marc Slemko3ea00332006-08-17 01:11:13 +0000142 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000143
Marc Slemko3ea00332006-08-17 01:11:13 +0000144 _endTime = Util::currentTime();
145
146 _transport->close();
David Reiss0c90f6f2008-02-06 22:18:40 +0000147
Marc Slemko3ea00332006-08-17 01:11:13 +0000148 _done = true;
David Reiss0c90f6f2008-02-06 22:18:40 +0000149
Marc Slemko3ea00332006-08-17 01:11:13 +0000150 {Synchronized s(_monitor);
151
152 _workerCount--;
David Reiss0c90f6f2008-02-06 22:18:40 +0000153
Mark Slee3e5d2d72007-06-15 01:45:56 +0000154 if (_workerCount == 0) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000155
156 _monitor.notify();
Marc Slemko3ea00332006-08-17 01:11:13 +0000157 }
158 }
159 }
Marc Slemkod97eb612006-08-24 23:37:36 +0000160
161 void loopEchoVoid() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000162 for (size_t ix = 0; ix < _loopCount; ix++) {
Marc Slemkod97eb612006-08-24 23:37:36 +0000163 _client->echoVoid();
164 }
165 }
166
167 void loopEchoByte() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000168 for (size_t ix = 0; ix < _loopCount; ix++) {
Aditya Agarwal3950f472006-10-11 02:50:15 +0000169 int8_t arg = 1;
170 int8_t result;
Marc Slemkod97eb612006-08-24 23:37:36 +0000171 result =_client->echoByte(arg);
172 assert(result == arg);
173 }
174 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000175
Marc Slemkod97eb612006-08-24 23:37:36 +0000176 void loopEchoI32() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000177 for (size_t ix = 0; ix < _loopCount; ix++) {
Aditya Agarwal3950f472006-10-11 02:50:15 +0000178 int32_t arg = 1;
179 int32_t result;
Marc Slemkod97eb612006-08-24 23:37:36 +0000180 result =_client->echoI32(arg);
181 assert(result == arg);
182 }
183 }
184
185 void loopEchoI64() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000186 for (size_t ix = 0; ix < _loopCount; ix++) {
Aditya Agarwal3950f472006-10-11 02:50:15 +0000187 int64_t arg = 1;
188 int64_t result;
Marc Slemkod97eb612006-08-24 23:37:36 +0000189 result =_client->echoI64(arg);
190 assert(result == arg);
191 }
192 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000193
Marc Slemkod97eb612006-08-24 23:37:36 +0000194 void loopEchoString() {
Mark Slee3e5d2d72007-06-15 01:45:56 +0000195 for (size_t ix = 0; ix < _loopCount; ix++) {
Marc Slemkod97eb612006-08-24 23:37:36 +0000196 string arg = "hello";
197 string result;
Mark Slee739dbe52007-02-01 22:55:30 +0000198 _client->echoString(result, arg);
Marc Slemkod97eb612006-08-24 23:37:36 +0000199 assert(result == arg);
200 }
201 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000202
Roger Meier611f90c2011-12-11 22:08:51 +0000203 boost::shared_ptr<TTransport> _transport;
204 boost::shared_ptr<ServiceClient> _client;
Marc Slemko3ea00332006-08-17 01:11:13 +0000205 Monitor& _monitor;
206 size_t& _workerCount;
207 size_t _loopCount;
Marc Slemkod97eb612006-08-24 23:37:36 +0000208 TType _loopType;
Roger Meier5f9614c2010-11-21 16:59:05 +0000209 int64_t _startTime;
210 int64_t _endTime;
Marc Slemko3ea00332006-08-17 01:11:13 +0000211 bool _done;
212 Monitor _sleep;
213};
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000214
215
Marc Slemko3ea00332006-08-17 01:11:13 +0000216int main(int argc, char **argv) {
217
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000218 int port = 9091;
Marc Slemko3ea00332006-08-17 01:11:13 +0000219 string serverType = "thread-pool";
220 string protocolType = "binary";
221 size_t workerCount = 4;
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000222 size_t clientCount = 20;
223 size_t loopCount = 50000;
Marc Slemkod97eb612006-08-24 23:37:36 +0000224 TType loopType = T_VOID;
225 string callName = "echoVoid";
Marc Slemkob09f5882006-08-23 22:03:34 +0000226 bool runServer = true;
Aditya Agarwal3950f472006-10-11 02:50:15 +0000227 bool logRequests = false;
228 string requestLogPath = "./requestlog.tlog";
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000229 bool replayRequests = false;
Marc Slemko3ea00332006-08-17 01:11:13 +0000230
Marc Slemkob09f5882006-08-23 22:03:34 +0000231 ostringstream usage;
Marc Slemko3ea00332006-08-17 01:11:13 +0000232
233 usage <<
Marc Slemkob09f5882006-08-23 22:03:34 +0000234 argv[0] << " [--port=<port number>] [--server] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>] [--clients=<client-count>] [--loop=<loop-count>]" << endl <<
235 "\tclients Number of client threads to create - 0 implies no clients, i.e. server only. Default is " << clientCount << endl <<
236 "\thelp Prints this help text." << endl <<
Marc Slemkod97eb612006-08-24 23:37:36 +0000237 "\tcall Service method to call. Default is " << callName << endl <<
Marc Slemkob09f5882006-08-23 22:03:34 +0000238 "\tloop The number of remote thrift calls each client makes. Default is " << loopCount << endl <<
239 "\tport The port the server and clients should bind to for thrift network connections. Default is " << port << endl <<
240 "\tserver Run the Thrift server in this process. Default is " << runServer << endl <<
241 "\tserver-type Type of server, \"simple\" or \"thread-pool\". Default is " << serverType << endl <<
242 "\tprotocol-type Type of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl <<
Aditya Agarwal3950f472006-10-11 02:50:15 +0000243 "\tlog-request Log all request to ./requestlog.tlog. Default is " << logRequests << endl <<
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000244 "\treplay-request Replay requests from log file (./requestlog.tlog) Default is " << replayRequests << endl <<
Marc Slemkob09f5882006-08-23 22:03:34 +0000245 "\tworkers Number of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +0000246
247
Marc Slemko3ea00332006-08-17 01:11:13 +0000248 map<string, string> args;
David Reiss0c90f6f2008-02-06 22:18:40 +0000249
Mark Slee3e5d2d72007-06-15 01:45:56 +0000250 for (int ix = 1; ix < argc; ix++) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000251
252 string arg(argv[ix]);
253
Mark Slee3e5d2d72007-06-15 01:45:56 +0000254 if (arg.compare(0,2, "--") == 0) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000255
256 size_t end = arg.find_first_of("=", 2);
257
Marc Slemko056f9ba2006-08-17 02:59:05 +0000258 string key = string(arg, 2, end - 2);
259
Mark Slee3e5d2d72007-06-15 01:45:56 +0000260 if (end != string::npos) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000261 args[key] = string(arg, end + 1);
Marc Slemko3ea00332006-08-17 01:11:13 +0000262 } else {
David Reiss0c90f6f2008-02-06 22:18:40 +0000263 args[key] = "true";
Marc Slemko3ea00332006-08-17 01:11:13 +0000264 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000265 } else {
266 throw invalid_argument("Unexcepted command line token: "+arg);
267 }
268 }
269
270 try {
271
Mark Slee3e5d2d72007-06-15 01:45:56 +0000272 if (!args["clients"].empty()) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000273 clientCount = atoi(args["clients"].c_str());
274 }
275
Mark Slee3e5d2d72007-06-15 01:45:56 +0000276 if (!args["help"].empty()) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000277 cerr << usage.str();
278 return 0;
279 }
280
Mark Slee3e5d2d72007-06-15 01:45:56 +0000281 if (!args["loop"].empty()) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000282 loopCount = atoi(args["loop"].c_str());
283 }
284
Mark Slee3e5d2d72007-06-15 01:45:56 +0000285 if (!args["call"].empty()) {
Marc Slemkod97eb612006-08-24 23:37:36 +0000286 callName = args["call"];
287 }
288
Mark Slee3e5d2d72007-06-15 01:45:56 +0000289 if (!args["port"].empty()) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000290 port = atoi(args["port"].c_str());
291 }
292
Mark Slee3e5d2d72007-06-15 01:45:56 +0000293 if (!args["server"].empty()) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000294 runServer = args["server"] == "true";
295 }
296
Mark Slee3e5d2d72007-06-15 01:45:56 +0000297 if (!args["log-request"].empty()) {
Aditya Agarwal3950f472006-10-11 02:50:15 +0000298 logRequests = args["log-request"] == "true";
299 }
300
Mark Slee3e5d2d72007-06-15 01:45:56 +0000301 if (!args["replay-request"].empty()) {
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000302 replayRequests = args["replay-request"] == "true";
303 }
304
Mark Slee3e5d2d72007-06-15 01:45:56 +0000305 if (!args["server-type"].empty()) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000306 serverType = args["server-type"];
David Reiss0c90f6f2008-02-06 22:18:40 +0000307
Mark Slee3e5d2d72007-06-15 01:45:56 +0000308 if (serverType == "simple") {
Marc Slemko3ea00332006-08-17 01:11:13 +0000309
Mark Slee3e5d2d72007-06-15 01:45:56 +0000310 } else if (serverType == "thread-pool") {
Marc Slemko3ea00332006-08-17 01:11:13 +0000311
Mark Slee3e5d2d72007-06-15 01:45:56 +0000312 } else if (serverType == "threaded") {
Mark Slee739dbe52007-02-01 22:55:30 +0000313
Marc Slemko3ea00332006-08-17 01:11:13 +0000314 } else {
315
David Reiss0c90f6f2008-02-06 22:18:40 +0000316 throw invalid_argument("Unknown server type "+serverType);
Marc Slemko3ea00332006-08-17 01:11:13 +0000317 }
318 }
319
Mark Slee3e5d2d72007-06-15 01:45:56 +0000320 if (!args["workers"].empty()) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000321 workerCount = atoi(args["workers"].c_str());
322 }
323
Roger Meierbb09f442011-05-31 20:35:37 +0000324 } catch(std::exception& e) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000325 cerr << e.what() << endl;
326 cerr << usage;
327 }
328
Roger Meier611f90c2011-12-11 22:08:51 +0000329 boost::shared_ptr<PlatformThreadFactory> threadFactory = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
Marc Slemko3ea00332006-08-17 01:11:13 +0000330
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000331 // Dispatcher
Roger Meier611f90c2011-12-11 22:08:51 +0000332 boost::shared_ptr<Server> serviceHandler(new Server());
Marc Slemko3ea00332006-08-17 01:11:13 +0000333
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000334 if (replayRequests) {
Roger Meier611f90c2011-12-11 22:08:51 +0000335 boost::shared_ptr<Server> serviceHandler(new Server());
336 boost::shared_ptr<ServiceProcessor> serviceProcessor(new ServiceProcessor(serviceHandler));
David Reiss0c90f6f2008-02-06 22:18:40 +0000337
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000338 // Transports
Roger Meier611f90c2011-12-11 22:08:51 +0000339 boost::shared_ptr<TFileTransport> fileTransport(new TFileTransport(requestLogPath));
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000340 fileTransport->setChunkSize(2 * 1024 * 1024);
341 fileTransport->setMaxEventSize(1024 * 16);
342 fileTransport->seekToEnd();
343
344 // Protocol Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000345 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000346
347 TFileProcessor fileProcessor(serviceProcessor,
348 protocolFactory,
349 fileTransport);
350
David Reiss0c90f6f2008-02-06 22:18:40 +0000351 fileProcessor.process(0, true);
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000352 exit(0);
353 }
354
355
Mark Slee3e5d2d72007-06-15 01:45:56 +0000356 if (runServer) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000357
Roger Meier611f90c2011-12-11 22:08:51 +0000358 boost::shared_ptr<ServiceProcessor> serviceProcessor(new ServiceProcessor(serviceHandler));
Marc Slemko3ea00332006-08-17 01:11:13 +0000359
Marc Slemkob09f5882006-08-23 22:03:34 +0000360 // Transport
Roger Meier611f90c2011-12-11 22:08:51 +0000361 boost::shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
Marc Slemko3ea00332006-08-17 01:11:13 +0000362
Aditya Agarwal3950f472006-10-11 02:50:15 +0000363 // Transport Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000364 boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
Aditya Agarwal3950f472006-10-11 02:50:15 +0000365
Mark Sleed7173472006-10-25 19:52:10 +0000366 // Protocol Factory
Roger Meier611f90c2011-12-11 22:08:51 +0000367 boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
Aditya Agarwal3950f472006-10-11 02:50:15 +0000368
369 if (logRequests) {
370 // initialize the log file
Roger Meier611f90c2011-12-11 22:08:51 +0000371 boost::shared_ptr<TFileTransport> fileTransport(new TFileTransport(requestLogPath));
Aditya Agarwale9ef8d72006-12-08 23:52:57 +0000372 fileTransport->setChunkSize(2 * 1024 * 1024);
373 fileTransport->setMaxEventSize(1024 * 16);
David Reiss0c90f6f2008-02-06 22:18:40 +0000374
375 transportFactory =
Roger Meier611f90c2011-12-11 22:08:51 +0000376 boost::shared_ptr<TTransportFactory>(new TPipedTransportFactory(fileTransport));
Aditya Agarwal3950f472006-10-11 02:50:15 +0000377 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000378
Roger Meier611f90c2011-12-11 22:08:51 +0000379 boost::shared_ptr<Thread> serverThread;
Marc Slemko3ea00332006-08-17 01:11:13 +0000380
Mark Slee3e5d2d72007-06-15 01:45:56 +0000381 if (serverType == "simple") {
David Reiss0c90f6f2008-02-06 22:18:40 +0000382
Roger Meier611f90c2011-12-11 22:08:51 +0000383 serverThread = threadFactory->newThread(boost::shared_ptr<TServer>(new TSimpleServer(serviceProcessor, serverSocket, transportFactory, protocolFactory)));
David Reiss0c90f6f2008-02-06 22:18:40 +0000384
Mark Slee739dbe52007-02-01 22:55:30 +0000385 } else if (serverType == "threaded") {
386
Roger Meier611f90c2011-12-11 22:08:51 +0000387 serverThread = threadFactory->newThread(boost::shared_ptr<TServer>(new TThreadedServer(serviceProcessor, serverSocket, transportFactory, protocolFactory)));
David Reiss0c90f6f2008-02-06 22:18:40 +0000388
Mark Slee3e5d2d72007-06-15 01:45:56 +0000389 } else if (serverType == "thread-pool") {
Marc Slemkob09f5882006-08-23 22:03:34 +0000390
Roger Meier611f90c2011-12-11 22:08:51 +0000391 boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
Marc Slemkob09f5882006-08-23 22:03:34 +0000392
393 threadManager->threadFactory(threadFactory);
David Reiss0c90f6f2008-02-06 22:18:40 +0000394 threadManager->start();
Roger Meier611f90c2011-12-11 22:08:51 +0000395 serverThread = threadFactory->newThread(boost::shared_ptr<TServer>(new TThreadPoolServer(serviceProcessor, serverSocket, transportFactory, protocolFactory, threadManager)));
Marc Slemkob09f5882006-08-23 22:03:34 +0000396 }
397
398 cerr << "Starting the server on port " << port << endl;
399
400 serverThread->start();
David Reiss0c90f6f2008-02-06 22:18:40 +0000401
Marc Slemkob09f5882006-08-23 22:03:34 +0000402 // If we aren't running clients, just wait forever for external clients
403
Mark Sleed7173472006-10-25 19:52:10 +0000404 if (clientCount == 0) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000405 serverThread->join();
406 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000407 }
408
Mark Sleed7173472006-10-25 19:52:10 +0000409 if (clientCount > 0) {
Marc Slemko3ea00332006-08-17 01:11:13 +0000410
Marc Slemkob09f5882006-08-23 22:03:34 +0000411 Monitor monitor;
Marc Slemko3ea00332006-08-17 01:11:13 +0000412
Marc Slemkob09f5882006-08-23 22:03:34 +0000413 size_t threadCount = 0;
Marc Slemko3ea00332006-08-17 01:11:13 +0000414
Roger Meier611f90c2011-12-11 22:08:51 +0000415 set<boost::shared_ptr<Thread> > clientThreads;
Marc Slemko3ea00332006-08-17 01:11:13 +0000416
Mark Slee3e5d2d72007-06-15 01:45:56 +0000417 if (callName == "echoVoid") { loopType = T_VOID;}
418 else if (callName == "echoByte") { loopType = T_BYTE;}
419 else if (callName == "echoI32") { loopType = T_I32;}
420 else if (callName == "echoI64") { loopType = T_I64;}
421 else if (callName == "echoString") { loopType = T_STRING;}
Marc Slemkod97eb612006-08-24 23:37:36 +0000422 else {throw invalid_argument("Unknown service call "+callName);}
423
Mark Slee3e5d2d72007-06-15 01:45:56 +0000424 for (size_t ix = 0; ix < clientCount; ix++) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000425
Roger Meier611f90c2011-12-11 22:08:51 +0000426 boost::shared_ptr<TSocket> socket(new TSocket("127.0.0.1", port));
427 boost::shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket, 2048));
428 boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(bufferedSocket));
429 boost::shared_ptr<ServiceClient> serviceClient(new ServiceClient(protocol));
David Reiss0c90f6f2008-02-06 22:18:40 +0000430
Roger Meier611f90c2011-12-11 22:08:51 +0000431 clientThreads.insert(threadFactory->newThread(boost::shared_ptr<ClientThread>(new ClientThread(socket, serviceClient, monitor, threadCount, loopCount, loopType))));
Marc Slemkob09f5882006-08-23 22:03:34 +0000432 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000433
Roger Meier611f90c2011-12-11 22:08:51 +0000434 for (std::set<boost::shared_ptr<Thread> >::const_iterator thread = clientThreads.begin(); thread != clientThreads.end(); thread++) {
Marc Slemkob09f5882006-08-23 22:03:34 +0000435 (*thread)->start();
436 }
Marc Slemko3ea00332006-08-17 01:11:13 +0000437
Roger Meier5f9614c2010-11-21 16:59:05 +0000438 int64_t time00;
439 int64_t time01;
David Reiss0c90f6f2008-02-06 22:18:40 +0000440
Marc Slemkob09f5882006-08-23 22:03:34 +0000441 {Synchronized s(monitor);
442 threadCount = clientCount;
David Reiss0c90f6f2008-02-06 22:18:40 +0000443
Marc Slemkob09f5882006-08-23 22:03:34 +0000444 cerr << "Launch "<< clientCount << " client threads" << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +0000445
Marc Slemkob09f5882006-08-23 22:03:34 +0000446 time00 = Util::currentTime();
David Reiss0c90f6f2008-02-06 22:18:40 +0000447
Marc Slemkob09f5882006-08-23 22:03:34 +0000448 monitor.notifyAll();
David Reiss0c90f6f2008-02-06 22:18:40 +0000449
Marc Slemkob09f5882006-08-23 22:03:34 +0000450 while(threadCount > 0) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000451 monitor.wait();
Marc Slemkob09f5882006-08-23 22:03:34 +0000452 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000453
Marc Slemkob09f5882006-08-23 22:03:34 +0000454 time01 = Util::currentTime();
Marc Slemko3ea00332006-08-17 01:11:13 +0000455 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000456
Roger Meier5f9614c2010-11-21 16:59:05 +0000457 int64_t firstTime = 9223372036854775807LL;
458 int64_t lastTime = 0;
Marc Slemko3ea00332006-08-17 01:11:13 +0000459
Marc Slemkob09f5882006-08-23 22:03:34 +0000460 double averageTime = 0;
Roger Meier5f9614c2010-11-21 16:59:05 +0000461 int64_t minTime = 9223372036854775807LL;
462 int64_t maxTime = 0;
David Reiss0c90f6f2008-02-06 22:18:40 +0000463
Roger Meier611f90c2011-12-11 22:08:51 +0000464 for (set<boost::shared_ptr<Thread> >::iterator ix = clientThreads.begin(); ix != clientThreads.end(); ix++) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000465
Roger Meier611f90c2011-12-11 22:08:51 +0000466 boost::shared_ptr<ClientThread> client = dynamic_pointer_cast<ClientThread>((*ix)->runnable());
David Reiss0c90f6f2008-02-06 22:18:40 +0000467
Roger Meier5f9614c2010-11-21 16:59:05 +0000468 int64_t delta = client->_endTime - client->_startTime;
David Reiss0c90f6f2008-02-06 22:18:40 +0000469
Marc Slemkob09f5882006-08-23 22:03:34 +0000470 assert(delta > 0);
Marc Slemko056f9ba2006-08-17 02:59:05 +0000471
Mark Slee3e5d2d72007-06-15 01:45:56 +0000472 if (client->_startTime < firstTime) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000473 firstTime = client->_startTime;
Marc Slemkob09f5882006-08-23 22:03:34 +0000474 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000475
Mark Slee3e5d2d72007-06-15 01:45:56 +0000476 if (client->_endTime > lastTime) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000477 lastTime = client->_endTime;
Marc Slemkob09f5882006-08-23 22:03:34 +0000478 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000479
Mark Slee3e5d2d72007-06-15 01:45:56 +0000480 if (delta < minTime) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000481 minTime = delta;
Marc Slemkob09f5882006-08-23 22:03:34 +0000482 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000483
Mark Slee3e5d2d72007-06-15 01:45:56 +0000484 if (delta > maxTime) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000485 maxTime = delta;
Marc Slemkob09f5882006-08-23 22:03:34 +0000486 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000487
Marc Slemkob09f5882006-08-23 22:03:34 +0000488 averageTime+= delta;
489 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000490
Marc Slemkob09f5882006-08-23 22:03:34 +0000491 averageTime /= clientCount;
David Reiss0c90f6f2008-02-06 22:18:40 +0000492
493
Marc Slemkob09f5882006-08-23 22:03:34 +0000494 cout << "workers :" << workerCount << ", client : " << clientCount << ", loops : " << loopCount << ", rate : " << (clientCount * loopCount * 1000) / ((double)(time01 - time00)) << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +0000495
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000496 count_map count = serviceHandler->getCount();
497 count_map::iterator iter;
498 for (iter = count.begin(); iter != count.end(); ++iter) {
499 printf("%s => %d\n", iter->first, iter->second);
500 }
Marc Slemkob09f5882006-08-23 22:03:34 +0000501 cerr << "done." << endl;
Marc Slemko056f9ba2006-08-17 02:59:05 +0000502 }
Marc Slemko056f9ba2006-08-17 02:59:05 +0000503
Marc Slemko3ea00332006-08-17 01:11:13 +0000504 return 0;
505}