test/kwbench.c, etc.: Tidy up and publish the keyword benchmark I used.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 10 Aug 2019 16:28:58 +0000 (17:28 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 10 Aug 2019 16:30:44 +0000 (17:30 +0100)
test/Makefile.am
test/kwbench-back.c [new file with mode: 0644]
test/kwbench.c [new file with mode: 0644]
test/kwbench.h [new file with mode: 0644]

index 8145746..34ca415 100644 (file)
@@ -73,6 +73,9 @@ check-local:: kwtest kwtest.ref
 check_PROGRAMS         += rat
 -include rat.c-dep rat.h-dep
 
+check_PROGRAMS         += kwbench
+kwbench_SOURCES                 = kwbench.c kwbench-back.c kwbench.h
+
 EXTRA_DIST             += rat.sod rat.ref
 nodist_rat_SOURCES      = rat.c rat.h
 BUILT_SOURCES          += $(nodist_rat_SOURCES)
diff --git a/test/kwbench-back.c b/test/kwbench-back.c
new file mode 100644 (file)
index 0000000..a6d5009
--- /dev/null
@@ -0,0 +1,47 @@
+/* -*-c-*-
+ *
+ * Keyword benchmark, backend
+ *
+ * (c) 2019 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the Sensible Object Design, an object system for C.
+ *
+ * SOD is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * SOD is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with SOD.  If not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdarg.h>
+
+#include "keyword.h"
+#include "kwbench.h"
+
+void kwbench_trivial(struct myobject *obj, int x, int y, int z)
+  { obj->x = x; obj->y = y; obj->z = z; }
+
+#define kwbench_KWSET(_)                                               \
+  _(int, x, 0)                                                         \
+  _(int, y, 0)                                                         \
+  _(int, z, 0)
+KWSET_STRUCT(kwbench);
+static KWSET_PARSEFN(kwbench)
+KWCALL void kwbench_keywords(struct myobject *obj, KWTAIL)
+{
+  KWPARSE(kwbench);
+  obj->x = kw.x; obj->y = kw.y; obj->z = kw.z;
+}
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/test/kwbench.c b/test/kwbench.c
new file mode 100644 (file)
index 0000000..2a63f40
--- /dev/null
@@ -0,0 +1,121 @@
+/* -*-c-*-
+ *
+ * Keyword benchmark, frontend
+ *
+ * (c) 2019 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the Sensible Object Design, an object system for C.
+ *
+ * SOD is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * SOD is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with SOD.  If not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+
+#include "keyword.h"
+#include "kwbench.h"
+
+#if defined(__GNUC__) && (defined(__i386__) || defined(__amd64__))
+#define TIMEWHAT "cy"
+typedef unsigned long long timer;
+static void init_timing(void) { ; }
+static timer when(void)
+{
+  uint32_t lo, hi;
+
+  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
+  return (((timer)hi << 32) | lo);
+}
+#elif defined(__linux__)
+#include <linux/perf_event.h>
+#include <asm/unistd.h>
+#include <unistd.h>
+
+#define TIMEWHAT "cy"
+typedef uint64_t timer;
+static int perf_fd = -1;
+static void init_timing(void)
+{
+  struct perf_event_attr attr = { 0 };
+
+  attr.type = PERF_TYPE_HARDWARE;
+  attr.size = sizeof(attr);
+  attr.config = PERF_COUNT_HW_CPU_CYCLES;
+  attr.disabled = 0;
+  attr.exclude_kernel = 1;
+  attr.exclude_hv = 1;
+
+  if ((perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0)) < 0)
+    perror("failed to open perf event");
+}
+static timer when(void)
+{
+  timer cy;
+  ssize_t n;
+
+  if (perf_fd == -1) goto fail;
+  n = read(perf_fd, &cy, sizeof(cy));
+  if (n != sizeof(cy)) {
+    if (n < 0) perror("failed to read perf event");
+    else fprintf(stderr, "unexpected short read from perf event\n");
+    close(perf_fd); perf_fd = -1;
+    goto fail;
+  }
+  return (cy);
+fail:
+  return (0);
+}
+#else
+#include <time.h>
+#define TIMEWHAT "ns"
+typedef double timer;
+static void init_timing(void) { ; }
+static timer when(void)
+{
+  struct timespec tv;
+  clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tv);
+  return (1e9*(timer)tv.tv_sec + tv.tv_nsec);
+}
+#endif
+
+#define NITER 100000000
+
+int main(void)
+{
+  timer t0, t1;
+  double t;
+  int i;
+  struct myobject obj;
+
+  init_timing();
+
+  t0 = when();
+  for (i = 0; i < NITER; i++) kwbench_trivial(&obj, 4, 0, 0);
+  t1 = when();
+  t = t1 - t0; printf("trivial: %g "TIMEWHAT"\n", t/NITER);
+
+  t0 = when();
+  for (i = 0; i < NITER; i++)
+    kwbench_keywords(&obj, KWARGS(K(z, 19)));
+  t1 = when();
+  t = t1 - t0; printf("keywords: %g "TIMEWHAT"\n", t/NITER);
+
+  return (0);
+}
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/test/kwbench.h b/test/kwbench.h
new file mode 100644 (file)
index 0000000..28a4aea
--- /dev/null
@@ -0,0 +1,47 @@
+/* -*-c-*-
+ *
+ * Keyword benchmark, header
+ *
+ * (c) 2019 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the Sensible Object Design, an object system for C.
+ *
+ * SOD is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * SOD is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with SOD.  If not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef KWBENCH_H
+#define KWBENCH_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+#include "keyword.h"
+
+struct myobject { int x, y, z; };
+
+extern void kwbench_trivial(struct myobject */*obj*/,
+                           int /*x*/, int /*y*/, int /*z*/);
+
+extern KWCALL void kwbench_keywords(struct myobject */*obj*/, KWTAIL);
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif