test: add reproducer

This commit is contained in:
Gregor Jasny
2023-12-27 16:30:54 +01:00
parent c9ffcdda90
commit 6dfb47562d
+42 -37
View File
@@ -12,10 +12,13 @@
#include "prometheus/info.h"
#include "prometheus/registry.h"
#ifdef __linux__
#include <malloc.h>
#endif
int main() {
using namespace prometheus;
// create a http server running on port 8080
Exposer exposer{"127.0.0.1:8080"};
// create a metrics registry
@@ -27,51 +30,53 @@ int main() {
//
// @note please follow the metric-naming best-practices:
// https://prometheus.io/docs/practices/naming/
auto& packet_counter = BuildCounter()
.Name("observed_packets_total")
.Help("Number of observed packets")
.Register(*registry);
auto& test_gauge = BuildGauge()
.Name("test_gauge")
.Help("Test gauge")
.Register(*registry);
// add and remember dimensional data, incrementing those is very cheap
auto& tcp_rx_counter =
packet_counter.Add({{"protocol", "tcp"}, {"direction", "rx"}});
auto& tcp_tx_counter =
packet_counter.Add({{"protocol", "tcp"}, {"direction", "tx"}});
auto& udp_rx_counter =
packet_counter.Add({{"protocol", "udp"}, {"direction", "rx"}});
auto& udp_tx_counter =
packet_counter.Add({{"protocol", "udp"}, {"direction", "tx"}});
const char *labels[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
printf("Start adding data\n");
clock_t start_time = clock();
// add a counter whose dimensional data is not known at compile time
// nevertheless dimensional values should only occur in low cardinality:
// https://prometheus.io/docs/practices/naming/#labels
auto& http_requests_counter = BuildCounter()
.Name("http_requests_total")
.Help("Number of HTTP requests")
.Register(*registry);
for(int i=0;i<200000;i++)
{
for(int j=0;j<10;j++)
{
std::string str = std::to_string(i);
auto &tg = test_gauge.Add({{"label1",str}, {"label2",labels[j]}});
tg.Set(i);
}
}
printf("End adding data\n");
clock_t end_time = clock();
double duration = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("duration: %f\n",duration);
auto& version_info = BuildInfo()
.Name("versions")
.Help("Static info about the library")
.Register(*registry);
version_info.Add({{"prometheus", "1.0"}});
// ask the exposer to scrape the registry on incoming HTTP requests
exposer.RegisterCollectable(registry);
for (;;) {
std::this_thread::sleep_for(std::chrono::seconds(1));
const auto random_value = std::rand();
std::this_thread::sleep_for(std::chrono::seconds(5));
if (random_value & 1) tcp_rx_counter.Increment();
if (random_value & 2) tcp_tx_counter.Increment();
if (random_value & 4) udp_rx_counter.Increment();
if (random_value & 8) udp_tx_counter.Increment();
#ifdef __linux__
struct mallinfo2 mi;
const std::array<std::string, 4> methods = {"GET", "PUT", "POST", "HEAD"};
auto method = methods.at(random_value % methods.size());
// dynamically calling Family<T>.Add() works but is slow and should be
// avoided
http_requests_counter.Add({{"method", method}}).Increment();
mi = mallinfo2();
printf("Total non-mmapped bytes (arena): %zu\n", mi.arena);
printf("# of free chunks (ordblks): %zu\n", mi.ordblks);
printf("# of free fastbin blocks (smblks): %zu\n", mi.smblks);
printf("# of mapped regions (hblks): %zu\n", mi.hblks);
printf("Bytes in mapped regions (hblkhd): %zu\n", mi.hblkhd);
printf("Max. total allocated space (usmblks): %zu\n", mi.usmblks);
printf("Free bytes held in fastbins (fsmblks): %zu\n", mi.fsmblks);
printf("Total allocated space (uordblks): %zu\n", mi.uordblks);
printf("Total free space (fordblks): %zu\n", mi.fordblks);
printf("Topmost releasable block (keepcost): %zu\n", mi.keepcost);
#endif
}
return 0;
}