benchtests: Use JSON for bench-rawmemchr output
authorMatheus Castanho <msc@linux.ibm.com>
Wed, 12 May 2021 14:27:16 +0000 (11:27 -0300)
committerMatheus Castanho <msc@linux.ibm.com>
Mon, 17 May 2021 14:10:19 +0000 (11:10 -0300)
Convert the output of benchtests/bench-rawmemchr to JSON like other string
benchmarks.  This makes the output more parseable and allows usage of
compare_strings.py, for example.

Reviewed-by: Lucas A. M. Magalhaes <lamm@linux.ibm.com>
benchtests/bench-rawmemchr.c

index 93b4a1e..80286b0 100644 (file)
@@ -23,6 +23,8 @@
 #define TEST_NAME "rawmemchr"
 #include "bench-string.h"
 
+#include "json-lib.h"
+
 typedef char *(*proto_t) (const char *, int);
 
 char *
@@ -37,7 +39,7 @@ IMPL (rawmemchr, 1)
 IMPL (generic_rawmemchr, 0)
 
 static void
-do_one_test (impl_t *impl, const char *s, int c, char *exp_res)
+do_one_test (json_ctx_t *json_ctx, impl_t *impl, const char *s, int c, char *exp_res)
 {
   size_t i, iters = INNER_LOOP_ITERS_LARGE * 4;
   timing_t start, stop, cur;
@@ -59,11 +61,11 @@ do_one_test (impl_t *impl, const char *s, int c, char *exp_res)
 
   TIMING_DIFF (cur, start, stop);
 
-  TIMING_PRINT_MEAN ((double) cur, (double) iters);
+  json_element_double (json_ctx, (double) cur / (double) iters);
 }
 
 static void
-do_test (size_t align, size_t pos, size_t len, int seek_char)
+do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len, int seek_char)
 {
   size_t i;
   char *result;
@@ -86,39 +88,61 @@ do_test (size_t align, size_t pos, size_t len, int seek_char)
   buf1[align + len] = -seek_char;
   result = (char *) (buf1 + align + pos);
 
-  printf ("Length %4zd, alignment %2zd:", pos, align);
+  json_element_object_begin (json_ctx);
+  json_attr_uint (json_ctx, "length", pos);
+  json_attr_uint (json_ctx, "alignment", align);
+  json_attr_uint (json_ctx, "char", seek_char);
+  json_array_begin (json_ctx, "timings");
 
   FOR_EACH_IMPL (impl, 0)
-    do_one_test (impl, (char *) (buf1 + align), seek_char, result);
+    do_one_test (json_ctx, impl, (char *) (buf1 + align), seek_char, result);
 
-  putchar ('\n');
+  json_array_end (json_ctx);
+  json_element_object_end (json_ctx);
 }
 
 int
 test_main (void)
 {
+  json_ctx_t json_ctx;
   size_t i;
 
   test_init ();
 
-  printf ("%20s", "");
+  json_init (&json_ctx, 0, stdout);
+
+  json_document_begin (&json_ctx);
+  json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
+
+  json_attr_object_begin (&json_ctx, "functions");
+  json_attr_object_begin (&json_ctx, TEST_NAME);
+  json_attr_string (&json_ctx, "bench-variant", "");
+
+  json_array_begin (&json_ctx, "ifuncs");
   FOR_EACH_IMPL (impl, 0)
-    printf ("\t%s", impl->name);
-  putchar ('\n');
+      json_element_string (&json_ctx, impl->name);
+  json_array_end (&json_ctx);
+
+  json_array_begin (&json_ctx, "results");
 
   for (i = 1; i < 7; ++i)
     {
-      do_test (0, 16 << i, 2048, 23);
-      do_test (i, 64, 256, 23);
-      do_test (0, 16 << i, 2048, 0);
-      do_test (i, 64, 256, 0);
+      do_test (&json_ctx, 0, 16 << i, 2048, 23);
+      do_test (&json_ctx, i, 64, 256, 23);
+      do_test (&json_ctx, 0, 16 << i, 2048, 0);
+      do_test (&json_ctx, i, 64, 256, 0);
     }
   for (i = 1; i < 32; ++i)
     {
-      do_test (0, i, i + 1, 23);
-      do_test (0, i, i + 1, 0);
+      do_test (&json_ctx, 0, i, i + 1, 23);
+      do_test (&json_ctx, 0, i, i + 1, 0);
     }
 
+  json_array_end (&json_ctx);
+  json_attr_object_end (&json_ctx);
+  json_attr_object_end (&json_ctx);
+  json_document_end (&json_ctx);
+
   return ret;
 }