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