[ci] Update macos jobs
[xapian.git] / xapian-letor / tests / harness / cputimer.cc
blob6997148480554b5dbea38fe6cd1b4aa47de85dd7
1 /** @file
2 * @brief Measure CPU time.
3 */
4 /* Copyright (C) 2009,2015,2018,2020 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
23 #include "cputimer.h"
25 #include "errno_to_string.h"
26 #include "testsuite.h"
28 #ifdef HAVE_GETRUSAGE
29 # include <sys/time.h>
30 # include <sys/resource.h>
31 #elif defined HAVE_TIMES
32 # include <sys/times.h>
33 # ifdef HAVE_SYSCONF
34 # include "safeunistd.h"
35 # endif
36 #else
37 # include "realtime.h"
38 #endif
40 #include <cerrno>
41 #include <cstdlib>
42 #include <cstring>
43 #include <string>
45 using namespace std;
47 double
48 CPUTimer::get_current_cputime()
50 #ifdef XAPIAN_DEBUG_LOG
51 SKIP_TEST("Skipping timed test because configured with --enable-log");
52 #else
53 static bool skip = (getenv("AUTOMATED_TESTING") != NULL);
54 if (skip) {
55 SKIP_TEST("Skipping timed test because $AUTOMATED_TESTING is set");
58 double t = 0;
59 #ifdef HAVE_GETRUSAGE
60 struct rusage r;
61 if (getrusage(RUSAGE_SELF, &r) == -1) {
62 FAIL_TEST("Couldn't measure CPU for self: " << errno_to_string(errno));
65 t = r.ru_utime.tv_sec + r.ru_stime.tv_sec;
66 t += (r.ru_utime.tv_usec + r.ru_stime.tv_usec) * 0.000001;
67 #elif defined HAVE_TIMES
68 struct tms b;
69 if (times(&b) == clock_t(-1)) {
70 FAIL_TEST("Couldn't measure CPU: " << errno_to_string(errno));
72 t = (double)(b.tms_utime + b.tms_stime);
73 # ifdef HAVE_SYSCONF
74 t /= sysconf(_SC_CLK_TCK);
75 # else
76 t /= CLK_TCK;
77 # endif
78 #else
79 // FIXME: Fallback to just using wallclock time, which is probably only
80 // going to be used on Microsoft Windows, where nobody has implemented
81 // the code required to get the CPU time used by a process.
82 t = RealTime::now();
83 #endif
85 return t;
86 #endif