-
Notifications
You must be signed in to change notification settings - Fork 284
/
https_trace_client.cpp
201 lines (164 loc) · 6.99 KB
/
https_trace_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// Created by Ivan Shynkarenka on 02.05.2019
//
#include "server/asio/service.h"
#include "server/http/https_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::HTTP;
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 HTTPSTraceClient : public HTTPSClient
{
public:
HTTPSTraceClient(const std::shared_ptr<Service>& service, std::shared_ptr<SSLContext> context, const std::string& address, int port, int messages)
: HTTPSClient(service, context, address, port),
_messages(messages)
{
}
void SendMessage() { SendRequestAsync(request().MakeTraceRequest("/")); }
protected:
void onHandshaked() override
{
for (size_t i = _messages; i > 0; --i)
SendMessage();
}
void onSent(size_t sent, size_t pending) override
{
_sent += sent;
HTTPSClient::onSent(sent, pending);
}
void onReceived(const void* buffer, size_t size) override
{
_received += size;
timestamp_stop = Timestamp::nano();
total_bytes += size;
HTTPSClient::onReceived(buffer, size);
}
void onReceivedResponse(const HTTPResponse& response) override
{
if (response.status() == 200)
++total_messages;
else
++total_errors;
SendMessage();
}
void onReceivedResponseError(const HTTPResponse& response, const std::string& error) override
{
std::cout << "Response error: " << error << std::endl;
++total_errors;
SendMessage();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "HTTPS Trace client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
++total_errors;
}
private:
size_t _sent{0};
size_t _received{0};
size_t _messages{0};
};
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("-m", "--messages").dest("messages").action("store").type("int").set_default(1).help("Count of messages to send at the same time. 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 messages_count = options.get("messages");
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 << "Working messages: " << messages_count << 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<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 HTTPS Trace clients
std::vector<std::shared_ptr<HTTPSTraceClient>> clients;
for (int i = 0; i < clients_count; ++i)
{
// Create echo client
auto client = std::make_shared<HTTPSTraceClient>(service, context, address, port, messages_count);
// 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->DisconnectAsync();
std::cout << "Done!" << std::endl;
for (const auto& client : clients)
while (client->IsConnected())
Thread::Yield();
std::cout << "All clients disconnected!" << std::endl;
// 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;
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;
}