generic test
authorLaurent Mazet <mazet@softndesign.org>
Fri, 26 Sep 2025 21:15:29 +0000 (23:15 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Fri, 26 Sep 2025 21:15:29 +0000 (23:15 +0200)
mstime.c
mstime.h
mustime.c [new file with mode: 0644]
mustime.h [new file with mode: 0644]
mutex.c
test.c [new file with mode: 0644]
test.h [new file with mode: 0644]

index e9065b75eb22bf4e9b20835547ce9d38a842d778..d92d0ebe4f58d0014b022d217bd157f35c86823e 100644 (file)
--- a/mstime.c
+++ b/mstime.c
@@ -2,14 +2,14 @@
 
 #include "mstime.h"
 
-mstime_t timespec_as_microseconds (struct timespec ts)
+mstime_t timespec_as_milliseconds (struct timespec ts)
 {
-    return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
+    return ts.tv_sec * 1000 + ts.tv_nsec / 10000000 ;
 }
 
 mstime_t sys_timestamp (void)
 {
     struct timespec ts;
     clock_gettime (CLOCK_REALTIME, &ts);
-    return timespec_as_microseconds (ts);
+    return timespec_as_milliseconds (ts);
 }
index b007ffd1304597cb713048395700eb11f53efcb0..4c46aa30b9a9154aa3bd4e3bcab030550bcd4be6 100644 (file)
--- a/mstime.h
+++ b/mstime.h
@@ -1,4 +1,4 @@
-/* microsecond time module */
+/* millisecond time module */
 
 #ifndef __MSTIME_H__
 #define __MSTIME_H__
diff --git a/mustime.c b/mustime.c
new file mode 100644 (file)
index 0000000..0983278
--- /dev/null
+++ b/mustime.c
@@ -0,0 +1,15 @@
+#include <time.h>
+
+#include "mustime.h"
+
+mustime_t timespec_as_microseconds (struct timespec ts)
+{
+    return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
+}
+
+mustime_t sys_timestamp (void)
+{
+    struct timespec ts;
+    clock_gettime (CLOCK_REALTIME, &ts);
+    return timespec_as_microseconds (ts);
+}
diff --git a/mustime.h b/mustime.h
new file mode 100644 (file)
index 0000000..070790d
--- /dev/null
+++ b/mustime.h
@@ -0,0 +1,12 @@
+/* microsecond time module */
+
+#ifndef __MUSTIME_H__
+#define __MUSTIME_H__
+
+#include "inttypes.h"
+
+typedef int64_t mustime_t;
+
+mustime_t sys_timestamp (void);
+
+#endif /* __MUSTIME_H__ */
diff --git a/mutex.c b/mutex.c
index eb60dcb51cde9d883c94e2e6a5b1bc53608a8271..8c3db8d406e15f49ba96e69105236b3752962376 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -1,28 +1,19 @@
 /* depend: */
 /* cflags: */
-/* linker: nstime.o stat.o -lpthread -lm */
+/* linker: nstime.o test.o stat.o -lpthread -lm */
 
-#include <assert.h>
-#include <inttypes.h>
 #include <pthread.h>
 #include <stdio.h>
-#include <stdlib.h>
 
 #include "nstime.h"
 #include "stat.h"
-
-/* static variables */
-
-char *progname = NULL;
-char *version = "1.1";
-
-int nb_measurements = 1000;
-int do_stat = 0;
-int hist_bin = 10;
-char *output = NULL;
+#include "test.h"
 
 /* global variables */
 
+int64_t *deltas = NULL;
+int nb_measurements = 0;
+
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 int shared_flag = 0;
 nstime_t timestamp1, timestamp2;
@@ -41,8 +32,6 @@ void *mutex_giver_task (__attribute__((unused)) void *arg)
 
 void *mutex_taker_task (__attribute__((unused)) void *arg)
 {
-    nstime_t *deltas = (nstime_t *) calloc (nb_measurements, sizeof (nstime_t));
-    assert (deltas);
 
     while (try < nb_measurements) {
         pthread_mutex_lock (&mutex);
@@ -54,101 +43,16 @@ void *mutex_taker_task (__attribute__((unused)) void *arg)
         pthread_mutex_unlock(&mutex);
     }
 
-    if (output) {
-        FILE *fd = fopen (output, "w");
-        assert (fd);
-        for (int i = 0; i < nb_measurements; i++) {
-            fprintf (fd, "%ld\n", deltas[i]);
-        }
-        fclose (fd);
-    }
-
-    if (do_stat) {
-        printf ("Results are in nanosecond\n");
-        compute_statistics (deltas, nb_measurements, hist_bin);
-    }
-
-    free (deltas);
-
     pthread_exit(NULL);
 }
 
-/* usage function */
-
-int usage (int ret)
-{
-    FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s [-b int] [-h] [-n int] [-o file] [-s]\n", progname);
-    fprintf (fd, " -b: nb bins for histogram (%d)\n", hist_bin);
-    fprintf (fd, " -h: help message\n");
-    fprintf (fd, " -n: nb measurements (%d)\n", nb_measurements);
-    fprintf (fd, " -o: output raw data (%s)\n", (output) ? output : "none");
-    fprintf (fd, " -s: display statistics (%s)\n", (do_stat) ? "yes" : "no");
-    fprintf (fd, "%s version %s\n", progname, version);
-
-    return ret;
-}
-
-/* main function */
-
-int main (int argc, char *argv[])
+sec_t test (int64_t *buffer, int nb)
 {
 
-    /* get basename */
+    /* set global variables */
 
-    char *pt = progname = argv[0];
-    while (*pt) {
-        if ((*pt == '/') || (*pt == '\\')) {
-           progname = pt + 1;
-        }
-        pt++;
-    }
-
-    /* process arguments */
-
-    while (argc-- > 1) {
-        char *arg = *(++argv);
-        if (arg[0] != '-') {
-            fprintf (stderr, "%s: invalid option -- %s\n", progname, arg);
-            return usage (1);
-        }
-        char c = arg[1];
-        switch (c) {
-        case 'b':
-            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
-            if (arg == NULL) {
-                fprintf (stderr, "%s: no number of bins specified\n", progname);
-                return usage (1);
-            }
-            hist_bin = atoi (arg);
-            break;
-        case 'n':
-            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
-            if (arg == NULL) {
-                fprintf (stderr, "%s: no number of measurements specified\n", progname);
-                return usage (1);
-            }
-            nb_measurements = atoi (arg);
-            break;
-        case 'o':
-            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
-            if (arg == NULL) {
-                fprintf (stderr, "%s: no output file specified\n", progname);
-                return usage (1);
-            }
-            output = arg;
-            break;
-        case 's':
-            do_stat = 1;
-            break;
-        case 'v':
-            printf ("version: %s\n", version);
-            break;
-        case 'h':
-        default:
-            return usage (c != 'h');
-        }
-    }
+    deltas = buffer;
+    nb_measurements = nb;
 
     /* main process */
 
@@ -179,5 +83,5 @@ int main (int argc, char *argv[])
 
     pthread_mutex_destroy(&mutex);
 
-    return 0;
+    return nsec_e;
 }
diff --git a/test.c b/test.c
new file mode 100644 (file)
index 0000000..b67524f
--- /dev/null
+++ b/test.c
@@ -0,0 +1,130 @@
+#include <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "stat.h"
+#include "test.h"
+
+/* static variables */
+
+char *progname = NULL;
+char *version = "1.1";
+
+int nb = 1000;
+int do_stat = 0;
+int hist_bin = 10;
+char *output = NULL;
+
+/* usage function */
+
+int usage (int ret)
+{
+    FILE *fd = ret ? stderr : stdout;
+    fprintf (fd, "usage: %s [-b int] [-h] [-n int] [-o file] [-s]\n", progname);
+    fprintf (fd, " -b: nb bins for histogram (%d)\n", hist_bin);
+    fprintf (fd, " -h: help message\n");
+    fprintf (fd, " -n: nb measurements (%d)\n", nb);
+    fprintf (fd, " -o: output raw data (%s)\n", (output) ? output : "none");
+    fprintf (fd, " -s: display statistics (%s)\n", (do_stat) ? "yes" : "no");
+    fprintf (fd, "%s version %s\n", progname, version);
+
+    return ret;
+}
+
+/* main function */
+
+int main (int argc, char *argv[])
+{
+
+    /* get basename */
+
+    char *pt = progname = argv[0];
+    while (*pt) {
+        if ((*pt == '/') || (*pt == '\\')) {
+           progname = pt + 1;
+        }
+        pt++;
+    }
+
+    /* process arguments */
+
+    while (argc-- > 1) {
+        char *arg = *(++argv);
+        if (arg[0] != '-') {
+            fprintf (stderr, "%s: invalid option -- %s\n", progname, arg);
+            return usage (1);
+        }
+        char c = arg[1];
+        switch (c) {
+        case 'b':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                fprintf (stderr, "%s: no number of bins specified\n", progname);
+                return usage (1);
+            }
+            hist_bin = atoi (arg);
+            break;
+        case 'n':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                fprintf (stderr, "%s: no number of measurements specified\n", progname);
+                return usage (1);
+            }
+            nb = atoi (arg);
+            break;
+        case 'o':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                fprintf (stderr, "%s: no output file specified\n", progname);
+                return usage (1);
+            }
+            output = arg;
+            break;
+        case 's':
+            do_stat = 1;
+            break;
+        case 'v':
+            printf ("version: %s\n", version);
+            break;
+        case 'h':
+        default:
+            return usage (c != 'h');
+        }
+    }
+
+    /* main process */
+
+    int64_t *buffer = (int64_t *) calloc (nb, sizeof (int64_t));
+    assert (buffer);
+
+    sec_t sec_unit = test (buffer, nb);
+
+    if (output) {
+        FILE *fd = fopen (output, "w");
+        assert (fd);
+        for (int i = 0; i < nb; i++) {
+            fprintf (fd, "%ld\n", buffer[i]);
+        }
+        fclose (fd);
+    }
+
+    if (do_stat) {
+        char unit[32] = { 0 };
+        switch (sec_unit) {
+        case sec_e : strcpy (unit, "second"); break;
+        case msec_e : strcpy (unit, "millisecond"); break;
+        case musec_e : strcpy (unit, "microsecond"); break;
+        case nsec_e : strcpy (unit, "nanosecond"); break;
+        default: strcpy (unit, "???"); break;
+        }
+
+        printf ("Results are in %s\n", unit);
+        compute_statistics (buffer, nb, hist_bin);
+    }
+
+    free (buffer);
+
+    return 0;
+}
diff --git a/test.h b/test.h
new file mode 100644 (file)
index 0000000..6fb35af
--- /dev/null
+++ b/test.h
@@ -0,0 +1,17 @@
+/* test module */
+
+#ifndef __TEST_H__
+#define __TEST_H__
+
+#include <inttypes.h>
+
+typedef enum {
+    sec_e = 0,
+    msec_e,
+    musec_e,
+    nsec_e
+} sec_t;
+
+sec_t test (int64_t *buffer, int nb);
+
+#endif /* __TEST_H__ */