Metrics
Using the metrics system.
Registering metrics and profiling in Cubos can be done through the metric macros defined in core/-DENABLE_PROFILING
.
#include <cubos/core/metrics.hpp> using cubos::core::Metrics;
The following example demonstrates a simple usage of the core::Metrics class, simulating a random function that takes X milliseconds to finish, where X is randomly generated, and also registering metrics values.
This is the main loop:
const int numFrames = 10; for (int i = 0; i < numFrames; ++i) { // simulate frame by calling a function that does work.. simulateFrame(randomMs(0, 500)); // register some metrics, this could be FPS, entities count, .... CUBOS_METRIC("i", i); }
Which calls these utility functions:
static void simulateFrame(int ms) { CUBOS_PROFILE(); std::this_thread::sleep_for(std::chrono::milliseconds(ms)); }
static int randomMs(int min, int max) { int range = (min - max) + 1; return rand() % range + min; }
We can now get the existing metrics and their values:
std::string name; std::size_t seenCount = 0; while (Metrics::readName(name, seenCount)) { CUBOS_DEBUG("Found new metric: {}", name); double value; std::size_t offset = 0; while (Metrics::readValue(name, value, offset)) { CUBOS_DEBUG("{} : {}", name, value); } }
Output:
[11:34:13.824] [main.cpp:51 main] debug: Found new metric: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" [11:34:13.824] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 103.0721 [11:34:13.824] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 469.1480 [11:34:13.825] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 379.0656 [11:34:13.825] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 388.1754 [11:34:13.825] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 37.1140 [11:34:13.825] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 14.0966 [11:34:13.825] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 8.1281 [11:34:13.825] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 464.1636 [11:34:13.826] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 313.1553 [11:34:13.826] [main.cpp:56 main] debug: "profiling:/home/cubos/core/samples/metrics/main.cpp:simulateFrame:27" : 57.0801 [11:34:13.826] [main.cpp:51 main] debug: Found new metric: "i" [11:34:13.826] [main.cpp:56 main] debug: "i" : 0.0000 [11:34:13.827] [main.cpp:56 main] debug: "i" : 1.0000 [11:34:13.827] [main.cpp:56 main] debug: "i" : 2.0000 [11:34:13.827] [main.cpp:56 main] debug: "i" : 3.0000 [11:34:13.827] [main.cpp:56 main] debug: "i" : 4.0000 [11:34:13.827] [main.cpp:56 main] debug: "i" : 5.0000 [11:34:13.828] [main.cpp:56 main] debug: "i" : 6.0000 [11:34:13.828] [main.cpp:56 main] debug: "i" : 7.0000 [11:34:13.828] [main.cpp:56 main] debug: "i" : 8.0000 [11:34:13.828] [main.cpp:56 main] debug: "i" : 9.0000