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