Drop special handling for Compaq C++
[xapian.git] / xapian-core / tests / harness / cputimer.cc
bloba0c805e8913cc9fc2aa1bfdfe1ddfd733abd1dff
1 /** @file cputimer.cc
2 * @brief Measure CPU time.
3 */
4 /* Copyright (C) 2009,2015,2018 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 "testsuite.h"
27 #ifdef HAVE_GETRUSAGE
28 # include <sys/time.h>
29 # include <sys/resource.h>
30 #elif defined HAVE_TIMES
31 # include <sys/times.h>
32 # ifdef HAVE_SYSCONF
33 # include "safeunistd.h"
34 # endif
35 #elif defined HAVE_FTIME
36 # include <sys/timeb.h>
37 #else
38 # include <ctime>
39 #endif
41 #include <cerrno>
42 #include <cstdlib>
43 #include <cstring>
44 #include <string>
46 using namespace std;
48 double
49 CPUTimer::get_current_cputime() const
51 #ifdef XAPIAN_DEBUG_LOG
52 SKIP_TEST("Skipping timed test because configured with --enable-log");
53 #else
54 static bool skip = (getenv("AUTOMATED_TESTING") != NULL);
55 if (skip) {
56 SKIP_TEST("Skipping timed test because $AUTOMATED_TESTING is set");
59 double t = 0;
60 #ifdef HAVE_GETRUSAGE
61 struct rusage r;
62 if (getrusage(RUSAGE_SELF, &r) == -1) {
63 FAIL_TEST("Couldn't measure CPU for self: " << strerror(errno));
66 t = r.ru_utime.tv_sec + r.ru_stime.tv_sec;
67 t += (r.ru_utime.tv_usec + r.ru_stime.tv_usec) * 0.000001;
68 #elif defined HAVE_TIMES
69 struct tms b;
70 if (times(&b) == clock_t(-1)) {
71 FAIL_TEST("Couldn't measure CPU: " << strerror(errno));
73 t = (double)(b.tms_utime + b.tms_stime);
74 # ifdef HAVE_SYSCONF
75 t /= sysconf(_SC_CLK_TCK);
76 # else
77 t /= CLK_TCK;
78 # endif
79 #else
80 // FIXME: Fallback to just using wallclock time, which is probably only
81 // going to be used on Microsoft Windows, where nobody has implemented
82 // the code required to get the CPU time used by a process.
83 # ifdef HAVE_FTIME
84 struct timeb tb;
85 # ifdef FTIME_RETURNS_VOID
86 ftime(&tb);
87 t = tb.time + (tb.millitm * 0.001);
88 # else
89 if (ftime(&tb) == -1) {
90 t = time(NULL);
91 } else {
92 t = tb.time + (tb.millitm * 0.001);
94 # endif
95 # else
96 t = time(NULL);
97 # endif
98 #endif
100 return t;
101 #endif