Measure accuracy by comparing known timings with CTRACK measurements
| 268 | |
| 269 | // Measure accuracy by comparing known timings with CTRACK measurements |
| 270 | std::pair<double, double> measure_accuracy() |
| 271 | { |
| 272 | std::cout << "\n=== Measuring Accuracy ===" << std::endl; |
| 273 | |
| 274 | // Clear any previous tracking data by getting and discarding results |
| 275 | ctrack::result_as_string(); |
| 276 | |
| 277 | // Run a controlled test with known timings |
| 278 | const int test_iterations = 100; |
| 279 | for (int i = 0; i < test_iterations; ++i) |
| 280 | { |
| 281 | level_1_function(10); |
| 282 | } |
| 283 | |
| 284 | // Get results |
| 285 | auto results = ctrack::result_as_string(); |
| 286 | |
| 287 | // Expected timings per iteration (in nanoseconds): |
| 288 | // leaf_function: 1000ns (called 20 times per iteration) = 20,000ns total per iteration |
| 289 | // level_3_function: 500ns + 2*1000ns = 2500ns (called 10 times per iteration) = 25,000ns total per iteration |
| 290 | // level_2_function: 300ns + 10*2500ns = 25,300ns (called 1 time per iteration) = 25,300ns total per iteration |
| 291 | // level_1_function: 200ns + 25,300ns = 25,500ns (called 1 time per iteration) = 25,500ns total per iteration |
| 292 | |
| 293 | struct ExpectedTiming |
| 294 | { |
| 295 | std::string name; |
| 296 | double expected_total_ns; |
| 297 | int call_count; |
| 298 | }; |
| 299 | |
| 300 | std::vector<ExpectedTiming> expected_timings = { |
| 301 | {"leaf_function", 1000.0 * 20 * test_iterations, 20 * test_iterations}, |
| 302 | {"level_3_function", 2500.0 * 10 * test_iterations, 10 * test_iterations}, |
| 303 | {"level_2_function", 25300.0 * 1 * test_iterations, 1 * test_iterations}, |
| 304 | {"level_1_function", 25500.0 * 1 * test_iterations, 1 * test_iterations}}; |
| 305 | |
| 306 | double total_expected_time = 0.0; |
| 307 | double total_actual_time = 0.0; |
| 308 | double max_absolute_error = 0.0; |
| 309 | |
| 310 | if (g_config.verbose) |
| 311 | { |
| 312 | std::cout << "Function accuracy analysis:" << std::endl; |
| 313 | } |
| 314 | |
| 315 | for (const auto &timing : expected_timings) |
| 316 | { |
| 317 | double actual_ns = parse_function_timing(results, timing.name); |
| 318 | if (actual_ns > 0) |
| 319 | { |
| 320 | double expected_ns = timing.expected_total_ns; |
| 321 | double absolute_error = std::abs(actual_ns - expected_ns); |
| 322 | double percent_error = (absolute_error / expected_ns) * 100.0; |
| 323 | |
| 324 | total_expected_time += expected_ns; |
| 325 | total_actual_time += actual_ns; |
| 326 | max_absolute_error = (std::max)(max_absolute_error, absolute_error); |
| 327 |
no test coverage detected