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/tel/metrics.hpp> using cubos::core::tel::Metrics;
The following example demonstrates a simple usage of the core::tel::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_SPAN_INFO("simulateFrame"); 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:
[17:21:59.677] [main.cpp:53 main] [thread21496] debug: Found new metric: "thread21496:simulateFrame:begin" [17:21:59.679] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.680] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.681] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.681] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.682] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.683] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.684] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.685] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.686] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.687] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:begin" : 5.0244E+8 [17:21:59.687] [main.cpp:53 main] [thread21496] debug: Found new metric: "thread21496:simulateFrame:end" [17:21:59.688] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.689] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.690] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.691] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.693] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.694] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.695] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.696] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.697] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.698] [main.cpp:58 main] [thread21496] debug: "thread21496:simulateFrame:end" : 5.0244E+8 [17:21:59.699] [main.cpp:53 main] [thread21496] debug: Found new metric: "thread21496:i" [17:21:59.700] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 0.0000 [17:21:59.701] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 1.0000 [17:21:59.701] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 2.0000 [17:21:59.702] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 3.0000 [17:21:59.703] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 4.0000 [17:21:59.704] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 5.0000 [17:21:59.705] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 6.0000 [17:21:59.706] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 7.0000 [17:21:59.707] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 8.0000 [17:21:59.708] [main.cpp:58 main] [thread21496] debug: "thread21496:i" : 9.0000