michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Program to test different ways to get the time; right now it is tuned michael@0: * only for solaris. michael@0: * solaris results (100000 iterations): michael@0: * time to get time with time(): 4.63 usec avg, 463 msec total michael@0: * time to get time with gethrtime(): 2.17 usec avg, 217 msec total michael@0: * time to get time with gettimeofday(): 1.25 usec avg, 125 msec total michael@0: * michael@0: * michael@0: */ michael@0: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: /* Used to get the command line option */ michael@0: #include "plgetopt.h" michael@0: michael@0: #include "nspr.h" michael@0: #include "prpriv.h" michael@0: #include "prinrval.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define DEFAULT_COUNT 100000 michael@0: PRInt32 count; michael@0: michael@0: time_t itime; michael@0: hrtime_t ihrtime; michael@0: michael@0: void michael@0: ftime_init() michael@0: { michael@0: itime = time(NULL); michael@0: ihrtime = gethrtime(); michael@0: } michael@0: michael@0: time_t michael@0: ftime() michael@0: { michael@0: hrtime_t now = gethrtime(); michael@0: michael@0: return itime + ((now - ihrtime) / 1000000000ll); michael@0: } michael@0: michael@0: static void timeTime(void) michael@0: { michael@0: PRInt32 index = count; michael@0: time_t rv; michael@0: michael@0: for (;index--;) michael@0: rv = time(NULL); michael@0: } michael@0: michael@0: static void timeGethrtime(void) michael@0: { michael@0: PRInt32 index = count; michael@0: time_t rv; michael@0: michael@0: for (;index--;) michael@0: rv = ftime(); michael@0: } michael@0: michael@0: static void timeGettimeofday(void) michael@0: { michael@0: PRInt32 index = count; michael@0: time_t rv; michael@0: struct timeval tp; michael@0: michael@0: for (;index--;) michael@0: rv = gettimeofday(&tp, NULL); michael@0: } michael@0: michael@0: static void timePRTime32(void) michael@0: { michael@0: PRInt32 index = count; michael@0: PRInt32 rv32; michael@0: PRTime q; michael@0: PRTime rv; michael@0: michael@0: LL_I2L(q, 1000000); michael@0: michael@0: for (;index--;) { michael@0: rv = PR_Now(); michael@0: LL_DIV(rv, rv, q); michael@0: LL_L2I(rv32, rv); michael@0: } michael@0: } michael@0: michael@0: static void timePRTime64(void) michael@0: { michael@0: PRInt32 index = count; michael@0: PRTime rv; michael@0: michael@0: for (;index--;) michael@0: rv = PR_Now(); michael@0: } michael@0: michael@0: /************************************************************************/ michael@0: michael@0: static void Measure(void (*func)(void), const char *msg) michael@0: { michael@0: PRIntervalTime start, stop; michael@0: double d; michael@0: PRInt32 tot; michael@0: michael@0: start = PR_IntervalNow(); michael@0: (*func)(); michael@0: stop = PR_IntervalNow(); michael@0: michael@0: d = (double)PR_IntervalToMicroseconds(stop - start); michael@0: tot = PR_IntervalToMilliseconds(stop-start); michael@0: michael@0: if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: /* The command line argument: -d is used to determine if the test is being run michael@0: in debug mode. The regress tool requires only one line output:PASS or FAIL. michael@0: All of the printfs associated with this test has been handled with a if (debug_mode) michael@0: test. michael@0: Usage: test_name -d michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug mode */ michael@0: debug_mode = 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: PR_STDIO_INIT(); michael@0: michael@0: if (argc > 1) { michael@0: count = atoi(argv[1]); michael@0: } else { michael@0: count = DEFAULT_COUNT; michael@0: } michael@0: michael@0: ftime_init(); michael@0: michael@0: Measure(timeTime, "time to get time with time()"); michael@0: Measure(timeGethrtime, "time to get time with gethrtime()"); michael@0: Measure(timeGettimeofday, "time to get time with gettimeofday()"); michael@0: Measure(timePRTime32, "time to get time with PR_Time() (32bit)"); michael@0: Measure(timePRTime64, "time to get time with PR_Time() (64bit)"); michael@0: michael@0: PR_Cleanup(); michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: