From 7a954636495111da9dd85dbdd6a33be35d60cb96 Mon Sep 17 00:00:00 2001
From: Matthew Blue <matthew.blue.neuro@gmail.com>
Date: Tue, 22 May 2018 18:09:16 -0400
Subject: [PATCH] tests/cb_mux: initial benchmark routine

---
 tests/cb_mux_bench/Makefile        | 11 ++++
 tests/cb_mux_bench/main.c          | 90 ++++++++++++++++++++++++++++++
 tests/cb_mux_bench/tests/01-run.py | 25 +++++++++
 3 files changed, 126 insertions(+)
 create mode 100644 tests/cb_mux_bench/Makefile
 create mode 100644 tests/cb_mux_bench/main.c
 create mode 100755 tests/cb_mux_bench/tests/01-run.py

diff --git a/tests/cb_mux_bench/Makefile b/tests/cb_mux_bench/Makefile
new file mode 100644
index 0000000000..61b37720f2
--- /dev/null
+++ b/tests/cb_mux_bench/Makefile
@@ -0,0 +1,11 @@
+include ../Makefile.tests_common
+
+USEMODULE += cb_mux \
+             xtimer
+
+TEST_ON_CI_WHITELIST += all
+
+include $(RIOTBASE)/Makefile.include
+
+test:
+	./tests/01-run.py
diff --git a/tests/cb_mux_bench/main.c b/tests/cb_mux_bench/main.c
new file mode 100644
index 0000000000..478d4f1657
--- /dev/null
+++ b/tests/cb_mux_bench/main.c
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2018 Acutam Automation, LLC
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ */
+
+/**
+ * @ingroup     tests
+ * @{
+ *
+ * @file
+ * @brief       cb_mux benchmark application
+ *
+ * @author      Matthew Blue <matthew.blue.neuro@gmail.com>
+ * @}
+ */
+
+#include <stdio.h>
+
+#include "cb_mux.h"
+#include "xtimer.h"
+
+/* Number of entries in the cb_mux list */
+#define NUM_ENTRIES (20U)
+
+/* Fail if us greater than threshold */
+#define FAIL_THRESH (200UL)
+
+/* Head of cb_mux list */
+cb_mux_t *cb_mux_head;
+
+/* cb_mux list entries */
+cb_mux_t entries[NUM_ENTRIES];
+
+/* Timing */
+unsigned long time_prev, time_curr;
+
+void cb(void *arg)
+{
+    (void)arg;
+    time_curr = xtimer_now_usec();
+}
+
+int main(void)
+{
+    unsigned long xtimer_delay, time_diff;
+    uint8_t num;
+    cb_mux_t *entry;
+
+    puts("cb_mux benchmark application");
+
+    /* Delay due to fetching timer with xtimer */
+    time_prev = xtimer_now_usec();
+    xtimer_delay = time_prev - xtimer_now_usec();
+
+    /* Test for worst case: finding last entry */
+    entries[NUM_ENTRIES - 1].cbid = 1;
+
+    printf("Populating cb_mux list with %u items\n", NUM_ENTRIES);
+
+    for (num = 0; num < NUM_ENTRIES; num++) {
+        entries[num].cb = cb;
+        cb_mux_add(&cb_mux_head, &(entries[num]));
+    }
+
+    puts("Finding the last list entry");
+
+    time_prev = xtimer_now_usec();
+
+    entry = cb_mux_find_cbid(cb_mux_head, 1);
+    entry->cb(entry->arg);
+
+    time_diff = time_curr - time_prev - xtimer_delay;
+
+    printf("List walk time: %lu us\n", time_diff);
+
+    if (time_diff > FAIL_THRESH) {
+        printf("Walk time greater than threshold of %lu us\n", FAIL_THRESH);
+        puts("[FAILURE]");
+        return 1;
+    }
+    else {
+        printf("Walk time less than threshold of %lu us\n", FAIL_THRESH);
+        puts("[SUCCESS]");
+    }
+
+    return 0;
+}
diff --git a/tests/cb_mux_bench/tests/01-run.py b/tests/cb_mux_bench/tests/01-run.py
new file mode 100755
index 0000000000..23896a9dfe
--- /dev/null
+++ b/tests/cb_mux_bench/tests/01-run.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+# Copyright (C) 2018 Acutam Automation, LLC
+#
+# This file is subject to the terms and conditions of the GNU Lesser
+# General Public License v2.1. See the file LICENSE in the top level
+# directory for more details.
+
+import os
+import sys
+
+
+def testfunc(child):
+    child.expect_exact("cb_mux benchmark application")
+    child.expect(u"Populating cb_mux list with \d+ items")
+    child.expect_exact("Finding the last list entry")
+    child.expect(u"List walk time: \d+ us")
+    child.expect(u"Walk time less than threshold of \d+ us")
+    child.expect_exact("[SUCCESS]")
+
+
+if __name__ == "__main__":
+    sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
+    from testrunner import run
+    sys.exit(run(testfunc))
-- 
GitLab