summaryrefslogtreecommitdiff
path: root/Sockets/tests/stressclient.cpp
blob: 41878686327a031a614a794758c86c91b4e586b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/**
 **  \file stressclient.cpp
 **  \date  2006-10-02
 **  \author grymse@alhem.net
**/
/*
Copyright (C) 2006  Anders Hedstrom

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#ifdef _WIN32
#pragma warning(disable:4786)
#endif
#include <StdoutLog.h>
#include <ListenSocket.h>
#include <SocketHandler.h>
#include <TcpSocket.h>
#include <Utility.h>
#ifndef _WIN32
#include <signal.h>
#include <stdint.h>
#else
typedef __int64 int64_t;
#endif
#include <HttpGetSocket.h>


static  double          g_min_time = 10000;
static  double          g_max_time = 0;
static  double          g_tot_time = 0;
static  int             g_ant = 0;
static  double          g_min_time2 = 10000;
static  double          g_max_time2 = 0;
static  double          g_tot_time2 = 0;
static  int             g_ant2 = 0;
static  bool            gQuit = false;
static  size_t          g_max_connections = 0;
static  std::string     gHost = "localhost";
static  port_t          gPort = 2222;
static  bool            g_b_flood = false;
static  bool            g_b_off = false;
static  bool            g_b_limit = false;
static  int64_t         gBytesIn = 0;
static  int64_t         gBytesOut = 0;
static  bool            g_b_repeat = false;
static  std::string     g_data;
static  size_t          g_data_size = 1024;
static  bool            g_b_stop = false;
#ifdef HAVE_OPENSSL
static  bool            g_b_ssl = false;
#endif
static  bool            g_b_instant = false;
static	struct timeval	g_t_start;


/**
 * Return time difference between two struct timeval's, in seconds
 * \param t0 start time
 * \param t  end time
 */
double Diff(struct timeval t0,struct timeval t)
{
  t.tv_sec -= t0.tv_sec;
  t.tv_usec -= t0.tv_usec;
  if (t.tv_usec < 0)
  {
    t.tv_usec += 1000000;
    t.tv_sec -= 1;
  }
  return t.tv_sec + (double)t.tv_usec / 1000000;
}


void gettime(struct timeval *p, struct timezone *)
{
#ifdef _WIN32
  FILETIME ft; // Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
  GetSystemTimeAsFileTime(&ft);
  uint64_t tt;
  memcpy(&tt, &ft, sizeof(tt));
  tt /= 10;
  p->tv_sec = tt / 1000000;
  p->tv_usec = tt % 1000000;
#else
  gettimeofday(p, NULL);
#endif
}


void printreport()
{
  struct timeval tv;
  gettime(&tv, NULL);
  double rt = Diff(g_t_start, tv);
  g_t_start = tv;
  //
  printf("\n");
  if (g_ant)
    printf("connect; ant: %5d min: %.4f max: %.4f med: %.4f\n", g_ant, g_min_time, g_max_time, g_tot_time / g_ant);
  if (g_ant2)
    printf("reply;   ant: %5d min: %.4f max: %.4f med: %.4f\n", g_ant2, g_min_time2, g_max_time2, g_tot_time2 / g_ant2);
  double mbi = (double)gBytesIn / 1024;
  mbi /= 1024;
  mbi /= rt;
  double mbo = (double)gBytesOut / 1024;
  mbo /= 1024;
  mbo /= rt;
  printf("bytes in: %lld", gBytesIn);
  printf(" (%.2f MB/sec)", mbi);
  printf("  bytes out: %lld", gBytesOut);
  printf(" (%.2f MB/sec)", mbo);
  printf("  time: %.2f sec\n", rt);
}


void printreport_reset()
{
  printreport();
  //
  g_min_time = 10000;
  g_max_time = 0;
  g_tot_time = 0;
  g_ant = 0;
  g_min_time2 = 10000;
  g_max_time2 = 0;
  g_tot_time2 = 0;
  g_ant2 = 0;
  gBytesIn = gBytesOut = 0;
}


class MySocket : public TcpSocket
{
public:
  MySocket(ISocketHandler& h,bool one) : TcpSocket(h), m_b_client(false), m_b_one(one), m_b_created(false), m_b_active(false) {
    gettime(&m_create, NULL);
    SetLineProtocol();
#ifdef HAVE_OPENSSL
    if (g_b_ssl)
      EnableSSL();
#endif
    if (g_max_connections && !m_b_one && Handler().GetCount() >= g_max_connections)
    {
      fprintf(stderr, "\nConnection limit reached: %d, continuing in single connection stress mode\n", (int)g_max_connections);
      if (g_b_off)
        printreport_reset();
      g_b_limit = true;
      m_b_one = true;
      //
      g_b_flood = g_b_repeat;
    }
    if (!m_b_one && Handler().GetCount() >= FD_SETSIZE - 17)
    {
      fprintf(stderr, "\nFD_SETSIZE connection limit reached: %d, continuing in single connection stress mode\n", (int)Handler().GetCount());
      if (g_b_off)
        printreport_reset();
      g_b_limit = true;
      m_b_one = true;
      //
      g_b_flood = g_b_repeat;
    }
  }
  ~MySocket() {
  }

