2 * Copyright (C) 2011 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/>.
26 #include "benchmark.h"
28 int benchmark_must_finish
= 0;
31 alarm_handler (int signo
)
33 benchmark_must_finish
= 1;
36 int start_benchmark(struct benchmark_st
* st
)
39 struct itimerval timer
;
41 memset(st
, 0, sizeof(*st
));
43 st
->old_handler
= signal (SIGPROF
, alarm_handler
);
45 ret
= gettimeofday (&st
->start
, NULL
);
47 perror("gettimeofday");
51 benchmark_must_finish
= 0;
53 memset(&timer
, 0, sizeof(timer
));
54 timer
.it_value
.tv_sec
= 0;
55 timer
.it_value
.tv_usec
= 100*1000;
57 ret
= setitimer(ITIMER_PROF
, &timer
, NULL
);
66 /* Returns -1 on error or 0 on success.
67 * elapsed: the elapsed time in milliseconds
69 int stop_benchmark(struct benchmark_st
* st
, unsigned long * elapsed
)
75 signal(SIGPROF
, st
->old_handler
);
77 ret
= gettimeofday (&stop
, NULL
);
81 msecs
= (stop
.tv_sec
* 1000 + stop
.tv_usec
/ 1000 -
82 (st
->start
.tv_sec
* 1000 + st
->start
.tv_usec
/ (1000)));
84 if (elapsed
) *elapsed
= msecs
;