-
Notifications
You must be signed in to change notification settings - Fork 284
/
wss_multicast_client.cpp
174 lines (140 loc) · 6.39 KB
/
wss_multicast_client.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// Created by Ivan Shynkarenka on 28.05.2019
//
#include "server/asio/service.h"
#include "server/ws/wss_client.h"
#include "benchmark/reporter_console.h"
#include "system/cpu.h"
#include "threads/thread.h"
#include "time/timestamp.h"
#include <atomic>
#include <iostream>
#include <vector>
#include <OptionParser.h>
using namespace CppCommon;
using namespace CppServer::Asio;
using namespace CppServer::WS;
std::atomic<uint64_t> timestamp_start(Timestamp::nano());
std::atomic<uint64_t> timestamp_stop(Timestamp::nano());
std::atomic<uint64_t> total_errors(0);
std::atomic<uint64_t> total_bytes(0);
std::atomic<uint64_t> total_messages(0);
class MulticastClient : public WSSClient
{
public:
using WSSClient::WSSClient;
protected:
void onWSConnecting(CppServer::HTTP::HTTPRequest& request) override
{
request.SetBegin("GET", "/");
request.SetHeader("Host", "localhost");
request.SetHeader("Origin", "https://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", CppCommon::Encoding::Base64Encode(ws_nonce()));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
void onWSReceived(const void* buffer, size_t size) override
{
total_bytes += size;
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "WebSocket secure client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
++total_errors;
}
};
int main(int argc, char** argv)
{
auto parser = optparse::OptionParser().version("1.0.0.0");
parser.add_option("-a", "--address").dest("address").set_default("127.0.0.1").help("Server address. Default: %default");
parser.add_option("-p", "--port").dest("port").action("store").type("int").set_default(8443).help("Server port. Default: %default");
parser.add_option("-t", "--threads").dest("threads").action("store").type("int").set_default(CPU::PhysicalCores()).help("Count of working threads. Default: %default");
parser.add_option("-c", "--clients").dest("clients").action("store").type("int").set_default(100).help("Count of working clients. Default: %default");
parser.add_option("-s", "--size").dest("size").action("store").type("int").set_default(32).help("Single message size. Default: %default");
parser.add_option("-z", "--seconds").dest("seconds").action("store").type("int").set_default(10).help("Count of seconds to benchmarking. Default: %default");
optparse::Values options = parser.parse_args(argc, argv);
// Print help
if (options.get("help"))
{
parser.print_help();
return 0;
}
// Client parameters
std::string address(options.get("address"));
int port = options.get("port");
int threads_count = options.get("threads");
int clients_count = options.get("clients");
int message_size = options.get("size");
int seconds_count = options.get("seconds");
std::cout << "Server address: " << address << std::endl;
std::cout << "Server port: " << port << std::endl;
std::cout << "Working threads: " << threads_count << std::endl;
std::cout << "Working clients: " << clients_count << std::endl;
std::cout << "Message size: " << message_size << std::endl;
std::cout << "Seconds to benchmarking: " << seconds_count << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<Service>(threads_count);
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create and prepare a new SSL client context
auto context = std::make_shared<CppServer::Asio::SSLContext>(asio::ssl::context::tlsv13);
context->set_default_verify_paths();
context->set_root_certs();
context->set_verify_mode(asio::ssl::verify_peer | asio::ssl::verify_fail_if_no_peer_cert);
context->load_verify_file("../tools/certificates/ca.pem");
// Create multicast clients
std::vector<std::shared_ptr<MulticastClient>> clients;
for (int i = 0; i < clients_count; ++i)
{
auto client = std::make_shared<MulticastClient>(service, context, address, port);
// client->SetupNoDelay(true);
clients.emplace_back(client);
}
timestamp_start = Timestamp::nano();
// Connect clients
std::cout << "Clients connecting...";
for (auto& client : clients)
client->ConnectAsync();
std::cout << "Done!" << std::endl;
for (const auto& client : clients)
while (!client->IsConnected())
Thread::Yield();
std::cout << "All clients connected!" << std::endl;
// Wait for benchmarking
std::cout << "Benchmarking...";
Thread::Sleep(seconds_count * 1000);
std::cout << "Done!" << std::endl;
// Disconnect clients
std::cout << "Clients disconnecting...";
for (auto& client : clients)
client->CloseAsync(1000);
std::cout << "Done!" << std::endl;
for (const auto& client : clients)
while (client->IsConnected())
Thread::Yield();
std::cout << "All clients disconnected!" << std::endl;
timestamp_stop = Timestamp::nano();
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
std::cout << std::endl;
std::cout << "Errors: " << total_errors << std::endl;
std::cout << std::endl;
total_messages = total_bytes / message_size;
std::cout << "Total time: " << CppBenchmark::ReporterConsole::GenerateTimePeriod(timestamp_stop - timestamp_start) << std::endl;
std::cout << "Total data: " << CppBenchmark::ReporterConsole::GenerateDataSize(total_bytes) << std::endl;
std::cout << "Total messages: " << total_messages << std::endl;
std::cout << "Data throughput: " << CppBenchmark::ReporterConsole::GenerateDataSize(total_bytes * 1000000000 / (timestamp_stop - timestamp_start)) << "/s" << std::endl;
if (total_messages > 0)
{
std::cout << "Message latency: " << CppBenchmark::ReporterConsole::GenerateTimePeriod((timestamp_stop - timestamp_start) / total_messages) << std::endl;
std::cout << "Message throughput: " << total_messages * 1000000000 / (timestamp_stop - timestamp_start) << " msg/s" << std::endl;
}
return 0;
}