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