tests: Automatically time TAP tests
[lttng-tools.git] / tests / utils / tap / tap.c
index a7ec3db2c4ada41c9d23aedb951711818f6e3ca7..1ef417fed34a70b4897918815c2c783db4840fd5 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017 Jérémie Galarneau
  */
 
+#include "../utils.h"
+#include "common/compat/time.hpp"
 #include "tap.h"
 
 #include <assert.h>
@@ -14,6 +16,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 
 static int no_plan = 0;
 static int skip_all = 0;
@@ -25,6 +28,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 +146,7 @@ unsigned int _gen_result(
        }
 
        printf("\n");
+       _output_test_time();
 
        if (!ok) {
                if (getenv("HARNESS_ACTIVE") != NULL)
@@ -176,8 +182,18 @@ void _tap_init(void)
                   in the same place relative to stderr output as it does
                   with Test::Harness */
                setbuf(stdout, 0);
+
+               char *autotime_env = getenv("TAP_AUTOTIME");
+               if (autotime_env != NULL) {
+                       time_tests = atoi(autotime_env);
+                       if (time_tests != 0) {
+                               time_tests = 1;
+                       }
+               }
+
                run_once = 1;
        }
+       lttng_clock_gettime(CLOCK_MONOTONIC, &last_time);
 }
 
 /*
@@ -319,6 +335,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 +345,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.023802 seconds and 4 git commands to generate.