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