nsprpub/pr/tests/time.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * Program to test different ways to get the time; right now it is tuned
michael@0 8 * only for solaris.
michael@0 9 * solaris results (100000 iterations):
michael@0 10 * time to get time with time(): 4.63 usec avg, 463 msec total
michael@0 11 * time to get time with gethrtime(): 2.17 usec avg, 217 msec total
michael@0 12 * time to get time with gettimeofday(): 1.25 usec avg, 125 msec total
michael@0 13 *
michael@0 14 *
michael@0 15 */
michael@0 16 /***********************************************************************
michael@0 17 ** Includes
michael@0 18 ***********************************************************************/
michael@0 19 /* Used to get the command line option */
michael@0 20 #include "plgetopt.h"
michael@0 21
michael@0 22 #include "nspr.h"
michael@0 23 #include "prpriv.h"
michael@0 24 #include "prinrval.h"
michael@0 25
michael@0 26 #include <stdio.h>
michael@0 27 #include <stdlib.h>
michael@0 28 #include <string.h>
michael@0 29 #include <sys/time.h>
michael@0 30
michael@0 31 #define DEFAULT_COUNT 100000
michael@0 32 PRInt32 count;
michael@0 33
michael@0 34 time_t itime;
michael@0 35 hrtime_t ihrtime;
michael@0 36
michael@0 37 void
michael@0 38 ftime_init()
michael@0 39 {
michael@0 40 itime = time(NULL);
michael@0 41 ihrtime = gethrtime();
michael@0 42 }
michael@0 43
michael@0 44 time_t
michael@0 45 ftime()
michael@0 46 {
michael@0 47 hrtime_t now = gethrtime();
michael@0 48
michael@0 49 return itime + ((now - ihrtime) / 1000000000ll);
michael@0 50 }
michael@0 51
michael@0 52 static void timeTime(void)
michael@0 53 {
michael@0 54 PRInt32 index = count;
michael@0 55 time_t rv;
michael@0 56
michael@0 57 for (;index--;)
michael@0 58 rv = time(NULL);
michael@0 59 }
michael@0 60
michael@0 61 static void timeGethrtime(void)
michael@0 62 {
michael@0 63 PRInt32 index = count;
michael@0 64 time_t rv;
michael@0 65
michael@0 66 for (;index--;)
michael@0 67 rv = ftime();
michael@0 68 }
michael@0 69
michael@0 70 static void timeGettimeofday(void)
michael@0 71 {
michael@0 72 PRInt32 index = count;
michael@0 73 time_t rv;
michael@0 74 struct timeval tp;
michael@0 75
michael@0 76 for (;index--;)
michael@0 77 rv = gettimeofday(&tp, NULL);
michael@0 78 }
michael@0 79
michael@0 80 static void timePRTime32(void)
michael@0 81 {
michael@0 82 PRInt32 index = count;
michael@0 83 PRInt32 rv32;
michael@0 84 PRTime q;
michael@0 85 PRTime rv;
michael@0 86
michael@0 87 LL_I2L(q, 1000000);
michael@0 88
michael@0 89 for (;index--;) {
michael@0 90 rv = PR_Now();
michael@0 91 LL_DIV(rv, rv, q);
michael@0 92 LL_L2I(rv32, rv);
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 static void timePRTime64(void)
michael@0 97 {
michael@0 98 PRInt32 index = count;
michael@0 99 PRTime rv;
michael@0 100
michael@0 101 for (;index--;)
michael@0 102 rv = PR_Now();
michael@0 103 }
michael@0 104
michael@0 105 /************************************************************************/
michael@0 106
michael@0 107 static void Measure(void (*func)(void), const char *msg)
michael@0 108 {
michael@0 109 PRIntervalTime start, stop;
michael@0 110 double d;
michael@0 111 PRInt32 tot;
michael@0 112
michael@0 113 start = PR_IntervalNow();
michael@0 114 (*func)();
michael@0 115 stop = PR_IntervalNow();
michael@0 116
michael@0 117 d = (double)PR_IntervalToMicroseconds(stop - start);
michael@0 118 tot = PR_IntervalToMilliseconds(stop-start);
michael@0 119
michael@0 120 if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
michael@0 121 }
michael@0 122
michael@0 123 int main(int argc, char **argv)
michael@0 124 {
michael@0 125 /* The command line argument: -d is used to determine if the test is being run
michael@0 126 in debug mode. The regress tool requires only one line output:PASS or FAIL.
michael@0 127 All of the printfs associated with this test has been handled with a if (debug_mode)
michael@0 128 test.
michael@0 129 Usage: test_name -d
michael@0 130 */
michael@0 131 PLOptStatus os;
michael@0 132 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
michael@0 133 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 134 {
michael@0 135 if (PL_OPT_BAD == os) continue;
michael@0 136 switch (opt->option)
michael@0 137 {
michael@0 138 case 'd': /* debug mode */
michael@0 139 debug_mode = 1;
michael@0 140 break;
michael@0 141 default:
michael@0 142 break;
michael@0 143 }
michael@0 144 }
michael@0 145 PL_DestroyOptState(opt);
michael@0 146
michael@0 147 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
michael@0 148 PR_STDIO_INIT();
michael@0 149
michael@0 150 if (argc > 1) {
michael@0 151 count = atoi(argv[1]);
michael@0 152 } else {
michael@0 153 count = DEFAULT_COUNT;
michael@0 154 }
michael@0 155
michael@0 156 ftime_init();
michael@0 157
michael@0 158 Measure(timeTime, "time to get time with time()");
michael@0 159 Measure(timeGethrtime, "time to get time with gethrtime()");
michael@0 160 Measure(timeGettimeofday, "time to get time with gettimeofday()");
michael@0 161 Measure(timePRTime32, "time to get time with PR_Time() (32bit)");
michael@0 162 Measure(timePRTime64, "time to get time with PR_Time() (64bit)");
michael@0 163
michael@0 164 PR_Cleanup();
michael@0 165 return 0;
michael@0 166 }
michael@0 167
michael@0 168
michael@0 169

mercurial