-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_benchmark.cpp
More file actions
556 lines (446 loc) · 22.2 KB
/
test_benchmark.cpp
File metadata and controls
556 lines (446 loc) · 22.2 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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* prometheus-cpp-lite - header-only C++ library for exposing Prometheus metrics
* https://github.com/biaks/prometheus-cpp-lite
*
* Copyright (c) 2026 Yan Kryukov ianiskr@gmail.com
* Licensed under the MIT License
*
* =============================================================================
* test_benchmark.cpp - Benchmark metric usage examples
*
* This file demonstrates every supported way to create and use Benchmark metrics
* with the prometheus-cpp-lite library — from the shortest one-liner forms
* that rely on the global registry, through explicit typed/untyped family
* wrappers, to the legacy APIs compatible with prometheus-cpp and
* prometheus-cpp-lite-core.
*
* Benchmark metric design philosophy
* -----------------------------------
* The benchmark_t metric is designed for per-thread elapsed time measurement.
* Each thread should own its own benchmark_metric_t instance (identified by a
* unique label, e.g. {"thread", "0"}), so that start()/stop() calls never
* race across threads. The underlying atomic<double> accumulator allows
* safe serialization from any thread, but the start/stop state machine is
* intentionally NOT thread-safe — this is by design to avoid lock overhead
* in hot measurement paths.
*
* Test list
* ---------
* test_main_1 — Shortest path: global registry + implicit family.
* test_main_2 — Per-thread pattern: family with thread-id labels.
* test_main_3 — Explicit untyped family wrapper (global registry, runtime type check).
* test_main_4 — Explicit typed family wrapper (global registry, compile-time type safety).
* test_main_5 — User-owned registry with implicit family.
* test_main_6 — User-owned registry with explicit untyped family wrapper.
* test_main_7 — User-owned registry with typed family wrapper (compile-time type safety).
* test_legacy_1 — Legacy prometheus-cpp API: standalone Builder + references.
* test_legacy_2 — Legacy prometheus-cpp-lite-core API: CustomFamily::Build() static method.
* test_legacy_3 — Legacy prometheus-cpp-lite-core API: Registry::Add<MetricType>() typed registration.
* test_legacy_4 — Legacy prometheus-cpp-lite-core API: untyped Family + explicit Family::Add<MetricType>().
* test_legacy_5 — Legacy SimpleAPI: metric wrappers with global registry (shortest form).
* test_legacy_6 — Legacy SimpleAPI: family wrapper + metric wrappers.
*/
#include "prometheus/benchmark.h"
#include <thread>
#include <vector>
#include <functional>
using namespace prometheus;
// =============================================================================
// Global registry definition
//
// Required when using the shortened construction chains that rely on `global_registry`.
// In a real application this should be defined once in a single .cpp file.
// =============================================================================
namespace prometheus {
registry_t global_registry;
}
// =============================================================================
// Helper: simulate work by sleeping for the given number of milliseconds.
// =============================================================================
static void simulate_work(int milliseconds) {
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
}
// =============================================================================
// test_main_1 - Shortest path with global registry and implicit family
//
// Chain: global_registry → (implicit family) → metric
//
// The simplest way to create a benchmark. A family is created automatically
// behind the scenes inside the global registry. The default value type is double
// (elapsed seconds).
// =============================================================================
void test_main_1() {
std::cout << "\n=== test_main_1 - Shortest path with global registry and implicit family ===\n";
benchmark_metric_t metric1 ("task_duration_seconds", "elapsed time of task1");
metric1.start();
simulate_work(15);
metric1.stop();
metric1.start();
simulate_work(10);
metric1.stop();
std::cout << " - metric1 accumulated time: " << metric1.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << global_registry.serialize();
}
// =============================================================================
// test_main_2 - Per-thread pattern: family with thread-id labels
//
// Chain: global_registry → family → metric (per thread)
//
// This is the RECOMMENDED usage pattern for benchmark_t.
//
// Each thread creates its own benchmark_metric_t with a unique label
// (e.g. thread index). Since start()/stop() state is local to each
// instance, there is no contention between threads. The accumulated
// elapsed time is stored in a per-metric atomic, so serialization
// from any thread is always safe.
//
// ┌──────────┐ ┌──────────────────────────┐
// │ Thread 0 │──▶│ metric {thread="0"} │──▶ start/stop (no lock)
// └──────────┘ └──────────────────────────┘
// ┌──────────┐ ┌──────────────────────────┐
// │ Thread 1 │──▶│ metric {thread="1"} │──▶ start/stop (no lock)
// └──────────┘ └──────────────────────────┘
// ┌──────────┐ ┌──────────────────────────┐
// │ Thread 2 │──▶│ metric {thread="2"} │──▶ start/stop (no lock)
// └──────────┘ └──────────────────────────┘
// │
// ▼
// ┌──────────────────┐
// │ serialize() from │ (safe: reads atomic values)
// │ any thread │
// └──────────────────┘
// =============================================================================
void test_main_2() {
std::cout << "\n=== test_main_2 - Per-thread pattern: family with thread-id labels ===\n";
benchmark_family_t worker_time ("worker_duration_seconds", "per-thread elapsed time");
constexpr int NUM_THREADS = 4;
std::vector<std::thread> threads;
// Each thread gets its own metric identified by thread index.
// No synchronization is needed for start()/stop() — each thread
// exclusively owns its metric instance.
for (int i = 0; i < NUM_THREADS; ++i) {
threads.emplace_back([&worker_time, i]() {
benchmark_metric_t my_metric(worker_time, {{"thread", std::to_string(i)}});
// Simulate several work iterations.
for (int iter = 0; iter < 3; ++iter) {
my_metric.start();
simulate_work(5 + i * 3); // Different work per thread.
my_metric.stop();
}
std::cout << " - thread " << i << " accumulated time: " << my_metric.Get() << " sec" << std::endl;
});
}
for (auto& t : threads)
t.join();
// Serialization is safe from the main thread — reads atomic accumulators.
std::cout << " - output serialized data:\n" << global_registry.serialize();
}
// =============================================================================
// test_main_3 - Explicit untyped family wrapper (global registry)
//
// Chain: global_registry → family → metric
//
// Creates an untyped family_t wrapper. The metric type check (ensuring all
// metrics in the family share the same concrete type) happens at runtime.
// =============================================================================
void test_main_3() {
std::cout << "\n=== test_main_3 - Explicit untyped family wrapper (global registry) ===\n";
family_t durations ("pipeline_duration_seconds", "pipeline stage durations");
benchmark_metric_t metric_load (durations, {{"stage", "load"}});
benchmark_metric_t metric_proc (durations, {{"stage", "process"}});
metric_load.start();
simulate_work(10);
metric_load.stop();
metric_proc.start();
simulate_work(20);
metric_proc.stop();
std::cout << " - metric_load time: " << metric_load.Get() << " sec" << std::endl;
std::cout << " - metric_proc time: " << metric_proc.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << global_registry.serialize();
}
// =============================================================================
// test_main_4 - Explicit typed family wrapper (global registry, compile-time safety)
//
// Chain: global_registry → family → metric
//
// Uses typed family wrapper so the metric type is fixed at compile time —
// no runtime type mismatch is possible.
// =============================================================================
void test_main_4() {
std::cout << "\n=== test_main_4 - Explicit typed family wrapper (global registry, compile-time safety) ===\n";
benchmark_family_t durations ("service_duration_seconds", "service call durations");
benchmark_metric_t metric_auth (durations, {{"service", "auth"}});
benchmark_metric_t metric_search (durations, {{"service", "search"}});
metric_auth.start();
simulate_work(8);
metric_auth.stop();
metric_search.start();
simulate_work(25);
metric_search.stop();
std::cout << " - metric_auth time: " << metric_auth.Get() << " sec" << std::endl;
std::cout << " - metric_search time: " << metric_search.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << global_registry.serialize();
}
// =============================================================================
// test_main_5 - User-owned registry with implicit family
//
// Chain: registry → (implicit family) → metric
//
// Same as test_main_1, but with a locally created registry instead of the
// global one.
// =============================================================================
void test_main_5() {
std::cout << "\n=== test_main_5 - User-owned registry with implicit family ===\n";
registry_t registry;
benchmark_metric_t metric1 (registry, "job_duration_seconds", "job elapsed time");
metric1.start();
simulate_work(12);
metric1.stop();
std::cout << " - metric1 time: " << metric1.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << registry.serialize();
}
// =============================================================================
// test_main_6 - User-owned registry with explicit untyped family wrapper
//
// Chain: registry → family → metric
//
// Combines a user-owned registry with an explicit untyped family wrapper.
// =============================================================================
void test_main_6() {
std::cout << "\n=== test_main_6 - User-owned registry with explicit untyped family ===\n";
registry_t registry;
family_t durations (registry, "etl_duration_seconds", "ETL stage durations",
{{"host", "localhost"}});
benchmark_metric_t metric_extract (durations, {{"stage", "extract"}});
benchmark_metric_t metric_transform(durations, {{"stage", "transform"}});
benchmark_metric_t metric_load (durations, {{"stage", "load"}});
metric_extract.start();
simulate_work(10);
metric_extract.stop();
metric_transform.start();
simulate_work(20);
metric_transform.stop();
metric_load.start();
simulate_work(5);
metric_load.stop();
std::cout << " - metric_extract time: " << metric_extract.Get() << " sec" << std::endl;
std::cout << " - metric_transform time: " << metric_transform.Get() << " sec" << std::endl;
std::cout << " - metric_load time: " << metric_load.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << registry.serialize();
}
// =============================================================================
// test_main_7 - User-owned registry with typed family wrapper (compile-time safety)
//
// Chain: registry → family → metric
//
// Combines a user-owned registry with a typed family wrapper so the metric type
// is fixed at compile time — no runtime type mismatch is possible.
//
// Demonstrates the per-thread pattern with a user-owned registry.
// =============================================================================
void test_main_7() {
std::cout << "\n=== test_main_7 - User-owned registry with typed family wrapper + per-thread pattern ===\n";
std::shared_ptr<registry_t> registry = std::make_shared<registry_t>();
benchmark_family_t durations (registry, "render_duration_seconds", "per-thread render time", {{"host", "localhost"}});
constexpr int NUM_THREADS = 3;
std::vector<std::thread> threads;
for (int i = 0; i < NUM_THREADS; ++i) {
threads.emplace_back([&durations, i]() {
benchmark_metric_t my_metric(durations, {{"thread", std::to_string(i)}});
my_metric.start();
simulate_work(10 + i * 5);
my_metric.stop();
std::cout << " - thread " << i << " render time: " << my_metric.Get() << " sec" << std::endl;
});
}
for (auto& t : threads)
t.join();
std::cout << " - output serialized data:\n" << registry->serialize();
}
// =============================================================================
// test_legacy_1 - Legacy prometheus-cpp like API: standalone Builder + references
//
// Chain: Registry& → BuildBenchmark().Register() → CustomFamily& → Benchmark&
//
// Uses the fluent Builder to construct and register a CustomFamily, then
// obtains raw Benchmark references.
// =============================================================================
void test_legacy_1() {
std::cout << "\n=== test_legacy_1 - Legacy prometheus-cpp like API: standalone Builder + references ===\n";
typedef Benchmark<double> DoubleBenchmark;
typedef CustomFamily<DoubleBenchmark> MetricFamily;
Registry registry;
MetricFamily& durations = BuildBenchmark().Name("compile_duration_seconds")
.Help("compilation elapsed time")
.Labels({{"host", "localhost"}, {"legacy", "yes"}})
.Register(registry);
DoubleBenchmark& metric_parse = durations.Add({{"phase", "parse"}});
DoubleBenchmark& metric_codegen = durations.Add({{"phase", "codegen"}});
metric_parse.start();
simulate_work(15);
metric_parse.stop();
metric_codegen.start();
simulate_work(30);
metric_codegen.stop();
std::cout << " - metric_parse time: " << metric_parse.Get() << " sec" << std::endl;
std::cout << " - metric_codegen time: " << metric_codegen.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << registry.serialize();
}
// =============================================================================
// test_legacy_2 - Legacy prometheus-cpp-lite-core API: CustomFamily::Build() static method
//
// Chain: Registry& → CustomFamily::Build() → CustomFamily& → Benchmark&
//
// Same as test_legacy_1 but uses the static Build() method on CustomFamily.
// =============================================================================
void test_legacy_2() {
std::cout << "\n=== test_legacy_2 - Legacy prometheus-cpp-lite-core API: CustomFamily::Build() static method ===\n";
typedef Benchmark<double> DoubleBenchmark;
typedef CustomFamily<DoubleBenchmark> MetricFamily;
Registry registry;
MetricFamily& durations = MetricFamily::Build(registry, "deploy_duration_seconds", "deployment time",
{{"host", "localhost"}});
DoubleBenchmark& metric_build = durations.Add({{"step", "build"}});
DoubleBenchmark& metric_deploy = durations.Add({{"step", "deploy"}});
metric_build.start();
simulate_work(20);
metric_build.stop();
metric_deploy.start();
simulate_work(10);
metric_deploy.stop();
std::cout << " - metric_build time: " << metric_build.Get() << " sec" << std::endl;
std::cout << " - metric_deploy time: " << metric_deploy.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << registry.serialize();
}
// =============================================================================
// test_legacy_3 - Legacy prometheus-cpp-lite-core API: Registry::Add<MetricType>() typed registration
//
// Chain: Registry& → Registry::Add<Benchmark>() → CustomFamily& → Benchmark&
//
// Registers a typed family directly on the registry.
// =============================================================================
void test_legacy_3() {
std::cout << "\n=== test_legacy_3 - Legacy prometheus-cpp-lite-core API: Registry::Add<MetricType>() typed registration ===\n";
typedef Benchmark<double> DoubleBenchmark;
typedef CustomFamily<DoubleBenchmark> MetricFamily;
Registry registry;
MetricFamily& durations = registry.Add<DoubleBenchmark>("test_duration_seconds", "test suite timing",
{{"host", "localhost"}});
DoubleBenchmark& metric_unit = durations.Add({{"suite", "unit"}});
DoubleBenchmark& metric_integ = durations.Add({{"suite", "integration"}});
metric_unit.start();
simulate_work(8);
metric_unit.stop();
metric_integ.start();
simulate_work(35);
metric_integ.stop();
std::cout << " - metric_unit time: " << metric_unit.Get() << " sec" << std::endl;
std::cout << " - metric_integ time: " << metric_integ.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << registry.serialize();
}
// =============================================================================
// test_legacy_4 - Legacy prometheus-cpp-lite-core API: untyped Family + explicit Family::Add<MetricType>()
//
// Chain: Registry& → Registry::Add() → Family& → Family::Add<Benchmark>()
//
// The most explicit form: the family is untyped, and every Add() call
// must specify the concrete metric type as a template argument.
// =============================================================================
void test_legacy_4() {
std::cout << "\n=== test_legacy_4 - Legacy prometheus-cpp-lite-core API: untyped Family + explicit Family::Add<MetricType>() ===\n";
Registry registry;
Family& durations = registry.Add("io_duration_seconds", "I/O elapsed time",
{{"host", "localhost"}});
Benchmark<double>& metric_read = durations.Add<Benchmark<double>>({{"operation", "read"}});
Benchmark<double>& metric_write = durations.Add<Benchmark<double>>({{"operation", "write"}});
metric_read.start();
simulate_work(12);
metric_read.stop();
metric_write.start();
simulate_work(18);
metric_write.stop();
std::cout << " - metric_read time: " << metric_read.Get() << " sec" << std::endl;
std::cout << " - metric_write time: " << metric_write.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << registry.serialize();
}
// =============================================================================
// Legacy SimpleAPI examples
// =============================================================================
// suppress deprecation warnings - legacy API is intentionally used here to verify backward compatibility
#ifdef _MSC_VER
#pragma warning(disable: 4996) // deprecated declaration
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <prometheus/simpleapi.h>
// =============================================================================
// test_legacy_5 - Legacy SimpleAPI: metric wrappers with global registry (shortest form)
//
// Chain: global_registry → (implicit family) → metric
//
// Identical to test_main_1 but uses simpleapi namespace aliases.
// =============================================================================
void test_legacy_5() {
std::cout << "\n=== test_legacy_5 - Legacy SimpleAPI: metric wrappers with global registry (shortest form) ===\n";
global_registry.RemoveAll(); // Clear global registry for a clean test.
simpleapi::benchmark_metric_t metric1 { "operation_duration", "operation elapsed time" };
metric1.start();
simulate_work(15);
metric1.stop();
std::cout << " - metric1 time: " << metric1.Get() << " sec" << std::endl;
std::cout << " - output serialized data:\n" << global_registry.serialize();
}
// =============================================================================
// test_legacy_6 - Legacy SimpleAPI: family wrapper + metric wrappers
//
// Chain: global_registry → simpleapi::benchmark_family_t → simpleapi::benchmark_metric_t
//
// Uses the simpleapi namespace aliases and family.Add() to create metrics.
// Demonstrates per-thread ownership: each worker gets its own metric handle.
// =============================================================================
void test_legacy_6() {
std::cout << "\n=== test_legacy_6 - Legacy SimpleAPI: family wrapper + metric wrappers ===\n";
global_registry.RemoveAll(); // Clear global registry for a clean test.
simpleapi::benchmark_family_t metric_family { "worker_elapsed", "per-worker elapsed time" };
constexpr int NUM_THREADS = 2;
std::vector<std::thread> threads;
for (int i = 0; i < NUM_THREADS; ++i) {
threads.emplace_back([&metric_family, i]() {
// Each thread owns its metric — no start/stop contention.
simpleapi::benchmark_metric_t my_metric { metric_family.Add({{"worker", std::to_string(i)}}) };
my_metric.start();
simulate_work(10 + i * 10);
my_metric.stop();
std::cout << " - worker " << i << " time: " << my_metric.Get() << " sec" << std::endl;
});
}
for (auto& t : threads)
t.join();
std::cout << " - output serialized data:\n" << global_registry.serialize();
}
// =============================================================================
// main
// =============================================================================
int main() {
std::cout << "=== Benchmark metric - all usage variants ===\n";
try {
test_main_1();
test_main_2();
test_main_3();
test_main_4();
test_main_5();
test_main_6();
test_main_7();
test_legacy_1();
test_legacy_2();
test_legacy_3();
test_legacy_4();
test_legacy_5();
test_legacy_6();
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}