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"
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 time
, double *data
, double *speed
,
53 if (bytes
> 1000 && bytes
< 1000 * 1000)
55 *data
= ((double) bytes
) / 1000;
56 *speed
= *data
/ time
;
57 strcpy (metric
, "KB");
60 else if (bytes
>= 1000 * 1000 && bytes
< 1000 * 1000 * 1000)
62 *data
= ((double) bytes
) / (1000 * 1000);
63 *speed
= *data
/ time
;
64 strcpy (metric
, "MB");
67 else if (bytes
>= 1000 * 1000 * 1000)
69 *data
= ((double) bytes
) / (1000 * 1000 * 1000);
70 *speed
= *data
/ time
;
71 strcpy (metric
, "GB");
76 *data
= (double) bytes
;
77 *speed
= *data
/ time
;
78 strcpy (metric
, "bytes");
83 void start_benchmark(struct benchmark_st
* st
)
85 memset(st
, 0, sizeof(*st
));
87 st
->old_handler
= signal (SIGALRM
, alarm_handler
);
90 benchmark_must_finish
= 0;
93 st
->wtimer
= CreateWaitableTimer (NULL
, TRUE
, NULL
);
94 if (st
->wtimer
== NULL
)
96 fprintf (stderr
, "error: CreateWaitableTimer %u\n", GetLastError ());
99 st
->wthread
= CreateThread (NULL
, 0, alarm_handler
, &st
->wtimer
, 0, NULL
);
100 if (st
->wthread
== NULL
)
102 fprintf (stderr
, "error: CreateThread %u\n", GetLastError ());
105 st
->alarm_timeout
.QuadPart
= (2) * 10000000;
106 if (SetWaitableTimer (st
->wtimer
, &st
->alarm_timeout
, 0, NULL
, NULL
, FALSE
) == 0)
108 fprintf (stderr
, "error: SetWaitableTimer %u\n", GetLastError ());
117 /* returns the elapsed time */
118 double stop_benchmark(struct benchmark_st
* st
, const char* metric
)
122 struct timespec stop
;
123 double dspeed
, ddata
;
127 if (st
->wtimer
!= NULL
)
128 CloseHandle (st
->wtimer
);
129 if (st
->wthread
!= NULL
)
130 CloseHandle (st
->wthread
);
132 signal(SIGALRM
, st
->old_handler
);
137 lsecs
= (stop
.tv_sec
* 1000 + stop
.tv_nsec
/ (1000 * 1000) -
138 (st
->start
.tv_sec
* 1000 + st
->start
.tv_nsec
/ (1000 * 1000)));
143 { /* assume bytes/sec */
144 value2human (st
->size
, secs
, &ddata
, &dspeed
, imetric
);
145 printf ("Processed %.2f %s in %.2f secs: ", ddata
, imetric
, secs
);
146 printf ("%.2f %s/sec\n", dspeed
, imetric
);
150 ddata
= (double) st
->size
;
151 dspeed
= ddata
/ secs
;
152 printf ("Processed %.2f %s in %.2f secs: ", ddata
, metric
, secs
);
153 printf ("%.2f %s/sec\n", dspeed
, metric
);