-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
90 lines (67 loc) · 2.3 KB
/
client.cpp
File metadata and controls
90 lines (67 loc) · 2.3 KB
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
#include <thread>
#include <game/client.h>
#include <game/protocol.h>
#include <iostream>
#include <chrono>
// Message struct definition
#pragma pack(push, 1)
struct Position {
static constexpr uint8_t type = 1;
uint32_t client_id;
uint32_t message_id;
};
#pragma pack(pop)
std::string getColor(uint32_t client_id) {
switch (client_id % 6) {
case 1: return "\033[34m"; // Blue
case 2: return "\033[33m"; // Yellow
case 3: return "\033[31m"; // Red
case 4: return "\033[32m"; // Green
case 5: return "\033[35m"; // Magenta
case 6: return "\033[36m"; // Cyan
default: return "\033[0m"; // Default
}
}
const std::string COLOR_RESET = "\033[0m";
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: ./client <client_id>\n";
return 1;
}
uint32_t client_id = std::stoi(argv[1]);
Client client;
client.setHandler<Position>([](const Position& pos) {
std::string color = getColor(pos.client_id);
if (pos.message_id % 1000 == 0) {
std::cout << color
<< "cl_id: " << pos.client_id
<< ", msg_id: " << pos.message_id
<< COLOR_RESET << "\n";
}
});
client.enableAutoReconnect(true);
if (!client.connect("ws://localhost:9000")) {
return 1;
}
const uint32_t totalProtocols = 10000000;
const uint32_t batchSize = 100;
uint32_t sent = 0;
client.wait();
auto start = std::chrono::steady_clock::now();
while (sent < totalProtocols) {
for (int i = 0; i < 10; ++i) {
client.poll();
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
for (int i = 0; i < 1000; ++i) {
client.send(Position{ .client_id = client_id, .message_id = ++sent });
}
client.flush();
}
auto end = std::chrono::steady_clock::now();
auto elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "Sent " << sent << " messages in " << elapsedMs << " ms\n";
std::cout << "Average: " << (elapsedMs / static_cast<float>(totalProtocols)) << " ms/message\n";
return 0;
}
// g++ client.cpp src/game/client.cpp -I./src -lwebsockets -o client