BVB Source Codes

uWebSockets Show scalability.cpp Source code

Return Download uWebSockets: download scalability.cpp Source code - Download uWebSockets Source code - Type:.cpp
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <sys/socket.h>
  4. #include <arpa/inet.h>
  5. #include <cstring>
  6. #include <string>
  7. #include <chrono>
  8. #include <errno.h>
  9. #include <vector>
  10. #include <mutex>
  11. #include <thread>
  12. #include <fstream>
  13. using namespace std;
  14. using namespace chrono;
  15.  
  16. int totalConnections = 500000;
  17. int port = 3000;
  18.  
  19. #define CONNECTIONS_PER_ADDRESS 28000
  20. #define THREADS 10
  21.  
  22. int connections, address = 1;
  23. mutex m;
  24.  
  25. int getKb(int pid) {
  26.     std::string line;
  27.     std::ifstream self((std::string("/proc/") + std::to_string(pid) + std::string("/status")).c_str());
  28.     int vmRSS;
  29.     while(!self.eof()) {
  30.         std::getline(self, line, ':');
  31.         if (line == "VmRSS") {
  32.             self >> vmRSS;
  33.         }
  34.         std::getline(self, line);
  35.     }
  36.     return vmRSS;
  37. }
  38.  
  39. bool nextConnection(int tid)
  40. {
  41.     m.lock();
  42.     int socketfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  43.     if (socketfd == -1) {
  44.         cout << "FD error, connections: " << connections << endl;
  45.         return false;
  46.     }
  47.  
  48.     sockaddr_in addr = {};
  49.     addr.sin_family = AF_INET;
  50.     addr.sin_addr.s_addr = inet_addr(("127.0.0." + to_string(address)).c_str());
  51.     addr.sin_port = htons(port);
  52.     m.unlock();
  53.  
  54.     // this is a shared upgrade, no need to make it unique
  55.     const char *buf = "GET / HTTP/1.1\r\n"
  56.                       "Host: server.example.com\r\n"
  57.                       "Upgrade: websocket\r\n"
  58.                       "Connection: Upgrade\r\n"
  59.                       "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"
  60.                       "Sec-WebSocket-Protocol: default\r\n"
  61.                       "Sec-WebSocket-Version: 13\r\n"
  62.                       "Origin: http://example.com\r\n\r\n";
  63.  
  64.     char message[1024];
  65.  
  66.     int err = connect(socketfd, (sockaddr *) &addr, sizeof(addr));
  67.     if (err) {
  68.         cout << "Connection error, connections: " << connections << endl;
  69.         return false;
  70.     }
  71.     send(socketfd, buf, strlen(buf), 0);
  72.     memset(message, 0, 1024);
  73.     size_t length;
  74.     do {
  75.         length = recv(socketfd, message, sizeof(message), 0);
  76.     }
  77.     while (strncmp(&message[length - 4], "\r\n\r\n", 4));
  78.  
  79.     m.lock();
  80.     if (++connections % CONNECTIONS_PER_ADDRESS == 0) {
  81.         address++;
  82.     }
  83.  
  84.     if (connections % 1000 == 0 || connections < 1000) {
  85.         cout << "Connections: " << connections << endl;
  86.     }
  87.  
  88.     if (connections >= totalConnections - THREADS + 1) {
  89.         m.unlock();
  90.         return false;
  91.     }
  92.  
  93.     m.unlock();
  94.     return true;
  95. }
  96.  
  97. int main(int argc, char **argv)
  98. {
  99.     if (argc != 3) {
  100.         cout << "Usage: scalability numberOfConnections port" << endl;
  101.         return -1;
  102.     }
  103.  
  104.     totalConnections = atoi(argv[1]);
  105.     port = atoi(argv[2]);
  106.  
  107.     FILE *pipe = popen(("fuser " + to_string(port) + "/tcp 2> /dev/null").c_str(), "r");
  108.     char line[10240] = {};
  109.     fgets(line, sizeof(line), pipe);
  110.     pclose(pipe);
  111.     int pid = atoi(line);
  112.  
  113.     auto startPoint = high_resolution_clock::now();
  114.     vector<thread *> threads;
  115.     for (int i = 0; i < THREADS; i++) {
  116.         threads.push_back(new thread([i] {
  117.             while(nextConnection(i));
  118.         }));
  119.     }
  120.  
  121.     for (thread *t : threads) {
  122.         t->join();
  123.     }
  124.  
  125.     double connectionsPerMs = double(connections) / duration_cast<milliseconds>(high_resolution_clock::now() - startPoint).count();
  126.     cout << "Connection performance: " << connectionsPerMs << " connections/ms" << endl;
  127.  
  128.     unsigned long kbUsage = getKb(pid);
  129.     cout << "Memory usage: " << (double(kbUsage) / 1024.0) << " mb of user space" << std::endl;
  130.     cout << "Memory performance: " << 1024.0 * double(connections) / kbUsage << " connections/mb" << endl;
  131.     return 0;
  132. }
  133.  
  134.  
downloadscalability.cpp Source code - Download uWebSockets Source code
Related Source Codes/Software:
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11

 Back to top