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