1e2512892SShuo Chen#include <stdio.h>
2246b2f80SShuo Chen#include <sys/time.h>
3246b2f80SShuo Chen#include <unistd.h>
4e2512892SShuo Chen
5e2512892SShuo Chen#include "Acceptor.h"
6e2512892SShuo Chen#include "InetAddress.h"
7e2512892SShuo Chen#include "Socket.h"
8e2512892SShuo Chen#include "TcpStream.h"
9e2512892SShuo Chen
10e2512892SShuo Chen
11e2512892SShuo Chenvoid dump(const char* filename)
12e2512892SShuo Chen{
13e2512892SShuo Chen  char buf[65536];
14e2512892SShuo Chen  FILE* fp = fopen(filename, "r");
15e2512892SShuo Chen  if (fp)
16e2512892SShuo Chen  {
17e2512892SShuo Chen    ssize_t nr;
18e2512892SShuo Chen    while ( (nr = fread(buf, 1, sizeof buf, fp)) > 0)
19e2512892SShuo Chen      fwrite(buf, 1, nr, stdout);
20e2512892SShuo Chen
21e2512892SShuo Chen    fclose(fp);
22e2512892SShuo Chen  }
23e2512892SShuo Chen}
24e2512892SShuo Chen
25246b2f80SShuo Chenvoid snapshot(const char* name, int N)
26e2512892SShuo Chen{
27246b2f80SShuo Chen  printf("===== %d %s =====\n", N, name);
28e2512892SShuo Chen  dump("/proc/meminfo");
29e2512892SShuo Chen  dump("/proc/slabinfo");
30e2512892SShuo Chen}
31e2512892SShuo Chen
32e2512892SShuo Chenint main(int argc, char* argv[])
33e2512892SShuo Chen{
34e2512892SShuo Chen  const int N = argc > 1 ? atoi(argv[1]) : 1000;
35e2512892SShuo Chen
36246b2f80SShuo Chen  snapshot("started", N);
37e2512892SShuo Chen
38e2512892SShuo Chen  InetAddress listenAddr(2222);
39e2512892SShuo Chen  Acceptor acceptor(listenAddr);
40246b2f80SShuo Chen  snapshot("acceptor created", 1);
41e2512892SShuo Chen
4224ca08a8SShuo Chen  InetAddress serverAddr("127.0.0.1", 2222);
4324ca08a8SShuo Chen
44e2512892SShuo Chen  std::vector<Socket> clients;
45246b2f80SShuo Chen  clients.reserve(N);
46e2512892SShuo Chen  for (int i = 0; i < N; ++i)
4724ca08a8SShuo Chen    clients.push_back(Socket::createTCP(serverAddr.family()));
48246b2f80SShuo Chen  snapshot("clients created", N);
49e2512892SShuo Chen
50e2512892SShuo Chen  std::vector<Socket> servers;
51246b2f80SShuo Chen  servers.reserve(N);
52e2512892SShuo Chen  for (int i = 0; i < N; ++i)
53e2512892SShuo Chen  {
54246b2f80SShuo Chen    char buf[64];
5577bc5cb1SShuo Chen    const int ports_per_ip = 16384;
5677bc5cb1SShuo Chen    int clientIP = i / ports_per_ip;
57246b2f80SShuo Chen    snprintf(buf, sizeof buf, "127.1.%d.%d", clientIP / 128, clientIP % 128);
5877bc5cb1SShuo Chen    InetAddress localAddr(buf, 10000 + i % ports_per_ip);
59246b2f80SShuo Chen    clients[i].bindOrDie(localAddr);
60246b2f80SShuo Chen    if (i % 10000 == 0) {
61246b2f80SShuo Chen      struct timeval tv;
62246b2f80SShuo Chen      gettimeofday(&tv, NULL);
63246b2f80SShuo Chen      fprintf(stderr, "%ld.%06ld Client %d bind to %s\n", tv.tv_sec, tv.tv_usec, i, buf);
64246b2f80SShuo Chen    }
65e2512892SShuo Chen    if (clients[i].connect(serverAddr))
66e2512892SShuo Chen    {
67e2512892SShuo Chen      perror("connect");
68e2512892SShuo Chen      break;
69e2512892SShuo Chen    }
70e2512892SShuo Chen    servers.push_back(acceptor.acceptSocketOrDie());
71e2512892SShuo Chen  }
72246b2f80SShuo Chen  snapshot("clients connected", N);
73e2512892SShuo Chen
74e2512892SShuo Chen  // TODO: epoll
75e2512892SShuo Chen
76e2512892SShuo Chen  servers.clear();
77246b2f80SShuo Chen  snapshot("servers disconnected", N);
78e2512892SShuo Chen
79e2512892SShuo Chen  clients.clear();
80246b2f80SShuo Chen  snapshot("clients disconnected", N);
81e2512892SShuo Chen}
82