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