BVB Source Codes

uWebSockets Show scalability_socketio.cpp Source code

Return Download uWebSockets: download scalability_socketio.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 /socket.io/?EIO=4&transport=websocket 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-Version: 13\r\n\r\n";
  61.  
  62.     char message[1024];
  63.  
  64.     int err = connect(socketfd, (sockaddr *) &addr, sizeof(addr));
  65.     if (err) {
  66.         cout << "Connection error, connections: " << connections << endl;
  67.         return false;
  68.     }
  69.     send(socketfd, buf, strlen(buf), 0);
  70.     memset(message, 0, 1024);
  71.     size_t length;
  72.     // Socket.IO sends 221 bytes in response
  73.     for (int r = 0; r < 221; ) {
  74.         r += recv(socketfd, message, sizeof(message), 0);
  75.     }
  76.  
  77.     m.lock();
  78.     if (++connections % CONNECTIONS_PER_ADDRESS == 0) {
  79.         address++;
  80.     }
  81.  
  82.     if (connections % 1000 == 0 || connections < 1000) {
  83.         cout << "Connections: " << connections << endl;
  84.     }
  85.  
  86.     if (connections >= totalConnections - THREADS + 1) {
  87.         m.unlock();
  88.         return false;
  89.     }
  90.  
  91.     m.unlock();
  92.     return true;
  93. }
  94.  
  95. int main(int argc, char **argv)
  96. {
  97.     if (argc != 3) {
  98.         cout << "Usage: scalability numberOfConnections port" << endl;
  99.         return -1;
  100.     }
  101.  
  102.     totalConnections = atoi(argv[1]);
  103.     port = atoi(argv[2]);
  104.  
  105.     FILE *pipe = popen(("fuser " + to_string(port) + "/tcp 2> /dev/null").c_str(), "r");
  106.     char line[10240] = {};
  107.     fgets(line, sizeof(line), pipe);
  108.     pclose(pipe);
  109.     int pid = atoi(line);
  110.  
  111.     auto startPoint = high_resolution_clock::now();
  112.     vector<thread *> threads;
  113.     for (int i = 0; i < THREADS; i++) {
  114.         threads.push_back(new thread([i] {
  115.             while(nextConnection(i));
  116.         }));
  117.     }
  118.  
  119.     for (thread *t : threads) {
  120.         t->join();
  121.     }
  122.  
  123.     double connectionsPerMs = double(connections) / duration_cast<milliseconds>(high_resolution_clock::now() - startPoint).count();
  124.     cout << "Connection performance: " << connectionsPerMs << " connections/ms" << endl;
  125.  
  126.     unsigned long kbUsage = getKb(pid);
  127.     cout << "Memory usage: " << (double(kbUsage) / 1024.0) << " mb of user space" << std::endl;
  128.     cout << "Memory performance: " << 1024.0 * double(connections) / kbUsage << " connections/mb" << endl;
  129.     return 0;
  130. }
  131.  
  132.  
downloadscalability_socketio.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