  void OnConnect() {
    gettime(&m_connect, NULL);
    m_b_active = true;
    {
      double tconnect = Diff(m_create, m_connect);
      //
      g_min_time = tconnect < g_min_time ? tconnect : g_min_time;
      g_max_time = tconnect > g_max_time ? tconnect : g_max_time;
      g_tot_time += tconnect;
      g_ant += 1;
    }
    SendBlock();
    m_b_client = true;
  }

  void SendBlock() {
    gettime(&m_send, NULL);
    Send(g_data + "\n");
  }

  void OnLine(const std::string& line) {
    gettime(&m_reply, NULL);
    m_b_active = true;
    {
      double treply = Diff(m_send, m_reply);
      //
      g_min_time2 = treply < g_min_time2 ? treply : g_min_time2;
      g_max_time2 = treply > g_max_time2 ? treply : g_max_time2;
      g_tot_time2 += treply;
      g_ant2 += 1;
    }
    //
    if (line != g_data)
    {
      fprintf(stderr, "\n%s\n%s\n", line.c_str(), g_data.c_str());
      fprintf(stderr, "(reply did not match data - exiting)\n");
      exit(-1);
    }
    //
    gBytesIn += GetBytesReceived(true);
    gBytesOut += GetBytesSent(true);
    if (m_b_one)
    {
      SetCloseAndDelete();
    }
    else
    if (g_b_repeat && g_b_limit)
    {
      SendBlock();
    }
    // add another
    if (!m_b_created && (!g_b_limit || !g_b_off) && !g_b_instant)
    {
      MySocket *p = new MySocket(Handler(), m_b_one);
      p -> SetDeleteByHandler();
      p -> Open(gHost, gPort);
      Handler().Add(p);
      m_b_created = true;
    }
  }

  bool IsActive() {
    bool b = m_b_active;
    m_b_active = false;
    return b;
  }

private:
  bool m_b_client;
  bool m_b_one;
  bool m_b_created;
  bool m_b_active;
  struct timeval m_create;
  struct timeval m_connect;
  struct timeval m_send;
  struct timeval m_reply;
};


class MyHttpSocket : public HttpGetSocket
{
public:
  MyHttpSocket(ISocketHandler& h,const std::string& url) : HttpGetSocket(h,url), m_url(url) {
    gettime(&m_create, NULL);
    AddResponseHeader("content-length", Utility::l2string(g_data_size));
  }
  ~MyHttpSocket() {}

  void OnConnect() {
    gettime(&m_connect, NULL);
    {
      double tconnect = Diff(m_create, m_connect);
      //
      g_min_time = tconnect < g_min_time ? tconnect : g_min_time;
      g_max_time = tconnect > g_max_time ? tconnect : g_max_time;
      g_tot_time += tconnect;
      g_ant += 1;
    }
    gettime(&m_send, NULL);

    // send request header
    HttpGetSocket::OnConnect();

    // send body
    Send(g_data);
  }

  void OnContent() {
    gettime(&m_reply, NULL);
    {
      double treply = Diff(m_send, m_reply);
      //
      g_min_time2 = treply < g_min_time2 ? treply : g_min_time2;
      g_max_time2 = treply > g_max_time2 ? treply : g_max_time2;
      g_tot_time2 += treply;
      g_ant2 += 1;
    }
    CreateNew();
  }
  void CreateNew() {
    if (g_b_off)
      return;
    MyHttpSocket *p = new MyHttpSocket(Handler(), m_url);
    p -> SetDeleteByHandler();
    Handler().Add(p);
    SetCloseAndDelete();
  }

private:
  std::string m_url;
  struct timeval m_create;
  struct timeval m_connect;
  struct timeval m_send;
  struct timeval m_reply;
};


#ifndef _WIN32
void sigint(int)
{
  printreport();
  gQuit = true;
}


void sigusr1(int)
{
  g_b_flood = true;
}


void sigusr2(int)
{
  printreport_reset();
}
#endif


class MyHandler : public SocketHandler
{
public:
  MyHandler() : SocketHandler() {
  }
  MyHandler(StdoutLog *p) : SocketHandler(p) {
  }
  ~MyHandler() {
  }
  void Flood() {
    for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
    {
      Socket *p0 = it -> second;
      MySocket *p = dynamic_cast<MySocket *>(p0);
      if (p)
      {
        p -> SendBlock();
      }
    }
  }
  void Report() {
    int ant = 0;
    int act = 0;
    for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
    {
      MySocket *p = dynamic_cast<MySocket *>(it -> second);
      if (p)
      {
        ant++;
        if (p -> IsActive())
        {
          act++;
        }
      }
    }
    printf("Number of //stress// sockets: %d  Active: %d\n", ant, act);
  }
};


