| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* | 
|  | 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 Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 20 | #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> | 
|  | 32 | #include <thrift/transport/TTransportUtils.h> | 
|  | 33 | #include <thrift/transport/TFileTransport.h> | 
| Roger Meier | 33eaa0f | 2012-04-13 09:13:13 +0000 | [diff] [blame] | 34 | #include <thrift/TLogging.h> | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 35 |  | 
|  | 36 | #include "Service.h" | 
|  | 37 |  | 
|  | 38 | #include <boost/shared_ptr.hpp> | 
|  | 39 |  | 
|  | 40 | #include <iostream> | 
|  | 41 | #include <set> | 
|  | 42 | #include <stdexcept> | 
|  | 43 | #include <sstream> | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 44 | #include <map> | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 45 | #if _WIN32 | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 46 | #include <thrift/windows/TWinsockSingleton.h> | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 47 | #endif | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 48 |  | 
|  | 49 | using namespace std; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 50 |  | 
| T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 51 | using namespace apache::thrift; | 
|  | 52 | using namespace apache::thrift::protocol; | 
|  | 53 | using namespace apache::thrift::transport; | 
|  | 54 | using namespace apache::thrift::server; | 
|  | 55 | using namespace apache::thrift::concurrency; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 56 |  | 
|  | 57 | using namespace test::stress; | 
|  | 58 |  | 
|  | 59 | struct eqstr { | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 60 | bool operator()(const char* s1, const char* s2) const { | 
|  | 61 | return strcmp(s1, s2) == 0; | 
|  | 62 | } | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 63 | }; | 
|  | 64 |  | 
|  | 65 | struct ltstr { | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 66 | bool operator()(const char* s1, const char* s2) const { | 
|  | 67 | return strcmp(s1, s2) < 0; | 
|  | 68 | } | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 69 | }; | 
|  | 70 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 71 |  | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 72 | // typedef hash_map<const char*, int, hash<const char*>, eqstr> count_map; | 
|  | 73 | typedef map<const char*, int, ltstr> count_map; | 
|  | 74 |  | 
|  | 75 | class Server : public ServiceIf { | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 76 | public: | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 77 | Server() {} | 
|  | 78 |  | 
|  | 79 | void count(const char* method) { | 
| Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 80 | Guard m(lock_); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 81 | int ct = counts_[method]; | 
|  | 82 | counts_[method] = ++ct; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | void echoVoid() { | 
|  | 86 | count("echoVoid"); | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 87 | // Sleep to simulate work | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 88 | THRIFT_SLEEP_USEC(1); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 89 | return; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | count_map getCount() { | 
| Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 93 | Guard m(lock_); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 94 | return counts_; | 
|  | 95 | } | 
|  | 96 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 97 | int8_t echoByte(const int8_t arg) {return arg;} | 
|  | 98 | int32_t echoI32(const int32_t arg) {return arg;} | 
|  | 99 | int64_t echoI64(const int64_t arg) {return arg;} | 
|  | 100 | void echoString(string& out, const string &arg) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 101 | if (arg != "hello") { | 
| Roger Meier | a8cef6e | 2011-07-17 18:55:59 +0000 | [diff] [blame] | 102 | T_ERROR_ABORT("WRONG STRING (%s)!!!!", arg.c_str()); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 103 | } | 
|  | 104 | out = arg; | 
|  | 105 | } | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 106 | void echoList(vector<int8_t> &out, const vector<int8_t> &arg) { out = arg; } | 
|  | 107 | void echoSet(set<int8_t> &out, const set<int8_t> &arg) { out = arg; } | 
|  | 108 | void echoMap(map<int8_t, int8_t> &out, const map<int8_t, int8_t> &arg) { out = arg; } | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 109 |  | 
|  | 110 | private: | 
|  | 111 | count_map counts_; | 
|  | 112 | Mutex lock_; | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 113 |  | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 114 | }; | 
|  | 115 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 116 | class ClientThread: public Runnable { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 117 | public: | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 118 |  | 
|  | 119 | ClientThread(boost::shared_ptr<TTransport>transport, boost::shared_ptr<ServiceClient> client, Monitor& monitor, size_t& workerCount, size_t loopCount, TType loopType) : | 
|  | 120 | _transport(transport), | 
|  | 121 | _client(client), | 
|  | 122 | _monitor(monitor), | 
|  | 123 | _workerCount(workerCount), | 
|  | 124 | _loopCount(loopCount), | 
|  | 125 | _loopType(loopType) | 
|  | 126 | {} | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 127 |  | 
|  | 128 | void run() { | 
|  | 129 |  | 
|  | 130 | // Wait for all worker threads to start | 
|  | 131 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 132 | {Synchronized s(_monitor); | 
|  | 133 | while(_workerCount == 0) { | 
|  | 134 | _monitor.wait(); | 
|  | 135 | } | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
|  | 138 | _startTime = Util::currentTime(); | 
|  | 139 |  | 
|  | 140 | _transport->open(); | 
|  | 141 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 142 | switch(_loopType) { | 
|  | 143 | case T_VOID: loopEchoVoid(); break; | 
|  | 144 | case T_BYTE: loopEchoByte(); break; | 
|  | 145 | case T_I32: loopEchoI32(); break; | 
|  | 146 | case T_I64: loopEchoI64(); break; | 
|  | 147 | case T_STRING: loopEchoString(); break; | 
|  | 148 | default: cerr << "Unexpected loop type" << _loopType << endl; break; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
|  | 151 | _endTime = Util::currentTime(); | 
|  | 152 |  | 
|  | 153 | _transport->close(); | 
|  | 154 |  | 
|  | 155 | _done = true; | 
|  | 156 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 157 | {Synchronized s(_monitor); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 158 |  | 
|  | 159 | _workerCount--; | 
|  | 160 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 161 | if (_workerCount == 0) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 162 |  | 
|  | 163 | _monitor.notify(); | 
|  | 164 | } | 
|  | 165 | } | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | void loopEchoVoid() { | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 169 | for (size_t ix = 0; ix < _loopCount; ix++) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 170 | _client->echoVoid(); | 
|  | 171 | } | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | void loopEchoByte() { | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 175 | for (size_t ix = 0; ix < _loopCount; ix++) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 176 | int8_t arg = 1; | 
|  | 177 | int8_t result; | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 178 | result =_client->echoByte(arg); | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 179 | (void)result; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 180 | assert(result == arg); | 
|  | 181 | } | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | void loopEchoI32() { | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 185 | for (size_t ix = 0; ix < _loopCount; ix++) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 186 | int32_t arg = 1; | 
|  | 187 | int32_t result; | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 188 | result =_client->echoI32(arg); | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 189 | (void)result; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 190 | assert(result == arg); | 
|  | 191 | } | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | void loopEchoI64() { | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 195 | for (size_t ix = 0; ix < _loopCount; ix++) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 196 | int64_t arg = 1; | 
|  | 197 | int64_t result; | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 198 | result =_client->echoI64(arg); | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 199 | (void)result; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 200 | assert(result == arg); | 
|  | 201 | } | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | void loopEchoString() { | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 205 | for (size_t ix = 0; ix < _loopCount; ix++) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 206 | string arg = "hello"; | 
|  | 207 | string result; | 
|  | 208 | _client->echoString(result, arg); | 
|  | 209 | assert(result == arg); | 
|  | 210 | } | 
|  | 211 | } | 
|  | 212 |  | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 213 | boost::shared_ptr<TTransport> _transport; | 
|  | 214 | boost::shared_ptr<ServiceClient> _client; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 215 | Monitor& _monitor; | 
|  | 216 | size_t& _workerCount; | 
|  | 217 | size_t _loopCount; | 
|  | 218 | TType _loopType; | 
| Roger Meier | 5f9614c | 2010-11-21 16:59:05 +0000 | [diff] [blame] | 219 | int64_t _startTime; | 
|  | 220 | int64_t _endTime; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 221 | bool _done; | 
|  | 222 | Monitor _sleep; | 
|  | 223 | }; | 
|  | 224 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 225 |  | 
|  | 226 | int main(int argc, char **argv) { | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 227 | #if _WIN32 | 
|  | 228 | transport::TWinsockSingleton::create(); | 
|  | 229 | #endif | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 230 |  | 
|  | 231 | int port = 9091; | 
|  | 232 | string serverType = "simple"; | 
|  | 233 | string protocolType = "binary"; | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 234 | uint32_t workerCount = 4; | 
|  | 235 | uint32_t clientCount = 20; | 
|  | 236 | uint32_t loopCount = 1000; | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 237 | TType loopType  = T_VOID; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 238 | string callName = "echoVoid"; | 
|  | 239 | bool runServer = true; | 
|  | 240 | bool logRequests = false; | 
|  | 241 | string requestLogPath = "./requestlog.tlog"; | 
|  | 242 | bool replayRequests = false; | 
|  | 243 |  | 
|  | 244 | ostringstream usage; | 
|  | 245 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 246 | usage << | 
|  | 247 | argv[0] << " [--port=<port number>] [--server] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>] [--clients=<client-count>] [--loop=<loop-count>]" << endl << | 
|  | 248 | "\tclients        Number of client threads to create - 0 implies no clients, i.e. server only.  Default is " << clientCount << endl << | 
|  | 249 | "\thelp           Prints this help text." << endl << | 
|  | 250 | "\tcall           Service method to call.  Default is " << callName << endl << | 
|  | 251 | "\tloop           The number of remote thrift calls each client makes.  Default is " << loopCount << endl << | 
|  | 252 | "\tport           The port the server and clients should bind to for thrift network connections.  Default is " << port << endl << | 
|  | 253 | "\tserver         Run the Thrift server in this process.  Default is " << runServer << endl << | 
|  | 254 | "\tserver-type    Type of server, \"simple\" or \"thread-pool\".  Default is " << serverType << endl << | 
|  | 255 | "\tprotocol-type  Type of protocol, \"binary\", \"ascii\", or \"xml\".  Default is " << protocolType << endl << | 
|  | 256 | "\tlog-request    Log all request to ./requestlog.tlog. Default is " << logRequests << endl << | 
|  | 257 | "\treplay-request Replay requests from log file (./requestlog.tlog) Default is " << replayRequests << endl << | 
|  | 258 | "\tworkers        Number of thread pools workers.  Only valid for thread-pool server type.  Default is " << workerCount << endl; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 259 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 260 |  | 
|  | 261 | map<string, string>  args; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 262 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 263 | for (int ix = 1; ix < argc; ix++) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 264 |  | 
|  | 265 | string arg(argv[ix]); | 
|  | 266 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 267 | if (arg.compare(0,2, "--") == 0) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 268 |  | 
|  | 269 | size_t end = arg.find_first_of("=", 2); | 
|  | 270 |  | 
|  | 271 | string key = string(arg, 2, end - 2); | 
|  | 272 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 273 | if (end != string::npos) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 274 | args[key] = string(arg, end + 1); | 
|  | 275 | } else { | 
|  | 276 | args[key] = "true"; | 
|  | 277 | } | 
|  | 278 | } else { | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 279 | throw invalid_argument("Unexcepted command line token: "+arg); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 280 | } | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | try { | 
|  | 284 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 285 | if (!args["clients"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 286 | clientCount = atoi(args["clients"].c_str()); | 
|  | 287 | } | 
|  | 288 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 289 | if (!args["help"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 290 | cerr << usage.str(); | 
|  | 291 | return 0; | 
|  | 292 | } | 
|  | 293 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 294 | if (!args["loop"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 295 | loopCount = atoi(args["loop"].c_str()); | 
|  | 296 | } | 
|  | 297 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 298 | if (!args["call"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 299 | callName = args["call"]; | 
|  | 300 | } | 
|  | 301 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 302 | if (!args["port"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 303 | port = atoi(args["port"].c_str()); | 
|  | 304 | } | 
|  | 305 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 306 | if (!args["server"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 307 | runServer = args["server"] == "true"; | 
|  | 308 | } | 
|  | 309 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 310 | if (!args["log-request"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 311 | logRequests = args["log-request"] == "true"; | 
|  | 312 | } | 
|  | 313 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 314 | if (!args["replay-request"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 315 | replayRequests = args["replay-request"] == "true"; | 
|  | 316 | } | 
|  | 317 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 318 | if (!args["server-type"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 319 | serverType = args["server-type"]; | 
|  | 320 | } | 
|  | 321 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 322 | if (!args["workers"].empty()) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 323 | workerCount = atoi(args["workers"].c_str()); | 
|  | 324 | } | 
|  | 325 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 326 | } catch(std::exception& e) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 327 | cerr << e.what() << endl; | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 328 | cerr << usage.str(); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 329 | } | 
|  | 330 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 331 | boost::shared_ptr<PlatformThreadFactory> threadFactory = boost::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory()); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 332 |  | 
|  | 333 | // Dispatcher | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 334 | boost::shared_ptr<Server> serviceHandler(new Server()); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 335 |  | 
|  | 336 | if (replayRequests) { | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 337 | boost::shared_ptr<Server> serviceHandler(new Server()); | 
|  | 338 | boost::shared_ptr<ServiceProcessor> serviceProcessor(new ServiceProcessor(serviceHandler)); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 339 |  | 
|  | 340 | // Transports | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 341 | boost::shared_ptr<TFileTransport> fileTransport(new TFileTransport(requestLogPath)); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 342 | fileTransport->setChunkSize(2 * 1024 * 1024); | 
|  | 343 | fileTransport->setMaxEventSize(1024 * 16); | 
|  | 344 | fileTransport->seekToEnd(); | 
|  | 345 |  | 
|  | 346 | // Protocol Factory | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 347 | boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 348 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 349 | TFileProcessor fileProcessor(serviceProcessor, | 
|  | 350 | protocolFactory, | 
|  | 351 | fileTransport); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 352 |  | 
|  | 353 | fileProcessor.process(0, true); | 
|  | 354 | exit(0); | 
|  | 355 | } | 
|  | 356 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 357 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 358 | if (runServer) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 359 |  | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 360 | boost::shared_ptr<ServiceProcessor> serviceProcessor(new ServiceProcessor(serviceHandler)); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 361 |  | 
|  | 362 | // Protocol Factory | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 363 | boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 364 |  | 
|  | 365 | // Transport Factory | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 366 | boost::shared_ptr<TTransportFactory>      transportFactory; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 367 |  | 
|  | 368 | if (logRequests) { | 
|  | 369 | // initialize the log file | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 370 | boost::shared_ptr<TFileTransport> fileTransport(new TFileTransport(requestLogPath)); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 371 | fileTransport->setChunkSize(2 * 1024 * 1024); | 
|  | 372 | fileTransport->setMaxEventSize(1024 * 16); | 
|  | 373 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 374 | transportFactory = | 
|  | 375 | boost::shared_ptr<TTransportFactory>(new TPipedTransportFactory(fileTransport)); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 376 | } | 
|  | 377 |  | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 378 | boost::shared_ptr<Thread> serverThread; | 
|  | 379 | boost::shared_ptr<Thread> serverThread2; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 380 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 381 | if (serverType == "simple") { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 382 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 383 | serverThread = threadFactory->newThread(boost::shared_ptr<TServer>(new TNonblockingServer(serviceProcessor, protocolFactory, port))); | 
|  | 384 | serverThread2 = threadFactory->newThread(boost::shared_ptr<TServer>(new TNonblockingServer(serviceProcessor, protocolFactory, port+1))); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 385 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 386 | } else if (serverType == "thread-pool") { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 387 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 388 | boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 389 |  | 
|  | 390 | threadManager->threadFactory(threadFactory); | 
|  | 391 | threadManager->start(); | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 392 | serverThread = threadFactory->newThread(boost::shared_ptr<TServer>(new TNonblockingServer(serviceProcessor, protocolFactory, port, threadManager))); | 
|  | 393 | serverThread2 = threadFactory->newThread(boost::shared_ptr<TServer>(new TNonblockingServer(serviceProcessor, protocolFactory, port+1, threadManager))); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
| Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 396 | cerr << "Starting the server on port " << port << " and " << (port + 1) << endl; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 397 | serverThread->start(); | 
| Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 398 | serverThread2->start(); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 399 |  | 
|  | 400 | // If we aren't running clients, just wait forever for external clients | 
|  | 401 |  | 
|  | 402 | if (clientCount == 0) { | 
|  | 403 | serverThread->join(); | 
| Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 404 | serverThread2->join(); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 405 | } | 
|  | 406 | } | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 407 | THRIFT_SLEEP_SEC(1); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 408 |  | 
|  | 409 | if (clientCount > 0) { | 
|  | 410 |  | 
|  | 411 | Monitor monitor; | 
|  | 412 |  | 
|  | 413 | size_t threadCount = 0; | 
|  | 414 |  | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 415 | set<boost::shared_ptr<Thread> > clientThreads; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 416 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 417 | 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;} | 
|  | 422 | else {throw invalid_argument("Unknown service call "+callName);} | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 423 |  | 
| Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 424 | for (uint32_t ix = 0; ix < clientCount; ix++) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 425 |  | 
| Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 426 | boost::shared_ptr<TSocket> socket(new TSocket("127.0.0.1", port + (ix % 2))); | 
|  | 427 | boost::shared_ptr<TFramedTransport> framedSocket(new TFramedTransport(socket)); | 
|  | 428 | boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(framedSocket)); | 
|  | 429 | boost::shared_ptr<ServiceClient> serviceClient(new ServiceClient(protocol)); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 430 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 431 | clientThreads.insert(threadFactory->newThread(boost::shared_ptr<ClientThread>(new ClientThread(socket, serviceClient, monitor, threadCount, loopCount, loopType)))); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 432 | } | 
|  | 433 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 434 | for (std::set<boost::shared_ptr<Thread> >::const_iterator thread = clientThreads.begin(); thread != clientThreads.end(); thread++) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 435 | (*thread)->start(); | 
|  | 436 | } | 
|  | 437 |  | 
| Roger Meier | 5f9614c | 2010-11-21 16:59:05 +0000 | [diff] [blame] | 438 | int64_t time00; | 
|  | 439 | int64_t time01; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 440 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 441 | {Synchronized s(monitor); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 442 | threadCount = clientCount; | 
|  | 443 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 444 | cerr << "Launch "<< clientCount << " client threads" << endl; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 445 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 446 | time00 =  Util::currentTime(); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 447 |  | 
|  | 448 | monitor.notifyAll(); | 
|  | 449 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 450 | while(threadCount > 0) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 451 | monitor.wait(); | 
|  | 452 | } | 
|  | 453 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 454 | time01 =  Util::currentTime(); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 455 | } | 
|  | 456 |  | 
| Roger Meier | 5f9614c | 2010-11-21 16:59:05 +0000 | [diff] [blame] | 457 | int64_t firstTime = 9223372036854775807LL; | 
|  | 458 | int64_t lastTime = 0; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 459 |  | 
|  | 460 | double averageTime = 0; | 
| Roger Meier | 5f9614c | 2010-11-21 16:59:05 +0000 | [diff] [blame] | 461 | int64_t minTime = 9223372036854775807LL; | 
|  | 462 | int64_t maxTime = 0; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 463 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 464 | for (set<boost::shared_ptr<Thread> >::iterator ix = clientThreads.begin(); ix != clientThreads.end(); ix++) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 465 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 466 | boost::shared_ptr<ClientThread> client = boost::dynamic_pointer_cast<ClientThread>((*ix)->runnable()); | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 467 |  | 
| Roger Meier | 5f9614c | 2010-11-21 16:59:05 +0000 | [diff] [blame] | 468 | int64_t delta = client->_endTime - client->_startTime; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 469 |  | 
|  | 470 | assert(delta > 0); | 
|  | 471 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 472 | if (client->_startTime < firstTime) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 473 | firstTime = client->_startTime; | 
|  | 474 | } | 
|  | 475 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 476 | if (client->_endTime > lastTime) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 477 | lastTime = client->_endTime; | 
|  | 478 | } | 
|  | 479 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 480 | if (delta < minTime) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 481 | minTime = delta; | 
|  | 482 | } | 
|  | 483 |  | 
| Mark Slee | 3e5d2d7 | 2007-06-15 01:45:56 +0000 | [diff] [blame] | 484 | if (delta > maxTime) { | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 485 | maxTime = delta; | 
|  | 486 | } | 
|  | 487 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 488 | averageTime+= delta; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 489 | } | 
|  | 490 |  | 
|  | 491 | averageTime /= clientCount; | 
|  | 492 |  | 
| Konrad Grochowski | 240120c | 2014-11-18 11:33:31 +0100 | [diff] [blame^] | 493 |  | 
|  | 494 | cout <<  "workers :" << workerCount << ", client : " << clientCount << ", loops : " << loopCount << ", rate : " << (clientCount * loopCount * 1000) / ((double)(time01 - time00)) << endl; | 
| Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 495 |  | 
|  | 496 | 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 | } | 
|  | 501 | cerr << "done." << endl; | 
|  | 502 | } | 
|  | 503 |  | 
|  | 504 | return 0; | 
|  | 505 | } |