]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
gnutls-cli: benchmark-tls-kx can work with sub-ms accuracy
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Wed, 4 Dec 2019 12:58:21 +0000 (13:58 +0100)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Fri, 20 Dec 2019 19:52:40 +0000 (20:52 +0100)
This allows micro and nanoseconds to be reported if necessary,
and it changes reporting of sample variance to standard deviation
giving a possibly better overview as it is in the same units as
the average.

Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
src/Makefile.am
src/benchmark-tls.c
src/benchmark.h

index 9e166989167f7564aa049f9d1fb7d0f529b0b3b3..92762fa88a5506643c5ea4d75fb82859ba3b3395 100644 (file)
@@ -160,7 +160,7 @@ BENCHMARK_SRCS = benchmark-cipher.c benchmark.c benchmark.h benchmark-tls.c
 gnutls_cli_SOURCES = cli.c common.h common.c \
        socket.c socket.h ocsptool-common.c inline_cmds.h \
        $(BENCHMARK_SRCS)
-gnutls_cli_LDADD = ../lib/libgnutls.la
+gnutls_cli_LDADD = ../lib/libgnutls.la -lm
 if ENABLE_DANE
 gnutls_cli_LDADD += ../libdane/libgnutls-dane.la
 endif
index 48ca7e2f0ac36a9980648ef5ef5738cd52dd2688..a9c4ce02db7276f8ab75f630990a96470794e4b0 100644 (file)
@@ -349,7 +349,7 @@ static void test_ciphersuite(const char *cipher_prio, int size)
 }
 
 static
-double calc_avg(unsigned int *diffs, unsigned int diffs_size)
+double calc_avg(uint64_t *diffs, unsigned int diffs_size)
 {
        double avg = 0;
        unsigned int i;
@@ -363,7 +363,7 @@ double calc_avg(unsigned int *diffs, unsigned int diffs_size)
 }
 
 static
-double calc_sstdev(unsigned int *diffs, unsigned int diffs_size,
+double calc_svar(uint64_t *diffs, unsigned int diffs_size,
                   double avg)
 {
        double sum = 0, d;
@@ -381,7 +381,7 @@ double calc_sstdev(unsigned int *diffs, unsigned int diffs_size,
 }
 
 
-unsigned int total_diffs[32 * 1024];
+uint64_t total_diffs[32 * 1024];
 unsigned int total_diffs_size = 0;
 
 static void test_ciphersuite_kx(const char *cipher_prio, unsigned pk)
@@ -389,19 +389,18 @@ static void test_ciphersuite_kx(const char *cipher_prio, unsigned pk)
        /* Server stuff. */
        gnutls_anon_server_credentials_t s_anoncred;
        gnutls_session_t server;
-       int sret, cret;
+       int sret, cret, ret;
        const char *str;
        char *suite = NULL;
-       /* Client stuff. */
        gnutls_anon_client_credentials_t c_anoncred;
        gnutls_certificate_credentials_t c_certcred, s_certcred;
        gnutls_session_t client;
-       /* Need to enable anonymous KX specifically. */
-       int ret;
+       unsigned i;
        struct benchmark_st st;
        struct timespec tr_start, tr_stop;
-       double avg, sstddev;
+       double avg, svar;
        gnutls_priority_t priority_cache;
+       const char *scale;
 
        total_diffs_size = 0;
 
@@ -501,7 +500,7 @@ static void test_ciphersuite_kx(const char *cipher_prio, unsigned pk)
                gnutls_deinit(client);
                gnutls_deinit(server);
 
-               total_diffs[total_diffs_size++] = timespec_sub_ms(&tr_stop, &tr_start);
+               total_diffs[total_diffs_size++] = timespec_sub_ns(&tr_stop, &tr_start);
                if (total_diffs_size > sizeof(total_diffs)/sizeof(total_diffs[0]))
                        abort();
 
@@ -515,10 +514,25 @@ static void test_ciphersuite_kx(const char *cipher_prio, unsigned pk)
        gnutls_priority_deinit(priority_cache);
 
        avg = calc_avg(total_diffs, total_diffs_size);
-       sstddev = calc_sstdev(total_diffs, total_diffs_size, avg);
 
-       printf("%32s %.2f ms, sample variance: %.2f)\n",
-              "(avg. handshake time:", avg, sstddev);
+       if (avg < 1000) {
+               scale = "ns";
+       } else if (avg < 1000000) {
+               scale = "\u00B5s";
+               avg /= 1000;
+               for (i=0;i<total_diffs_size;i++)
+                       total_diffs[i] /= 1000;
+       } else {
+               scale = "ms";
+               avg /= 1000*1000;
+               for (i=0;i<total_diffs_size;i++)
+                       total_diffs[i] /= 1000*1000;
+       }
+
+       svar = calc_svar(total_diffs, total_diffs_size, avg);
+
+       printf("%32s %.2f %s, standard deviation: %.2f)\n",
+              "(avg. handshake time:", avg, scale, sqrt(svar));
 
        gnutls_anon_free_client_credentials(c_anoncred);
        gnutls_anon_free_server_credentials(s_anoncred);
index 2152e6edcfc82f569ed57739b2db4e8874a24925..a5e2aff1248151fa6f74a537ec0ef87ccd477a84 100644 (file)
@@ -71,4 +71,10 @@ timespec_sub_ms(struct timespec *a, struct timespec *b)
        return (a->tv_sec - b->tv_sec) * 1000 + (a->tv_nsec - b->tv_nsec) / (1000 * 1000);
 }
 
+inline static unsigned long
+timespec_sub_ns(struct timespec *a, struct timespec *b)
+{
+       return (a->tv_sec - b->tv_sec) * 1000000000 + (a->tv_nsec - b->tv_nsec);
+}
+
 #endif /* GNUTLS_SRC_BENCHMARK_H */