diff --git a/demo/unit_test.c b/demo/unit_test.c index e9cd708..e70211e 100644 --- a/demo/unit_test.c +++ b/demo/unit_test.c @@ -22,6 +22,7 @@ typedef struct { char message[256]; + char benchmark_summary[256]; } test_context_t; typedef int (*test_fn_t)(test_context_t *context); @@ -1226,7 +1227,7 @@ static int test_hook_alloc_fail_internal_v2_memory_copy(test_context_t *context) static int test_binary_v2_file_benchmark(test_context_t *context) { - enum { benchmark_iterations = 100 }; + enum { benchmark_iterations = 1000 }; const char *path = "demo_benchmark_v2.ikvb"; ikv_node_t *root = NULL; double total_index_us = 0.0; @@ -1296,8 +1297,8 @@ static int test_binary_v2_file_benchmark(test_context_t *context) if (result == 0) { - snprintf(context->message, - sizeof(context->message), + snprintf(context->benchmark_summary, + sizeof(context->benchmark_summary), "avg index %.2fus avg read %.2fus avg write %.2fus over %u iterations", total_index_us / (double)benchmark_iterations, total_read_us / (double)benchmark_iterations, @@ -1399,12 +1400,14 @@ static void log_case(bool passed, unsigned int index, unsigned int total, long e total, name); - if (message && message[0] != 0) + if (!passed && message && message[0] != 0) printf(" %s- %s%s", ANSI_YELLOW, message, ANSI_RESET); putchar('\n'); } +static char benchmark_summary[256]; + static int run_test_case(unsigned int index, unsigned int total) { test_context_t context; @@ -1422,6 +1425,9 @@ static int run_test_case(unsigned int index, unsigned int total) end_time = clock(); elapsed_ms = (long)(((end_time - start_time) * 1000) / CLOCKS_PER_SEC); + if (context.benchmark_summary[0] != 0) + snprintf(benchmark_summary, sizeof(benchmark_summary), "%s", context.benchmark_summary); + log_case(result == 0, index + 1u, total, elapsed_ms, test_cases[index].name, context.message); return result; } @@ -1451,6 +1457,8 @@ int main(int argc, char **argv) const unsigned int total = (unsigned int)(sizeof(test_cases) / sizeof(test_cases[0])); unsigned int passed = 0u; + benchmark_summary[0] = 0; + if (argc == 3 && strcmp(argv[1], "--case") == 0) { unsigned int index = 0u; @@ -1461,7 +1469,11 @@ int main(int argc, char **argv) return 2; } - return run_test_case(index, total); + if (run_test_case(index, total) != 0) + return 1; + if (benchmark_summary[0] != 0) + printf("benchmark: %s\n", benchmark_summary); + return 0; } if (argc != 1) @@ -1485,6 +1497,8 @@ int main(int argc, char **argv) ANSI_RESET, passed, total); + if (benchmark_summary[0] != 0) + printf("benchmark: %s\n", benchmark_summary); return 1; } @@ -1495,5 +1509,7 @@ int main(int argc, char **argv) ANSI_RESET, passed, total); + if (benchmark_summary[0] != 0) + printf("benchmark: %s\n", benchmark_summary); return 0; }