footprint.cc revision 24ca08a8
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 4424ca08a8SShuo Chen InetAddress serverAddr("127.0.0.1", 2222); 4524ca08a8SShuo Chen 46e2512892SShuo Chen std::vector<Socket> clients; 47e2512892SShuo Chen for (int i = 0; i < N; ++i) 4824ca08a8SShuo Chen clients.push_back(Socket::createTCP(serverAddr.family())); 49e2512892SShuo Chen snapshot("clients created"); 50e2512892SShuo Chen 51e2512892SShuo Chen std::vector<Socket> servers; 52e2512892SShuo Chen for (int i = 0; i < N; ++i) 53e2512892SShuo Chen { 54e2512892SShuo Chen if (clients[i].connect(serverAddr)) 55e2512892SShuo Chen { 56e2512892SShuo Chen perror("connect"); 57e2512892SShuo Chen break; 58e2512892SShuo Chen } 59e2512892SShuo Chen servers.push_back(acceptor.acceptSocketOrDie()); 60e2512892SShuo Chen } 61e2512892SShuo Chen snapshot("clients connected"); 62e2512892SShuo Chen 63e2512892SShuo Chen // TODO: epoll 64e2512892SShuo Chen 65e2512892SShuo Chen servers.clear(); 66e2512892SShuo Chen snapshot("servers disconnected"); 67e2512892SShuo Chen 68e2512892SShuo Chen clients.clear(); 69e2512892SShuo Chen snapshot("clients disconnected"); 70e2512892SShuo Chen} 71