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