Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 1 | #include <concurrency/ThreadManager.h> |
| 2 | #include <concurrency/PosixThreadFactory.h> |
| 3 | #include <concurrency/Monitor.h> |
| 4 | #include <concurrency/Util.h> |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 5 | #include <concurrency/Mutex.h> |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 6 | #include <protocol/TBinaryProtocol.h> |
| 7 | #include <server/TSimpleServer.h> |
| 8 | #include <server/TThreadPoolServer.h> |
| 9 | #include <transport/TServerSocket.h> |
| 10 | #include <transport/TSocket.h> |
Mark Slee | d717347 | 2006-10-25 19:52:10 +0000 | [diff] [blame] | 11 | #include <transport/TTransportUtils.h> |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 12 | #include <transport/TBufferedRouterTransport.h> |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 13 | #include <transport/TBufferedFileWriter.h> |
| 14 | |
| 15 | #include "Service.h" |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 16 | |
| 17 | #include <iostream> |
| 18 | #include <set> |
| 19 | #include <stdexcept> |
| 20 | #include <sstream> |
| 21 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 22 | #include <map> |
| 23 | #include <ext/hash_map> |
| 24 | using __gnu_cxx::hash_map; |
| 25 | using __gnu_cxx::hash; |
| 26 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 27 | using namespace std; |
| 28 | |
| 29 | using namespace facebook::thrift; |
| 30 | using namespace facebook::thrift::protocol; |
| 31 | using namespace facebook::thrift::transport; |
| 32 | using namespace facebook::thrift::server; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 33 | using namespace facebook::thrift::concurrency; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace test::stress; |
| 36 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 37 | struct eqstr { |
| 38 | bool operator()(const char* s1, const char* s2) const { |
| 39 | return strcmp(s1, s2) == 0; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | struct ltstr { |
| 44 | bool operator()(const char* s1, const char* s2) const { |
| 45 | return strcmp(s1, s2) < 0; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | // typedef hash_map<const char*, int, hash<const char*>, eqstr> count_map; |
| 51 | typedef map<const char*, int, ltstr> count_map; |
| 52 | |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 53 | class Server : public ServiceIf { |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 54 | public: |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 55 | Server() {} |
| 56 | |
| 57 | void count(const char* method) { |
| 58 | MutexMonitor m(lock_); |
| 59 | int ct = counts_[method]; |
| 60 | counts_[method] = ++ct; |
| 61 | } |
| 62 | |
| 63 | void echoVoid() { |
| 64 | count("echoVoid"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | count_map getCount() { |
| 69 | MutexMonitor m(lock_); |
| 70 | return counts_; |
| 71 | } |
| 72 | |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 73 | int8_t echoByte(int8_t arg) {return arg;} |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 74 | int32_t echoI32(int32_t arg) {return arg;} |
| 75 | int64_t echoI64(int64_t arg) {return arg;} |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 76 | string echoString(string arg) {return arg;} |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 77 | vector<int8_t> echoList(vector<int8_t> arg) {return arg;} |
| 78 | set<int8_t> echoSet(set<int8_t> arg) {return arg;} |
| 79 | map<int8_t, int8_t> echoMap(map<int8_t, int8_t> arg) {return arg;} |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 80 | |
| 81 | private: |
| 82 | count_map counts_; |
| 83 | Mutex lock_; |
| 84 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | class ClientThread: public Runnable { |
| 88 | public: |
| 89 | |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 90 | ClientThread(shared_ptr<TTransport>transport, shared_ptr<ServiceClient> client, Monitor& monitor, size_t& workerCount, size_t loopCount, TType loopType) : |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 91 | _transport(transport), |
| 92 | _client(client), |
| 93 | _monitor(monitor), |
| 94 | _workerCount(workerCount), |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 95 | _loopCount(loopCount), |
| 96 | _loopType(loopType) |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 97 | {} |
| 98 | |
| 99 | void run() { |
| 100 | |
| 101 | // Wait for all worker threads to start |
| 102 | |
| 103 | {Synchronized s(_monitor); |
| 104 | while(_workerCount == 0) { |
| 105 | _monitor.wait(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | _startTime = Util::currentTime(); |
| 110 | |
| 111 | _transport->open(); |
| 112 | |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 113 | switch(_loopType) { |
| 114 | case T_VOID: loopEchoVoid(); break; |
| 115 | case T_BYTE: loopEchoByte(); break; |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 116 | case T_I32: loopEchoI32(); break; |
| 117 | case T_I64: loopEchoI64(); break; |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 118 | case T_STRING: loopEchoString(); break; |
| 119 | default: cerr << "Unexpected loop type" << _loopType << endl; break; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | _endTime = Util::currentTime(); |
| 123 | |
| 124 | _transport->close(); |
| 125 | |
| 126 | _done = true; |
| 127 | |
| 128 | {Synchronized s(_monitor); |
| 129 | |
| 130 | _workerCount--; |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 131 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 132 | if(_workerCount == 0) { |
| 133 | |
| 134 | _monitor.notify(); |
| 135 | } |
| 136 | } |
| 137 | } |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 138 | |
| 139 | void loopEchoVoid() { |
| 140 | for(size_t ix = 0; ix < _loopCount; ix++) { |
| 141 | _client->echoVoid(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void loopEchoByte() { |
| 146 | for(size_t ix = 0; ix < _loopCount; ix++) { |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 147 | int8_t arg = 1; |
| 148 | int8_t result; |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 149 | result =_client->echoByte(arg); |
| 150 | assert(result == arg); |
| 151 | } |
| 152 | } |
| 153 | |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 154 | void loopEchoI32() { |
| 155 | for(size_t ix = 0; ix < _loopCount; ix++) { |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 156 | int32_t arg = 1; |
| 157 | int32_t result; |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 158 | result =_client->echoI32(arg); |
| 159 | assert(result == arg); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void loopEchoI64() { |
| 164 | for(size_t ix = 0; ix < _loopCount; ix++) { |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 165 | int64_t arg = 1; |
| 166 | int64_t result; |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 167 | result =_client->echoI64(arg); |
| 168 | assert(result == arg); |
| 169 | } |
| 170 | } |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 171 | |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 172 | void loopEchoString() { |
| 173 | for(size_t ix = 0; ix < _loopCount; ix++) { |
| 174 | string arg = "hello"; |
| 175 | string result; |
| 176 | result =_client->echoString(arg); |
| 177 | assert(result == arg); |
| 178 | } |
| 179 | } |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 180 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 181 | shared_ptr<TTransport> _transport; |
| 182 | shared_ptr<ServiceClient> _client; |
| 183 | Monitor& _monitor; |
| 184 | size_t& _workerCount; |
| 185 | size_t _loopCount; |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 186 | TType _loopType; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 187 | long long _startTime; |
| 188 | long long _endTime; |
| 189 | bool _done; |
| 190 | Monitor _sleep; |
| 191 | }; |
| 192 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 193 | int main(int argc, char **argv) { |
| 194 | |
| 195 | int port = 9090; |
| 196 | string serverType = "thread-pool"; |
| 197 | string protocolType = "binary"; |
| 198 | size_t workerCount = 4; |
| 199 | size_t clientCount = 10; |
| 200 | size_t loopCount = 10000; |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 201 | TType loopType = T_VOID; |
| 202 | string callName = "echoVoid"; |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 203 | bool runServer = true; |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 204 | bool logRequests = false; |
| 205 | string requestLogPath = "./requestlog.tlog"; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 206 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 207 | ostringstream usage; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 208 | |
| 209 | usage << |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 210 | argv[0] << " [--port=<port number>] [--server] [--server-type=<server-type>] [--protocol-type=<protocol-type>] [--workers=<worker-count>] [--clients=<client-count>] [--loop=<loop-count>]" << endl << |
| 211 | "\tclients Number of client threads to create - 0 implies no clients, i.e. server only. Default is " << clientCount << endl << |
| 212 | "\thelp Prints this help text." << endl << |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 213 | "\tcall Service method to call. Default is " << callName << endl << |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 214 | "\tloop The number of remote thrift calls each client makes. Default is " << loopCount << endl << |
| 215 | "\tport The port the server and clients should bind to for thrift network connections. Default is " << port << endl << |
| 216 | "\tserver Run the Thrift server in this process. Default is " << runServer << endl << |
| 217 | "\tserver-type Type of server, \"simple\" or \"thread-pool\". Default is " << serverType << endl << |
| 218 | "\tprotocol-type Type of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl << |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 219 | "\tlog-request Log all request to ./requestlog.tlog. Default is " << logRequests << endl << |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 220 | "\tworkers Number of thread pools workers. Only valid for thread-pool server type. Default is " << workerCount << endl; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 221 | |
| 222 | map<string, string> args; |
| 223 | |
| 224 | for(int ix = 1; ix < argc; ix++) { |
| 225 | |
| 226 | string arg(argv[ix]); |
| 227 | |
| 228 | if(arg.compare(0,2, "--") == 0) { |
| 229 | |
| 230 | size_t end = arg.find_first_of("=", 2); |
| 231 | |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 232 | string key = string(arg, 2, end - 2); |
| 233 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 234 | if(end != string::npos) { |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 235 | args[key] = string(arg, end + 1); |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 236 | } else { |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 237 | args[key] = "true"; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 238 | } |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 239 | } else { |
| 240 | throw invalid_argument("Unexcepted command line token: "+arg); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | try { |
| 245 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 246 | if(!args["clients"].empty()) { |
| 247 | clientCount = atoi(args["clients"].c_str()); |
| 248 | } |
| 249 | |
| 250 | if(!args["help"].empty()) { |
| 251 | cerr << usage.str(); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | if(!args["loop"].empty()) { |
| 256 | loopCount = atoi(args["loop"].c_str()); |
| 257 | } |
| 258 | |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 259 | if(!args["call"].empty()) { |
| 260 | callName = args["call"]; |
| 261 | } |
| 262 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 263 | if(!args["port"].empty()) { |
| 264 | port = atoi(args["port"].c_str()); |
| 265 | } |
| 266 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 267 | if(!args["server"].empty()) { |
| 268 | runServer = args["server"] == "true"; |
| 269 | } |
| 270 | |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 271 | if(!args["log-request"].empty()) { |
| 272 | logRequests = args["log-request"] == "true"; |
| 273 | } |
| 274 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 275 | if(!args["server-type"].empty()) { |
| 276 | serverType = args["server-type"]; |
| 277 | |
| 278 | if(serverType == "simple") { |
| 279 | |
| 280 | } else if(serverType == "thread-pool") { |
| 281 | |
| 282 | } else { |
| 283 | |
| 284 | throw invalid_argument("Unknown server type "+serverType); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if(!args["workers"].empty()) { |
| 289 | workerCount = atoi(args["workers"].c_str()); |
| 290 | } |
| 291 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 292 | } catch(exception& e) { |
| 293 | cerr << e.what() << endl; |
| 294 | cerr << usage; |
| 295 | } |
| 296 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 297 | shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
| 298 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 299 | // Dispatcher |
| 300 | shared_ptr<Server> serviceHandler(new Server()); |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 301 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 302 | if(runServer) { |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 303 | |
Mark Slee | d717347 | 2006-10-25 19:52:10 +0000 | [diff] [blame] | 304 | shared_ptr<ServiceProcessor> serviceProcessor(new ServiceProcessor(serviceHandler)); |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 305 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 306 | // Transport |
| 307 | shared_ptr<TServerSocket> serverSocket(new TServerSocket(port)); |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 308 | |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 309 | // Transport Factory |
| 310 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); |
| 311 | |
Mark Slee | d717347 | 2006-10-25 19:52:10 +0000 | [diff] [blame] | 312 | // Protocol Factory |
| 313 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 314 | |
| 315 | if (logRequests) { |
| 316 | // initialize the log file |
| 317 | shared_ptr<TBufferedFileWriter> bufferedFileWriter(new TBufferedFileWriter(requestLogPath, 1000)); |
| 318 | bufferedFileWriter->setChunkSize(2 * 1024 * 1024); |
| 319 | bufferedFileWriter->setMaxEventSize(1024 * 16); |
| 320 | |
| 321 | transportFactory = shared_ptr<TTransportFactory>(new TBufferedRouterTransportFactory(bufferedFileWriter)); |
| 322 | } |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 323 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 324 | shared_ptr<Thread> serverThread; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 325 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 326 | if(serverType == "simple") { |
| 327 | |
Mark Slee | d717347 | 2006-10-25 19:52:10 +0000 | [diff] [blame] | 328 | serverThread = threadFactory->newThread(shared_ptr<TServer>(new TSimpleServer(serviceProcessor, serverSocket, transportFactory, protocolFactory))); |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 329 | |
| 330 | } else if(serverType == "thread-pool") { |
| 331 | |
| 332 | shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount); |
| 333 | |
| 334 | threadManager->threadFactory(threadFactory); |
Mark Slee | d717347 | 2006-10-25 19:52:10 +0000 | [diff] [blame] | 335 | threadManager->start(); |
| 336 | serverThread = threadFactory->newThread(shared_ptr<TServer>(new TThreadPoolServer(serviceProcessor, serverSocket, transportFactory, protocolFactory, threadManager))); |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | cerr << "Starting the server on port " << port << endl; |
| 340 | |
| 341 | serverThread->start(); |
| 342 | |
| 343 | // If we aren't running clients, just wait forever for external clients |
| 344 | |
Mark Slee | d717347 | 2006-10-25 19:52:10 +0000 | [diff] [blame] | 345 | if (clientCount == 0) { |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 346 | serverThread->join(); |
| 347 | } |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Mark Slee | d717347 | 2006-10-25 19:52:10 +0000 | [diff] [blame] | 350 | if (clientCount > 0) { |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 351 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 352 | Monitor monitor; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 353 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 354 | size_t threadCount = 0; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 355 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 356 | set<shared_ptr<Thread> > clientThreads; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 357 | |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 358 | if(callName == "echoVoid") { loopType = T_VOID;} |
| 359 | else if(callName == "echoByte") { loopType = T_BYTE;} |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 360 | else if(callName == "echoI32") { loopType = T_I32;} |
| 361 | else if(callName == "echoI64") { loopType = T_I64;} |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 362 | else if(callName == "echoString") { loopType = T_STRING;} |
| 363 | else {throw invalid_argument("Unknown service call "+callName);} |
| 364 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 365 | for(size_t ix = 0; ix < clientCount; ix++) { |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 366 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 367 | shared_ptr<TSocket> socket(new TSocket("127.0.01", port)); |
| 368 | shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket, 2048)); |
Mark Slee | d717347 | 2006-10-25 19:52:10 +0000 | [diff] [blame] | 369 | shared_ptr<TProtocol> protocol(new TBinaryProtocol(bufferedSocket, bufferedSocket)); |
| 370 | shared_ptr<ServiceClient> serviceClient(new ServiceClient(protocol)); |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 371 | |
Aditya Agarwal | 3950f47 | 2006-10-11 02:50:15 +0000 | [diff] [blame] | 372 | clientThreads.insert(threadFactory->newThread(shared_ptr<ClientThread>(new ClientThread(socket, serviceClient, monitor, threadCount, loopCount, loopType)))); |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 373 | } |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 374 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 375 | for(std::set<shared_ptr<Thread> >::const_iterator thread = clientThreads.begin(); thread != clientThreads.end(); thread++) { |
| 376 | (*thread)->start(); |
| 377 | } |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 378 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 379 | long long time00; |
| 380 | long long time01; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 381 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 382 | {Synchronized s(monitor); |
| 383 | threadCount = clientCount; |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 384 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 385 | cerr << "Launch "<< clientCount << " client threads" << endl; |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 386 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 387 | time00 = Util::currentTime(); |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 388 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 389 | monitor.notifyAll(); |
| 390 | |
| 391 | while(threadCount > 0) { |
| 392 | monitor.wait(); |
| 393 | } |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 394 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 395 | time01 = Util::currentTime(); |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 396 | } |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 397 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 398 | long long firstTime = 9223372036854775807LL; |
| 399 | long long lastTime = 0; |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 400 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 401 | double averageTime = 0; |
| 402 | long long minTime = 9223372036854775807LL; |
| 403 | long long maxTime = 0; |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 404 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 405 | for(set<shared_ptr<Thread> >::iterator ix = clientThreads.begin(); ix != clientThreads.end(); ix++) { |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 406 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 407 | shared_ptr<ClientThread> client = dynamic_pointer_cast<ClientThread>((*ix)->runnable()); |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 408 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 409 | long long delta = client->_endTime - client->_startTime; |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 410 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 411 | assert(delta > 0); |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 412 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 413 | if(client->_startTime < firstTime) { |
| 414 | firstTime = client->_startTime; |
| 415 | } |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 416 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 417 | if(client->_endTime > lastTime) { |
| 418 | lastTime = client->_endTime; |
| 419 | } |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 420 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 421 | if(delta < minTime) { |
| 422 | minTime = delta; |
| 423 | } |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 424 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 425 | if(delta > maxTime) { |
| 426 | maxTime = delta; |
| 427 | } |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 428 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 429 | averageTime+= delta; |
| 430 | } |
| 431 | |
| 432 | averageTime /= clientCount; |
| 433 | |
| 434 | |
| 435 | cout << "workers :" << workerCount << ", client : " << clientCount << ", loops : " << loopCount << ", rate : " << (clientCount * loopCount * 1000) / ((double)(time01 - time00)) << endl; |
| 436 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame^] | 437 | count_map count = serviceHandler->getCount(); |
| 438 | count_map::iterator iter; |
| 439 | for (iter = count.begin(); iter != count.end(); ++iter) { |
| 440 | printf("%s => %d\n", iter->first, iter->second); |
| 441 | } |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 442 | cerr << "done." << endl; |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 443 | } |
Marc Slemko | 056f9ba | 2006-08-17 02:59:05 +0000 | [diff] [blame] | 444 | |
Marc Slemko | 3ea0033 | 2006-08-17 01:11:13 +0000 | [diff] [blame] | 445 | return 0; |
| 446 | } |