footprint.cc revision e2512892
1e2512892SShuo Chen#include <stdio.h>
2e2512892SShuo Chen
3e2512892SShuo Chen#include "Acceptor.h"
4e2512892SShuo Chen#include "InetAddress.h"
5e2512892SShuo Chen#include "Socket.h"
6e2512892SShuo Chen#include "TcpStream.h"
7e2512892SShuo Chen
8e2512892SShuo Chen
9e2512892SShuo Chenvoid dump(const char* filename)
10e2512892SShuo Chen{
11e2512892SShuo Chen  char buf[65536];
12e2512892SShuo Chen  FILE* fp = fopen(filename, "r");
13e2512892SShuo Chen  if (fp)
14e2512892SShuo Chen  {
15e2512892SShuo Chen    ssize_t nr;
16e2512892SShuo Chen    while ( (nr = fread(buf, 1, sizeof buf, fp)) > 0)
17e2512892SShuo Chen      fwrite(buf, 1, nr, stdout);
18e2512892SShuo Chen
19e2512892SShuo Chen    fclose(fp);
20e2512892SShuo Chen  }
21e2512892SShuo Chen}
22e2512892SShuo Chen
23e2512892SShuo Chenvoid snapshot(const char* name)
24e2512892SShuo Chen{
25e2512892SShuo Chen  printf("===== %s =====\n", name);
26e2512892SShuo Chen  dump("/proc/meminfo");
27e2512892SShuo Chen  dump("/proc/slabinfo");
28e2512892SShuo Chen}
29e2512892SShuo Chen
30e2512892SShuo Chenint main(int argc, char* argv[])
31e2512892SShuo Chen{
32e2512892SShuo Chen  const int N = argc > 1 ? atoi(argv[1]) : 1000;
33e2512892SShuo Chen
34e2512892SShuo Chen  {
35e2512892SShuo Chen    char buf[32];
36e2512892SShuo Chen    snprintf(buf, sizeof buf, "start N=%d", N);
37e2512892SShuo Chen    snapshot(buf);
38e2512892SShuo Chen  }
39e2512892SShuo Chen
40e2512892SShuo Chen  InetAddress listenAddr(2222);
41e2512892SShuo Chen  Acceptor acceptor(listenAddr);
42e2512892SShuo Chen  snapshot("acceptor created");
43e2512892SShuo Chen
44e2512892SShuo Chen  std::vector<Socket> clients;
45e2512892SShuo Chen  for (int i = 0; i < N; ++i)
46e2512892SShuo Chen    clients.push_back(Socket::createTCP());
47e2512892SShuo Chen  snapshot("clients created");
48e2512892SShuo Chen
49e2512892SShuo Chen  std::vector<Socket> servers;
50e2512892SShuo Chen  InetAddress serverAddr("127.0.0.1", 2222);
51e2512892SShuo Chen  for (int i = 0; i < N; ++i)
52e2512892SShuo Chen  {
53e2512892SShuo Chen    if (clients[i].connect(serverAddr))
54e2512892SShuo Chen    {
55e2512892SShuo Chen      perror("connect");
56e2512892SShuo Chen      break;
57e2512892SShuo Chen    }
58e2512892SShuo Chen    servers.push_back(acceptor.acceptSocketOrDie());
59e2512892SShuo Chen  }
60e2512892SShuo Chen  snapshot("clients connected");
61e2512892SShuo Chen
62e2512892SShuo Chen  // TODO: epoll
63e2512892SShuo Chen
64e2512892SShuo Chen  servers.clear();
65e2512892SShuo Chen  snapshot("servers disconnected");
66e2512892SShuo Chen
67e2512892SShuo Chen  clients.clear();
68e2512892SShuo Chen  snapshot("clients disconnected");
69e2512892SShuo Chen}
70