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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "bench_cmd.h"
#include "bench.h"
#include <zencore/compactbinary.h>
#include <zencore/except.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/process.h>
#include <zencore/string.h>
#include <zencore/thread.h>
#include <zencore/timer.h>
#include <zenhttp/httpclient.h>
#include <zentelemetry/stats.h>
#include <algorithm>
#include <atomic>
#include <csignal>
#include <mutex>
#include <thread>
static std::atomic<bool> s_BenchAbort{false};
namespace zen {
//////////////////////////////////////////////////////////////////////////
// BenchPurgeSubCmd
BenchPurgeSubCmd::BenchPurgeSubCmd() : ZenSubCmdBase("purge", "Purge standby memory (system cache)")
{
SubOptions().add_options()("single", "Do not spawn child processes", cxxopts::value<bool>(m_SingleProcess)->default_value("false"));
}
void
BenchPurgeSubCmd::Run(const ZenCliOptions& GlobalOptions)
{
ZEN_UNUSED(GlobalOptions);
bool Ok = false;
zen::Stopwatch Timer;
try
{
zen::bench::util::EmptyStandByList();
Ok = true;
}
catch (const zen::bench::util::elevation_required_exception&)
{
ZEN_CONSOLE_WARN("Purging standby lists requires elevation. Will try launch as elevated process");
}
catch (const std::exception& Ex)
{
ZEN_CONSOLE_ERROR("{}", Ex.what());
}
#if ZEN_PLATFORM_WINDOWS
if (!Ok && !m_SingleProcess)
{
try
{
zen::CreateProcOptions Cpo;
Cpo.Flags = zen::CreateProcOptions::Flag_Elevated | zen::CreateProcOptions::Flag_NewConsole;
std::filesystem::path CurExe{zen::GetRunningExecutablePath()};
if (zen::CreateProcResult Cpr = zen::CreateProc(CurExe, fmt::format("bench purge --single"), Cpo))
{
zen::ProcessHandle ProcHandle;
ProcHandle.Initialize(Cpr);
int ExitCode = ProcHandle.WaitExitCode();
if (ExitCode == 0)
{
Ok = true;
}
else
{
ZEN_CONSOLE_ERROR("Elevated child process failed with return code {}", ExitCode);
}
}
}
catch (const std::exception& Ex)
{
ZEN_CONSOLE_ERROR("{}", Ex.what());
}
}
#endif
if (Ok)
{
// TODO: could also add reporting on just how much memory was purged
ZEN_CONSOLE("Purged standby lists! (took {})", zen::NiceTimeSpanMs(Timer.GetElapsedTimeMs()));
}
}
//////////////////////////////////////////////////////////////////////////
// BenchHttpSubCmd
BenchHttpSubCmd::BenchHttpSubCmd() : ZenSubCmdBase("http", "Benchmark an HTTP server")
{
SubOptions().add_option("", "u", "url", "URL to benchmark", cxxopts::value<std::string>(m_Url), "<url>");
SubOptions().add_option("", "n", "count", "Number of requests to send", cxxopts::value<int>(m_Count)->default_value("100"), "<count>");
SubOptions().add_option("",
"c",
"concurrency",
"Number of concurrent threads",
cxxopts::value<int>(m_Concurrency)->default_value("1"),
"<threads>");
SubOptions().add_option("",
"",
"method",
"HTTP method to use (GET, HEAD)",
cxxopts::value<std::string>(m_Method)->default_value("GET"),
"<method>");
SubOptions()
.add_option("", "", "unix-socket", "Unix domain socket path (overrides TCP)", cxxopts::value<std::string>(m_SocketPath), "<path>");
SubOptions().add_options()("no-keepalive",
"Close connection after each request (disables keep-alive)",
cxxopts::value<bool>(m_NoKeepAlive)->default_value("false"));
SubOptions().add_options()("continuous",
"Run until interrupted (Ctrl+C), printing metrics once per second",
cxxopts::value<bool>(m_Continuous)->default_value("false"));
SubOptions().parse_positional({"url"});
}
static std::pair<std::string, std::string>
SplitUrl(std::string_view Url)
{
size_t SchemeEnd = Url.find("://");
size_t SearchFrom = (SchemeEnd != std::string_view::npos) ? SchemeEnd + 3 : 0;
size_t PathStart = Url.find('/', SearchFrom);
if (PathStart == std::string_view::npos)
{
return {std::string(Url), "/"};
}
return {std::string(Url.substr(0, PathStart)), std::string(Url.substr(PathStart))};
}
void
BenchHttpSubCmd::Run(const ZenCliOptions& GlobalOptions)
{
ZEN_UNUSED(GlobalOptions);
if (m_Url.empty())
{
throw OptionParseException("URL is required", SubOptions().help());
}
if (!m_Continuous && m_Count <= 0)
{
throw OptionParseException("--count must be a positive integer", SubOptions().help());
}
if (m_Concurrency <= 0)
{
throw OptionParseException("--concurrency must be a positive integer", SubOptions().help());
}
if (m_Method != "GET" && m_Method != "HEAD")
{
throw OptionParseException(fmt::format("Unsupported HTTP method '{}'. Supported: GET, HEAD", m_Method), SubOptions().help());
}
auto [BaseUri, Path] = SplitUrl(m_Url);
std::string ModeStr = m_Continuous ? "continuous" : fmt::format("count={}", m_Count);
if (m_SocketPath.empty())
{
ZEN_CONSOLE("Benchmarking {} {} ({}, concurrency={})", m_Method, m_Url, ModeStr, m_Concurrency);
}
else
{
ZEN_CONSOLE("Benchmarking {} {} via {} ({}, concurrency={})", m_Method, m_Url, m_SocketPath, ModeStr, m_Concurrency);
}
// Probe for a zenserver identity. If the target exposes /health/info and the
// response contains a BuildVersion field we print a short summary. Any failure
// (non-zenserver, timeout, unreachable) is silently ignored.
try
{
HttpClientSettings ProbeSettings{.ConnectTimeout = std::chrono::milliseconds(2000),
.Timeout = std::chrono::milliseconds(2000),
.UnixSocketPath = m_SocketPath};
HttpClient ProbeHttp(BaseUri, ProbeSettings);
HttpClient::Response ProbeResp = ProbeHttp.Get("/health/info");
if (ProbeResp.IsSuccess())
{
CbObject Info = ProbeResp.AsObject();
std::string_view BuildVersion = Info["BuildVersion"].AsString();
if (!BuildVersion.empty())
{
std::string_view Hostname = Info["Hostname"].AsString();
int64_t Pid = Info["Pid"].AsInt64();
std::string_view HttpServerClass = Info["HttpServerClass"].AsString();
ZEN_CONSOLE("Remote : zenserver {} on {} (pid {}, {})", BuildVersion, Hostname, Pid, HttpServerClass);
std::string_view OS = Info["OS"].AsString();
std::string_view Arch = Info["Arch"].AsString();
CbObjectView System = Info["System"].AsObjectView();
int64_t LpCount = System["lp_count"].AsInt64();
int64_t TotalMemMiB = System["total_memory_mb"].AsInt64();
ZEN_CONSOLE(" : {}, {}, {} logical processors, {} RAM",
OS,
Arch,
LpCount,
NiceBytes(static_cast<uint64_t>(TotalMemMiB) * 1024 * 1024));
}
}
}
catch (...)
{
}
if (m_Continuous)
{
RunContinuous(BaseUri, Path);
}
else
{
RunFixedCount(BaseUri, Path);
}
}
void
BenchHttpSubCmd::RunFixedCount(const std::string& BaseUri, const std::string& Path)
{
std::atomic<int> NextRequest{0};
std::vector<double> AllLatencies;
AllLatencies.reserve(m_Count);
std::mutex LatencyMutex;
std::atomic<int> ErrorCount{0};
std::atomic<int64_t> TotalDownloadedBytes{0};
std::atomic<int64_t> TotalUploadedBytes{0};
Stopwatch Timer;
auto WorkerFn = [&]() {
std::vector<double> LocalLatencies;
HttpClientSettings Settings{.UnixSocketPath = m_SocketPath, .ForbidReuseConnection = m_NoKeepAlive};
HttpClient Http(BaseUri, Settings);
while (true)
{
int RequestIndex = NextRequest.fetch_add(1);
if (RequestIndex >= m_Count)
{
break;
}
try
{
HttpClient::Response Resp = (m_Method == "HEAD") ? Http.Head(Path) : Http.Get(Path);
if (Resp.IsSuccess())
{
LocalLatencies.push_back(Resp.ElapsedSeconds);
TotalDownloadedBytes.fetch_add(Resp.DownloadedBytes);
TotalUploadedBytes.fetch_add(Resp.UploadedBytes);
}
else
{
ErrorCount.fetch_add(1);
}
}
catch (const HttpClientError&)
{
ErrorCount.fetch_add(1);
}
}
std::lock_guard Lock(LatencyMutex);
AllLatencies.insert(AllLatencies.end(), LocalLatencies.begin(), LocalLatencies.end());
};
std::vector<std::thread> Threads;
Threads.reserve(m_Concurrency);
for (int i = 0; i < m_Concurrency; ++i)
{
Threads.emplace_back(WorkerFn);
}
for (std::thread& T : Threads)
{
T.join();
}
double TotalSeconds = Timer.GetElapsedTimeMs() / 1000.0;
int SuccessCount = static_cast<int>(AllLatencies.size());
int TotalCount = SuccessCount + ErrorCount.load();
std::sort(AllLatencies.begin(), AllLatencies.end());
auto PercentileMs = [&](int Pct) -> double {
if (AllLatencies.empty())
{
return 0.0;
}
size_t Index = std::min(AllLatencies.size() * static_cast<size_t>(Pct) / 100, AllLatencies.size() - 1);
return AllLatencies[Index] * 1000.0;
};
double SumMs = 0.0;
for (double L : AllLatencies)
{
SumMs += L * 1000.0;
}
double MeanMs = SuccessCount > 0 ? SumMs / SuccessCount : 0.0;
double Rps = TotalSeconds > 0.0 ? TotalCount / TotalSeconds : 0.0;
uint64_t DownBytesPerSec = TotalSeconds > 0.0 ? static_cast<uint64_t>(TotalDownloadedBytes.load() / TotalSeconds) : 0;
uint64_t UpBytesPerSec = TotalSeconds > 0.0 ? static_cast<uint64_t>(TotalUploadedBytes.load() / TotalSeconds) : 0;
ZEN_CONSOLE(" Requests : {:L} total, {:L} success, {:L} errors", TotalCount, SuccessCount, ErrorCount.load());
ZEN_CONSOLE(" Latency : min={:.1f}ms mean={:.1f}ms p50={:.1f}ms p95={:.1f}ms p99={:.1f}ms max={:.1f}ms",
PercentileMs(0),
MeanMs,
PercentileMs(50),
PercentileMs(95),
PercentileMs(99),
PercentileMs(100));
ZEN_CONSOLE(" Throughput: {:.1f} req/s down: {}/s up: {}/s (elapsed: {:.2f}s)",
Rps,
NiceBytes(DownBytesPerSec),
NiceBytes(UpBytesPerSec),
TotalSeconds);
}
void
BenchHttpSubCmd::RunContinuous(const std::string& BaseUri, const std::string& Path)
{
s_BenchAbort.store(false);
auto PrevSigInt = std::signal(SIGINT, [](int) { s_BenchAbort.store(true); });
auto PrevSigTerm = std::signal(SIGTERM, [](int) { s_BenchAbort.store(true); });
metrics::Histogram LatencyHistogram;
std::atomic<int64_t> IntervalSuccessCount{0};
std::atomic<int64_t> IntervalErrorCount{0};
std::atomic<int64_t> IntervalDownloadBytes{0};
std::atomic<int64_t> IntervalUploadBytes{0};
std::atomic<int64_t> TotalSuccessCount{0};
std::atomic<int64_t> TotalErrorCount{0};
std::atomic<int64_t> TotalDownloadBytes{0};
std::atomic<int64_t> TotalUploadBytes{0};
Stopwatch RunTimer;
auto WorkerFn = [&]() {
HttpClientSettings Settings{.UnixSocketPath = m_SocketPath, .ForbidReuseConnection = m_NoKeepAlive};
HttpClient Http(BaseUri, Settings);
while (!s_BenchAbort.load(std::memory_order_relaxed))
{
try
{
HttpClient::Response Resp = (m_Method == "HEAD") ? Http.Head(Path) : Http.Get(Path);
if (Resp.IsSuccess())
{
LatencyHistogram.Update(static_cast<int64_t>(Resp.ElapsedSeconds * 1.0e6));
IntervalSuccessCount.fetch_add(1, std::memory_order_relaxed);
IntervalDownloadBytes.fetch_add(Resp.DownloadedBytes, std::memory_order_relaxed);
IntervalUploadBytes.fetch_add(Resp.UploadedBytes, std::memory_order_relaxed);
TotalSuccessCount.fetch_add(1, std::memory_order_relaxed);
TotalDownloadBytes.fetch_add(Resp.DownloadedBytes, std::memory_order_relaxed);
TotalUploadBytes.fetch_add(Resp.UploadedBytes, std::memory_order_relaxed);
}
else
{
IntervalErrorCount.fetch_add(1, std::memory_order_relaxed);
TotalErrorCount.fetch_add(1, std::memory_order_relaxed);
}
}
catch (const HttpClientError&)
{
IntervalErrorCount.fetch_add(1, std::memory_order_relaxed);
TotalErrorCount.fetch_add(1, std::memory_order_relaxed);
}
}
};
auto ReporterFn = [&]() {
while (!s_BenchAbort.load(std::memory_order_relaxed))
{
// Sleep 1s in short increments to stay responsive to abort
for (int i = 0; i < 10 && !s_BenchAbort.load(std::memory_order_relaxed); ++i)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (s_BenchAbort.load(std::memory_order_relaxed))
{
break;
}
// Snapshot and reset per-interval counters
int64_t Successes = IntervalSuccessCount.exchange(0);
int64_t Errors = IntervalErrorCount.exchange(0);
int64_t DownBytes = IntervalDownloadBytes.exchange(0);
int64_t UpBytes = IntervalUploadBytes.exchange(0);
// Snapshot and reset latency histogram
uint64_t HistCount = LatencyHistogram.Count();
int64_t HistMin = LatencyHistogram.Min();
int64_t HistMax = LatencyHistogram.Max();
double HistMean = LatencyHistogram.Mean();
metrics::SampleSnapshot Snap = LatencyHistogram.Snapshot();
LatencyHistogram.Clear();
// Format elapsed as HH:MM:SS
int TotalSec = static_cast<int>(RunTimer.GetElapsedTimeMs() / 1000.0);
int Hours = TotalSec / 3600;
int Minutes = (TotalSec % 3600) / 60;
int Secs = TotalSec % 60;
if (HistCount > 0)
{
ZEN_CONSOLE(
"[{:02d}:{:02d}:{:02d}] req/s: {:L} errors: {:L} lat(ms): min={:.1f} mean={:.1f} p95={:.1f} p99={:.1f} max={:.1f} "
"down: {}/s up: {}/s",
Hours,
Minutes,
Secs,
Successes,
Errors,
HistMin / 1000.0,
HistMean / 1000.0,
Snap.Get95Percentile() / 1000.0,
Snap.Get99Percentile() / 1000.0,
HistMax / 1000.0,
NiceBytes(static_cast<uint64_t>(std::max(int64_t{0}, DownBytes))),
NiceBytes(static_cast<uint64_t>(std::max(int64_t{0}, UpBytes))));
}
else
{
ZEN_CONSOLE("[{:02d}:{:02d}:{:02d}] req/s: 0 errors: {:L} (no successful requests)", Hours, Minutes, Secs, Errors);
}
}
};
std::vector<std::thread> Threads;
Threads.reserve(m_Concurrency + 1);
Threads.emplace_back(ReporterFn);
for (int i = 0; i < m_Concurrency; ++i)
{
Threads.emplace_back(WorkerFn);
}
for (std::thread& T : Threads)
{
T.join();
}
std::signal(SIGINT, PrevSigInt);
std::signal(SIGTERM, PrevSigTerm);
double TotalSeconds = RunTimer.GetElapsedTimeMs() / 1000.0;
int64_t TotalCount = TotalSuccessCount.load() + TotalErrorCount.load();
uint64_t DownPerSec = TotalSeconds > 0.0 ? static_cast<uint64_t>(TotalDownloadBytes.load() / TotalSeconds) : 0;
uint64_t UpPerSec = TotalSeconds > 0.0 ? static_cast<uint64_t>(TotalUploadBytes.load() / TotalSeconds) : 0;
ZEN_CONSOLE("Stopped. Total: {:L} requests, {:L} success, {:L} errors avg throughput: down {}/s up {}/s (elapsed: {:.2f}s)",
TotalCount,
TotalSuccessCount.load(),
TotalErrorCount.load(),
NiceBytes(DownPerSec),
NiceBytes(UpPerSec),
TotalSeconds);
}
//////////////////////////////////////////////////////////////////////////
// BenchCommand
BenchCommand::BenchCommand()
{
m_Options.add_options()("h,help", "Print help");
m_Options.add_option("__hidden__", "", "subcommand", "", cxxopts::value<std::string>(m_SubCommand)->default_value(""), "");
m_Options.parse_positional({"subcommand"});
AddSubCommand(m_PurgeSubCmd);
AddSubCommand(m_HttpSubCmd);
}
BenchCommand::~BenchCommand() = default;
} // namespace zen
|