aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRuben Beltran del Rio <jj@r.bdr.sh>2025-12-12 12:32:06 +0100
committerRuben Beltran del Rio <jj@r.bdr.sh>2025-12-12 12:44:51 +0100
commit735e4e1c8f5b51480b801024fd4d142758644810 (patch)
tree06d0b74e68a8e49cdcb48ebe7eacd8551540022d /src
parente9553cf80c3a5588b68a38ea7a99936a8b9b88b7 (diff)
Update benchmark to use huge map and match JS style for comparison.
Diffstat (limited to 'src')
-rw-r--r--src/test.c64
1 files changed, 41 insertions, 23 deletions
diff --git a/src/test.c b/src/test.c
index 858aa74..5602853 100644
--- a/src/test.c
+++ b/src/test.c
@@ -92,46 +92,67 @@ void print_map_contents(const wmap_map_t* map) {
}
}
-int benchmark_parser(const char* filename, int iterations) {
- clock_t start, end;
- double total_time, avg_time;
- int i;
-
- printf("=== Benchmarking Parser ===\n");
- printf("File: %s\n", filename);
- printf("Iterations: %d\n", iterations);
+int benchmark_parser(const char* filename, double time_limit_seconds) {
+ clock_t start, end, current;
+ double elapsed_time;
+ int iterations = 0;
+ double avg_time_ns;
start = clock();
- for (i = 0; i < iterations; i++) {
+ while (1) {
wmap_map_t* map = wmap_parse_file(filename);
if (!map) {
- printf("Error: Failed to parse file on iteration %d\n", i);
+ printf("Error: Failed to parse file on iteration %d\n", iterations);
return 1;
}
wmap_map_free(map);
+ iterations++;
+
+ current = clock();
+ elapsed_time = ((double)(current - start)) / CLOCKS_PER_SEC;
+ if (elapsed_time >= time_limit_seconds) {
+ break;
+ }
}
end = clock();
- total_time = ((double)(end - start)) / CLOCKS_PER_SEC;
- avg_time = total_time / iterations;
+ elapsed_time = ((double)(end - start)) / CLOCKS_PER_SEC;
+ avg_time_ns = (elapsed_time / iterations) * 1000000000.0;
- printf("Total time: %.4f seconds\n", total_time);
- printf("Average time per parse: %.4f seconds\n", avg_time);
- printf("Parses per second: %.2f\n", 1.0 / avg_time);
+ printf(" Iterations: %d\n", iterations);
+ printf(" Avg time: %.2f ns/iter\n", avg_time_ns);
+ printf(" Ops/sec: %.2f\n", iterations / elapsed_time);
return 0;
}
+void run_benchmark_suite(const char* example_file, const char* huge_file) {
+ printf("=== Parsing Benchmark ===\n\n");
+
+ printf("Example map:\n");
+ benchmark_parser(example_file, 1.0);
+
+ printf("\nHuge map:\n");
+ benchmark_parser(huge_file, 1.0);
+}
+
int main(int argc, char* argv[]) {
const char* filename;
wmap_map_t* map;
if (argc < 2) {
- printf("Usage: %s <wmap_file> [benchmark_iterations]\n", argv[0]);
+ printf("Usage: %s <wmap_file> [benchmark]\n", argv[0]);
+ printf(" %s benchmark\n", argv[0]);
return 1;
}
+ /* Check if we're running the benchmark suite */
+ if (argc == 2 && strcmp(argv[1], "benchmark") == 0) {
+ run_benchmark_suite("doc/example.wmap", "doc/huge.wmap");
+ return 0;
+ }
+
filename = argv[1];
printf("=== WMAP Parser Test ===\n");
@@ -146,17 +167,14 @@ int main(int argc, char* argv[]) {
print_map_stats(map);
if (argc > 2 && strcmp(argv[2], "benchmark") == 0) {
- int iterations = 1000;
- if (argc > 3) {
- iterations = atoi(argv[3]);
- }
- benchmark_parser(filename, iterations);
+ wmap_map_free(map);
+ printf("\nRunning benchmark for %s...\n", filename);
+ benchmark_parser(filename, 1.0);
} else {
print_map_contents(map);
+ wmap_map_free(map);
}
- wmap_map_free(map);
-
printf("=== Test Complete ===\n");
return 0;
}