Updated gnulib and added hash-pjw-bare
[gnutls.git] / src / benchmark.c
blobea19764160b7e6cca6971fd64f388637728c5d78
1 /*
2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
4 * This file is part of GnuTLS.
6 * GnuTLS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuTLS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <signal.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include "benchmark.h"
29 #define BSECS 5
31 int benchmark_must_finish = 0;
33 #if defined(_WIN32)
34 #include <windows.h>
35 DWORD WINAPI
36 alarm_handler (LPVOID lpParameter)
38 HANDLE wtimer = *((HANDLE *) lpParameter);
39 WaitForSingleObject (wtimer, INFINITE);
40 benchmark_must_finish = 1;
41 return 0;
43 #else
44 static void
45 alarm_handler (int signo)
47 benchmark_must_finish = 1;
49 #endif
51 static void
52 value2human (unsigned long bytes, double time, double *data, double *speed,
53 char *metric)
55 if (bytes > 1000 && bytes < 1000 * 1000)
57 *data = ((double) bytes) / 1000;
58 *speed = *data / time;
59 strcpy (metric, "KB");
60 return;
62 else if (bytes >= 1000 * 1000 && bytes < 1000 * 1000 * 1000)
64 *data = ((double) bytes) / (1000 * 1000);
65 *speed = *data / time;
66 strcpy (metric, "MB");
67 return;
69 else if (bytes >= 1000 * 1000 * 1000)
71 *data = ((double) bytes) / (1000 * 1000 * 1000);
72 *speed = *data / time;
73 strcpy (metric, "GB");
74 return;
76 else
78 *data = (double) bytes;
79 *speed = *data / time;
80 strcpy (metric, "bytes");
81 return;
85 void start_benchmark(struct benchmark_st * st)
87 memset(st, 0, sizeof(*st));
88 #ifndef _WIN32
89 st->old_handler = signal (SIGALRM, alarm_handler);
90 #endif
91 gettime (&st->start);
92 benchmark_must_finish = 0;
94 #if defined(_WIN32)
95 st->wtimer = CreateWaitableTimer (NULL, TRUE, NULL);
96 if (st->wtimer == NULL)
98 fprintf (stderr, "error: CreateWaitableTimer %u\n", GetLastError ());
99 exit(1);
101 st->wthread = CreateThread (NULL, 0, alarm_handler, &st->wtimer, 0, NULL);
102 if (st->wthread == NULL)
104 fprintf (stderr, "error: CreateThread %u\n", GetLastError ());
105 exit(1);
107 st->alarm_timeout.QuadPart = (BSECS) * 10000000;
108 if (SetWaitableTimer (st->wtimer, &st->alarm_timeout, 0, NULL, NULL, FALSE) == 0)
110 fprintf (stderr, "error: SetWaitableTimer %u\n", GetLastError ());
111 exit(1);
113 #else
114 alarm (BSECS);
115 #endif
119 /* returns the elapsed time */
120 double stop_benchmark(struct benchmark_st * st, const char* metric)
122 double secs;
123 unsigned long lsecs;
124 struct timespec stop;
125 double dspeed, ddata;
126 char imetric[16];
128 #if defined(_WIN32)
129 if (st->wtimer != NULL)
130 CloseHandle (st->wtimer);
131 if (st->wthread != NULL)
132 CloseHandle (st->wthread);
133 #else
134 signal(SIGALRM, st->old_handler);
135 #endif
137 gettime (&stop);
139 lsecs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) -
140 (st->start.tv_sec * 1000 + st->start.tv_nsec / (1000 * 1000)));
141 secs = lsecs;
142 secs /= 1000;
144 if (metric == NULL)
145 { /* assume bytes/sec */
146 value2human (st->size, secs, &ddata, &dspeed, imetric);
147 printf (" Processed %.2f %s in %.2f secs: ", ddata, imetric, secs);
148 printf ("%.2f %s/sec\n", dspeed, imetric);
150 else
152 ddata = (double) st->size;
153 dspeed = ddata / secs;
154 printf (" Processed %.2f %s in %.2f secs: ", ddata, metric, secs);
155 printf ("%.2f %s/sec\n", dspeed, metric);
158 return secs;