Tests: namespace TAP_AUTOTIME under LTTNG_TESTS
[lttng-tools.git] / tests / utils / tap / tap.c
index a7ec3db2c4ada41c9d23aedb951711818f6e3ca7..687ea22f28a9adf7f787dc473b5313d1da129cf3 100644 (file)
@@ -5,15 +5,19 @@
  * Copyright (C) 2017 Jérémie Galarneau
  */
 
+#include "../utils.h"
+#include "common/compat/time.hpp"
 #include "tap.h"
 
 #include <assert.h>
 #include <ctype.h>
+#include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 
 static int no_plan = 0;
 static int skip_all = 0;
@@ -25,6 +29,8 @@ static char *todo_msg = NULL;
 static const char *todo_msg_fixed = "libtap malloc issue";
 static int todo = 0;
 static int test_died = 0;
+static int time_tests = 1;
+struct timespec last_time;
 
 /* Encapsulate the pthread code in a conditional.  In the absence of
    libpthread the code does nothing */
@@ -141,6 +147,7 @@ unsigned int _gen_result(
        }
 
        printf("\n");
+       _output_test_time();
 
        if (!ok) {
                if (getenv("HARNESS_ACTIVE") != NULL)
@@ -176,8 +183,29 @@ void _tap_init(void)
                   in the same place relative to stderr output as it does
                   with Test::Harness */
                setbuf(stdout, 0);
+
+               /*
+                * Check if the LTTNG_TESTS_TAP_AUTOTIME environment variable
+                * is set and contains at least one byte.
+                */
+               const char *autotime_env = getenv("LTTNG_TESTS_TAP_AUTOTIME");
+               if (autotime_env != NULL && strnlen(autotime_env, 1)) {
+                       int tap_autotime;
+
+                       /*
+                        * Check if LTTNG_TESTS_TAP_AUTOTIME is '0', also check
+                        * errno because strtol() can return '0' on error.
+                        */
+                       errno = 0;
+                       tap_autotime = strtol(autotime_env, NULL, 10);
+                       if (tap_autotime == 0 && errno == 0) {
+                               time_tests = 0;
+                       }
+               }
+
                run_once = 1;
        }
+       lttng_clock_gettime(CLOCK_MONOTONIC, &last_time);
 }
 
 /*
@@ -319,6 +347,7 @@ int skip(unsigned int n, const char *fmt, ...)
                printf("ok %d # skip %s\n",
                       test_count,
                       skip_msg != NULL ? skip_msg : "libtap():malloc() failed");
+               _output_test_time();
        }
 
        free(skip_msg);
@@ -328,6 +357,20 @@ int skip(unsigned int n, const char *fmt, ...)
        return 1;
 }
 
+void _output_test_time(void)
+{
+       struct timespec new_time;
+       int64_t time_ns;
+       if (time_tests) {
+               lttng_clock_gettime(CLOCK_MONOTONIC, &new_time);
+               time_ns = elapsed_time_ns(&last_time, &new_time);
+               printf("  ---\n    duration_ms: %ld.%ld\n  ...\n",
+                      time_ns / 1000000,
+                      time_ns % 1000000);
+       }
+       lttng_clock_gettime(CLOCK_MONOTONIC, &last_time);
+}
+
 void todo_start(const char *fmt, ...)
 {
        va_list ap;
This page took 0.024453 seconds and 4 git commands to generate.