[Libguestfs] [PATCH libnbd v2 2/8] common/utils: Do not run benchmarks by default

Nir Soffer
Sun, 31 Oct 2021 15:38:51 -0000

The new benchmarks are very quick on modern machine, but in overload CI,
under valgrind, or running in qemu emulation they can be too slow to
run by default.

Run the benchmarks only when LIBNBD_BENCH environment variable is
defined.

To run all benchmarks in the project:

    make bench

To run single test program with benchmarks:

    $ LIBNBD_BENCH=1 ./test-vector
    bench_reserve: 1000000 appends in 0.004282 s
    bench_append: 1000000 appends in 0.015586 s

CI jobs that run on reliable environment (x86_64?) can add the `bench`
target to the job, to ensure that the benchmarks do not rot.

Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
 Makefile.am                |  5 +++++
 README                     |  4 ++++
 common/utils/Makefile.am   |  3 +++
 common/utils/test-vector.c | 11 +++++++----
 4 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 0f0427a..87ceec0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -81,6 +81,11 @@ check-valgrind: all
 	    $(MAKE) -C $$d check-valgrind || exit 1; \
 	done

+bench: all
+	@for d in common/utils; do \
+	    $(MAKE) -C $$d bench || exit 1; \
+	done
+
 check-root:
 	@for d in copy; do \
 	    $(MAKE) -C $$d check-root || exit 1; \
diff --git a/README b/README
index a0f2456..6502e7f 100644
--- a/README
+++ b/README
@@ -59,6 +59,10 @@ To run the tests under valgrind:

   make check-valgrind

+To run benchmarks:
+
+  make bench
+
 Some tests require root permissions (and are therefore risky).  If you
 want to run these tests, do:

diff --git a/common/utils/Makefile.am b/common/utils/Makefile.am
index 4730cea..0a8629a 100644
--- a/common/utils/Makefile.am
+++ b/common/utils/Makefile.am
@@ -62,3 +62,6 @@ test_human_size_CFLAGS = $(WARNINGS_CFLAGS)
 test_vector_SOURCES = test-vector.c vector.c vector.h bench.h
 test_vector_CPPFLAGS = -I$(srcdir)
 test_vector_CFLAGS = $(WARNINGS_CFLAGS)
+
+bench: test-vector
+	LIBNBD_BENCH=1 ./test-vector
diff --git a/common/utils/test-vector.c b/common/utils/test-vector.c
index 28e882f..d55de07 100644
--- a/common/utils/test-vector.c
+++ b/common/utils/test-vector.c
@@ -163,9 +163,12 @@ bench_append (void)
 int
 main (int argc, char *argv[])
 {
-  test_int64_vector ();
-  test_string_vector ();
-  bench_reserve ();
-  bench_append ();
+  if (getenv("LIBNBD_BENCH")) {
+    bench_reserve ();
+    bench_append ();
+  } else {
+    test_int64_vector ();
+    test_string_vector ();
+  }
   return 0;
 }
--
2.31.1