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/>.
27 #include "benchmark.h"
31 int benchmark_must_finish
= 0;
36 alarm_handler (LPVOID lpParameter
)
38 HANDLE wtimer
= *((HANDLE
*) lpParameter
);
39 WaitForSingleObject (wtimer
, INFINITE
);
40 benchmark_must_finish
= 1;
45 alarm_handler (int signo
)
47 benchmark_must_finish
= 1;
52 value2human (unsigned long bytes
, double time
, double *data
, double *speed
,
55 if (bytes
> 1000 && bytes
< 1000 * 1000)
57 *data
= ((double) bytes
) / 1000;
58 *speed
= *data
/ time
;
59 strcpy (metric
, "KB");
62 else if (bytes
>= 1000 * 1000 && bytes
< 1000 * 1000 * 1000)
64 *data
= ((double) bytes
) / (1000 * 1000);
65 *speed
= *data
/ time
;
66 strcpy (metric
, "MB");
69 else if (bytes
>= 1000 * 1000 * 1000)
71 *data
= ((double) bytes
) / (1000 * 1000 * 1000);
72 *speed
= *data
/ time
;
73 strcpy (metric
, "GB");
78 *data
= (double) bytes
;
79 *speed
= *data
/ time
;
80 strcpy (metric
, "bytes");
85 void start_benchmark(struct benchmark_st
* st
)
87 memset(st
, 0, sizeof(*st
));
89 st
->old_handler
= signal (SIGALRM
, alarm_handler
);
92 benchmark_must_finish
= 0;
95 st
->wtimer
= CreateWaitableTimer (NULL
, TRUE
, NULL
);
96 if (st
->wtimer
== NULL
)
98 fprintf (stderr
, "error: CreateWaitableTimer %u\n", GetLastError ());
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 ());
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 ());
119 /* returns the elapsed time */
120 double stop_benchmark(struct benchmark_st
* st
, const char* metric
)
124 struct timespec stop
;
125 double dspeed
, ddata
;
129 if (st
->wtimer
!= NULL
)
130 CloseHandle (st
->wtimer
);
131 if (st
->wthread
!= NULL
)
132 CloseHandle (st
->wthread
);
134 signal(SIGALRM
, st
->old_handler
);
139 lsecs
= (stop
.tv_sec
* 1000 + stop
.tv_nsec
/ (1000 * 1000) -
140 (st
->start
.tv_sec
* 1000 + st
->start
.tv_nsec
/ (1000 * 1000)));
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
);
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
);