int main(int argc,char *argv[])
{
  bool many = false;
  bool one = false;
  bool enableLog = false;
  bool http = false;
  std::string url;
  time_t report_period = 10;
  for (int i = 1; i < argc; i++)
  {
    if (!strcmp(argv[i], "-many"))
      many = true;
    if (!strcmp(argv[i], "-one"))
      one = true;
    if (*argv[i] == '-' && strlen(argv[i]) > 1 && isdigit(argv[i][1]) )
      g_max_connections = atoi(argv[i] + 1);
    if (!strcmp(argv[i], "-host") && i < argc - 1)
      gHost = argv[++i];
    if (!strcmp(argv[i], "-port") && i < argc - 1)
      gPort = atoi(argv[++i]);
    if (!strcmp(argv[i], "-off"))
      g_b_off = true;
    if (!strcmp(argv[i], "-repeat"))
      g_b_repeat = true;
    if (!strcmp(argv[i], "-size") && i < argc - 1)
      g_data_size = atoi(argv[++i]);
    if (!strcmp(argv[i], "-log"))
      enableLog = true;
    if (!strcmp(argv[i], "-time") && i < argc - 1)
      report_period = atoi(argv[++i]);
    if (!strcmp(argv[i], "-stop"))
      g_b_stop = true;
#ifdef HAVE_OPENSSL
    if (!strcmp(argv[i], "-ssl"))
      g_b_ssl = true;
#endif
    if (!strcmp(argv[i], "-instant"))
      g_b_instant = true;
    if (!strcmp(argv[i], "-http"))
      http = true;
    if (!strcmp(argv[i], "-url") && i < argc - 1)
      url = argv[++i];
  }
  if (argc < 2 || (!many && !one && !g_max_connections && !http) )
  {
    printf("Usage: %s [mode] [options]\n", *argv);
    printf("  Modes (only use one of these):\n");
    printf("    -many      start max number of connections\n");
    printf("    -one       open - close - repeat\n");
    printf("    -nn        open nn connections, then start -one mode\n");
    printf("    -http      send/receive http request/response\n");
    printf("  Options:\n");
    printf("    -host xx   host to connect to\n");
    printf("    -port nn   port number to connection to\n");
    printf("    -off       turn off new connections when connection limit reached\n");
    printf("    -repeat    send new block when reply is received\n");
    printf("    -size nn   size of block to send, default is 1024 bytes\n");
    printf("    -log       enable debug log\n");
    printf("    -time nn   time between reports, default 10s\n");
    printf("    -stop      stop after time elapsed\n");
    printf("    -instant   create all sockets at once\n");
#ifdef HAVE_OPENSSL
    printf("    -ssl       use ssl\n");
#endif
    printf("    -url xx    url to use in http mode (default http://<host>:<port>/)\n");
    exit(-1);
  }
  fprintf(stderr, "Using data size: %d bytes\n", (int)g_data_size);
  std::string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  while (g_data.size() < g_data_size)
  {
    g_data += chars[rand() % chars.size()];
  }
#ifndef _WIN32
  signal(SIGINT, sigint);
  signal(SIGUSR1, sigusr1);
  signal(SIGUSR2, sigusr2);
  signal(SIGPIPE, SIG_IGN);
#endif
  StdoutLog *log = enableLog ? new StdoutLog() : NULL;
  MyHandler h(log);
  if (http)
  {
    if (!url.size())
    {
      url = "http://" + gHost + ":" + Utility::l2string(gPort) + "/";
    }
    MyHttpSocket *s = new MyHttpSocket(h, url);
    s -> SetDeleteByHandler();
    h.Add(s);
  }
  else
  if (g_b_instant)
  {
    for (size_t i = 0; i < g_max_connections; i++)
    {
      MySocket *s = new MySocket(h, one);
      s -> SetDeleteByHandler();
      s -> Open(gHost, gPort);
      h.Add(s);
    }
    g_b_limit = true;
  }
  else
  {
    MySocket *s = new MySocket(h, one);
    s -> SetDeleteByHandler();
    s -> Open(gHost, gPort);
    h.Add(s);
  }
  time_t t = time(NULL);
  gettime(&g_t_start, NULL);
  while (!gQuit)
  {
    h.Select(1, 0);
    if (g_b_flood)
    {
      fprintf(stderr, "\nFlooding\n");
      h.Flood();
      g_b_flood = false;
    }
    if (time(NULL) - t >= report_period) // report
    {
      t = time(NULL);
      printreport_reset();
      h.Report();
      if (g_b_stop)
      {
        gQuit = true;
      }
    }
  }
  fprintf(stderr, "\nExiting...\n");
  if (log)
  {
//    delete log;
  }
  return 0;
}