Debian Codegraph @

nsprpub/pr/tests/time.c

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