From: Sebastian Schmidt Date: Wed, 12 Aug 2020 09:20:28 +0000 (+0200) Subject: Add a skeleton for benchmarking distribution_t. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=542f4ce3f7770f71946b12d62d2f0992e7ec6959;p=thirdparty%2Fcollectd.git Add a skeleton for benchmarking distribution_t. This adds a “distbench” subdirectory for benchmarking different implementations of distribution_t and three sample implementations. --- diff --git a/Makefile.am b/Makefile.am index 2acd0f146..e416f6577 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/configure.ac b/configure.ac index 45b94ce0d..57139f36b 100644 --- a/configure.ac +++ b/configure.ac @@ -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 index 000000000..85c52d30a --- /dev/null +++ b/distbench/Makefile.am @@ -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 index 000000000..cccdc4f4e --- /dev/null +++ b/distbench/bkjg.c @@ -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 index 000000000..c842bef1e --- /dev/null +++ b/distbench/function.h @@ -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 index 000000000..91c7d2f20 --- /dev/null +++ b/distbench/main.c @@ -0,0 +1,56 @@ +/* System headers */ +#include +#include +#include +#include +#include +#include + +/* 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 index 000000000..406082c22 --- /dev/null +++ b/distbench/margalit.c @@ -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 index 000000000..8de18fb9b --- /dev/null +++ b/distbench/run-all.sh @@ -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 index 000000000..5875f69ef --- /dev/null +++ b/distbench/sshmidt.c @@ -0,0 +1,3 @@ +unsigned int counter = 0; // Just an example + +int my_function() { return ++counter; }