2 * Copyright (C) 2011-2014 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/>.
27 #include "benchmark.h"
29 int benchmark_must_finish = 0;
34 alarm_handler (LPVOID lpParameter)
36 HANDLE wtimer = *((HANDLE *) lpParameter);
37 WaitForSingleObject (wtimer, INFINITE);
38 benchmark_must_finish = 1;
43 alarm_handler (int signo)
45 benchmark_must_finish = 1;
50 value2human (unsigned long bytes, double secs, double *data, double *speed,
53 if (bytes > 1000 && bytes < 1000 * 1000)
55 *data = ((double) bytes) / 1000;
56 *speed = *data / secs;
57 strcpy (metric, "KB");
60 else if (bytes >= 1000 * 1000 && bytes < 1000 * 1000 * 1000)
62 *data = ((double) bytes) / (1000 * 1000);
63 *speed = *data / secs;
64 strcpy (metric, "MB");
67 else if (bytes >= 1000 * 1000 * 1000)
69 *data = ((double) bytes) / (1000 * 1000 * 1000);
70 *speed = *data / secs;
71 strcpy (metric, "GB");
76 *data = (double) bytes;
77 *speed = *data / secs;
78 strcpy (metric, "bytes");
84 start_benchmark (struct benchmark_st *st)
86 memset (st, 0, sizeof (*st));
88 st->old_handler = signal (SIGALRM, alarm_handler);
91 benchmark_must_finish = 0;
94 st->wtimer = CreateWaitableTimer (NULL, TRUE, NULL);
95 if (st->wtimer == NULL)
97 fprintf (stderr, "error: CreateWaitableTimer %u\n", GetLastError ());
100 st->wthread = CreateThread (NULL, 0, alarm_handler, &st->wtimer, 0, NULL);
101 if (st->wthread == NULL)
103 fprintf (stderr, "error: CreateThread %u\n", GetLastError ());
106 st->alarm_timeout.QuadPart = (5) * 10000000;
107 if (SetWaitableTimer (st->wtimer, &st->alarm_timeout, 0, NULL, NULL, FALSE)
110 fprintf (stderr, "error: SetWaitableTimer %u\n", GetLastError ());
119 /* returns the elapsed time */
121 stop_benchmark (struct benchmark_st *st, const char *metric)
125 struct timespec stop;
126 double dspeed, ddata;
130 if (st->wtimer != NULL)
131 CloseHandle (st->wtimer);
132 if (st->wthread != NULL)
133 CloseHandle (st->wthread);
135 signal (SIGALRM, st->old_handler);
140 lsecs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) -
141 (st->start.tv_sec * 1000 + st->start.tv_nsec / (1000 * 1000)));
146 { /* assume bytes/sec */
147 value2human (st->size, secs, &ddata, &dspeed, imetric);
148 printf (" Processed %.2f %s in %.2f secs: ", ddata, imetric, secs);
149 printf ("%.2f %s/sec\n", dspeed, imetric);
153 ddata = (double) st->size;
154 dspeed = ddata / secs;
155 printf (" Processed %.2f %s in %.2f secs: ", ddata, metric, secs);
156 printf ("%.2f %s/sec\n", dspeed, metric);