forked from uNetworking/uWebSockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalability.cpp
133 lines (112 loc) · 3.58 KB
/
scalability.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <string>
#include <chrono>
#include <errno.h>
#include <vector>
#include <mutex>
#include <thread>
#include <fstream>
using namespace std;
using namespace chrono;
int totalConnections = 500000;
int port = 3000;
#define CONNECTIONS_PER_ADDRESS 28000
#define THREADS 10
int connections, address = 1;
mutex m;
int getKb(int pid) {
std::string line;
std::ifstream self((std::string("/proc/") + std::to_string(pid) + std::string("/status")).c_str());
int vmRSS;
while(!self.eof()) {
std::getline(self, line, ':');
if (line == "VmRSS") {
self >> vmRSS;
}
std::getline(self, line);
}
return vmRSS;
}
bool nextConnection(int tid)
{
m.lock();
int socketfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketfd == -1) {
cout << "FD error, connections: " << connections << endl;
return false;
}
sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(("127.0.0." + to_string(address)).c_str());
addr.sin_port = htons(port);
m.unlock();
// this is a shared upgrade, no need to make it unique
const char *buf = "GET / HTTP/1.1\r\n"
"Host: server.example.com\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"
"Sec-WebSocket-Protocol: default\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Origin: http://example.com\r\n\r\n";
char message[1024];
int err = connect(socketfd, (sockaddr *) &addr, sizeof(addr));
if (err) {
cout << "Connection error, connections: " << connections << endl;
return false;
}
send(socketfd, buf, strlen(buf), 0);
memset(message, 0, 1024);
size_t length;
do {
length = recv(socketfd, message, sizeof(message), 0);
}
while (strncmp(&message[length - 4], "\r\n\r\n", 4));
m.lock();
if (++connections % CONNECTIONS_PER_ADDRESS == 0) {
address++;
}
if (connections % 1000 == 0 || connections < 1000) {
cout << "Connections: " << connections << endl;
}
if (connections >= totalConnections - THREADS + 1) {
m.unlock();
return false;
}
m.unlock();
return true;
}
int main(int argc, char **argv)
{
if (argc != 3) {
cout << "Usage: scalability numberOfConnections port" << endl;
return -1;
}
totalConnections = atoi(argv[1]);
port = atoi(argv[2]);
FILE *pipe = popen(("fuser " + to_string(port) + "/tcp 2> /dev/null").c_str(), "r");
char line[10240] = {};
fgets(line, sizeof(line), pipe);
pclose(pipe);
int pid = atoi(line);
auto startPoint = high_resolution_clock::now();
vector<thread *> threads;
for (int i = 0; i < THREADS; i++) {
threads.push_back(new thread([i] {
while(nextConnection(i));
}));
}
for (thread *t : threads) {
t->join();
}
double connectionsPerMs = double(connections) / duration_cast<milliseconds>(high_resolution_clock::now() - startPoint).count();
cout << "Connection performance: " << connectionsPerMs << " connections/ms" << endl;
unsigned long kbUsage = getKb(pid);
cout << "Memory usage: " << (double(kbUsage) / 1024.0) << " mb of user space" << std::endl;
cout << "Memory performance: " << 1024.0 * double(connections) / kbUsage << " connections/mb" << endl;
return 0;
}