]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Add a skeleton for benchmarking distribution_t.
authorSebastian Schmidt <yath@google.com>
Wed, 12 Aug 2020 09:20:28 +0000 (11:20 +0200)
committerSebastian Schmidt <yath@google.com>
Wed, 12 Aug 2020 09:30:42 +0000 (11:30 +0200)
This adds a “distbench” subdirectory for benchmarking different
implementations of distribution_t and three sample implementations.

Makefile.am
configure.ac
distbench/Makefile.am [new file with mode: 0644]
distbench/bkjg.c [new file with mode: 0644]
distbench/function.h [new file with mode: 0644]
distbench/main.c [new file with mode: 0644]
distbench/margalit.c [new file with mode: 0644]
distbench/run-all.sh [new file with mode: 0755]
distbench/sshmidt.c [new file with mode: 0644]

index 2acd0f1468890b3003514305f2ae8bf42749cd61..e416f6577e3d81c40d6c6af4258266d7c7dfee0e 100644 (file)
@@ -1,6 +1,8 @@
 ACLOCAL_AMFLAGS = -I m4
 AM_YFLAGS = -d
 
+SUBDIRS = distbench # Benchmark for distribution_t.
+
 if BUILD_WIN32
 cpkgdatadir=$(datadir)
 cpkglibdir=$(libdir)/plugins
index 45b94ce0d2a6c7d207b27fd89698d7aba36f60fa..57139f36bfcb472c5ff0df73fb3da490e82cfeb9 100644 (file)
@@ -7333,6 +7333,7 @@ AC_SUBST([AM_CXXFLAGS])
 
 AC_CONFIG_FILES([ \
   Makefile \
+  distbench/Makefile \
   src/collectd.conf \
   src/libcollectdclient/libcollectdclient.pc \
 ])
diff --git a/distbench/Makefile.am b/distbench/Makefile.am
new file mode 100644 (file)
index 0000000..85c52d3
--- /dev/null
@@ -0,0 +1,10 @@
+bin_PROGRAMS = bkjg margalit sshmidt
+
+# If you need special libraries, e.g. libm (math lib), declare them like so:
+# xxx_LIBS = -lm
+
+bkjg_SOURCES = main.c bkjg.c
+
+margalit_SOURCES = main.c margalit.c
+
+sshmidt_SOURCES = main.c sshmidt.c
diff --git a/distbench/bkjg.c b/distbench/bkjg.c
new file mode 100644 (file)
index 0000000..cccdc4f
--- /dev/null
@@ -0,0 +1,3 @@
+int counter = 0; // Just an example
+
+int my_function() { return ++counter; }
diff --git a/distbench/function.h b/distbench/function.h
new file mode 100644 (file)
index 0000000..c842bef
--- /dev/null
@@ -0,0 +1,3 @@
+/* Example function that returns something. */
+
+extern int my_function();
diff --git a/distbench/main.c b/distbench/main.c
new file mode 100644 (file)
index 0000000..91c7d2f
--- /dev/null
@@ -0,0 +1,56 @@
+/* System headers */
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+/* Local headers */
+#include "function.h"
+
+/* Macro to exit with an error. */
+#define error(fmt, args...)                                                    \
+  do {                                                                         \
+    fprintf(stderr, "ERROR: " fmt "\n", ##args);                               \
+    _exit(1);                                                                  \
+  } while (0)
+
+/* How many nanoseconds there are in a second. */
+#define NANOS_PER_SECOND 10000000
+
+/* How many iterations to run. */
+#define ITERATIONS 10000000
+
+/* Returns the monotonic clock in nanoseconds. */
+static uint64_t get_clock() {
+  struct timespec ts;
+
+  if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
+    error("Unable to retrieve monotonic clock: %s", strerror(errno));
+
+  return (ts.tv_sec * NANOS_PER_SECOND) + ts.tv_nsec;
+}
+
+int main(int argc, char **argv) {
+  uint64_t start = get_clock();
+
+  for (int i = 0; i < ITERATIONS; i++) {
+    /* Store result in a volatile to prevent the compiler from ignoring the call
+     * because the result is unused. */
+    volatile int result = my_function();
+
+    /* Prevent "unused variable" warning/error. */
+    (void)result;
+  }
+
+  uint64_t end = get_clock();
+  uint64_t duration = end - start;
+
+  printf("%s: %d iterations took %f seconds (~ %d nanoseconds per call)\n",
+         argv[0],    /* Program name */
+         ITERATIONS, /* Number of iterations */
+         (duration * 1.0 /
+          NANOS_PER_SECOND), /* *1.0 to force floating-point arithmetic */
+         (int)(duration / ITERATIONS)); /* Only integer division here */
+}
diff --git a/distbench/margalit.c b/distbench/margalit.c
new file mode 100644 (file)
index 0000000..406082c
--- /dev/null
@@ -0,0 +1,3 @@
+int counter = ~0; // Just an example
+
+int my_function() { return --counter; }
diff --git a/distbench/run-all.sh b/distbench/run-all.sh
new file mode 100755 (executable)
index 0000000..8de18fb
--- /dev/null
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+# Change into distbench/ directory if not already.
+cd "$(dirname "$(readlink -f "$0")")"
+
+# (Re)build, just in case.
+make
+
+# Run all three benchmarks.
+for program in bkjg margalit sshmidt; do
+    echo "Running $program"
+    ./"$program"
+    echo
+done
diff --git a/distbench/sshmidt.c b/distbench/sshmidt.c
new file mode 100644 (file)
index 0000000..5875f69
--- /dev/null
@@ -0,0 +1,3 @@
+unsigned int counter = 0; // Just an example
+
+int my_function() { return ++counter; }