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