1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * Program to test different ways to get the time; right now it is tuned
|
---|
40 | * only for solaris.
|
---|
41 | * solaris results (100000 iterations):
|
---|
42 | * time to get time with time(): 4.63 usec avg, 463 msec total
|
---|
43 | * time to get time with gethrtime(): 2.17 usec avg, 217 msec total
|
---|
44 | * time to get time with gettimeofday(): 1.25 usec avg, 125 msec total
|
---|
45 | *
|
---|
46 | *
|
---|
47 | */
|
---|
48 | /***********************************************************************
|
---|
49 | ** Includes
|
---|
50 | ***********************************************************************/
|
---|
51 | /* Used to get the command line option */
|
---|
52 | #include "plgetopt.h"
|
---|
53 |
|
---|
54 | #include "nspr.h"
|
---|
55 | #include "prpriv.h"
|
---|
56 | #include "prinrval.h"
|
---|
57 |
|
---|
58 | #include <stdio.h>
|
---|
59 | #include <stdlib.h>
|
---|
60 | #include <string.h>
|
---|
61 | #include <sys/time.h>
|
---|
62 |
|
---|
63 | #define DEFAULT_COUNT 100000
|
---|
64 | PRInt32 count;
|
---|
65 |
|
---|
66 | time_t itime;
|
---|
67 | hrtime_t ihrtime;
|
---|
68 |
|
---|
69 | void
|
---|
70 | ftime_init()
|
---|
71 | {
|
---|
72 | itime = time(NULL);
|
---|
73 | ihrtime = gethrtime();
|
---|
74 | }
|
---|
75 |
|
---|
76 | time_t
|
---|
77 | ftime()
|
---|
78 | {
|
---|
79 | hrtime_t now = gethrtime();
|
---|
80 |
|
---|
81 | return itime + ((now - ihrtime) / 1000000000ll);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static void timeTime(void)
|
---|
85 | {
|
---|
86 | PRInt32 index = count;
|
---|
87 | time_t rv;
|
---|
88 |
|
---|
89 | for (;index--;)
|
---|
90 | rv = time(NULL);
|
---|
91 | }
|
---|
92 |
|
---|
93 | static void timeGethrtime(void)
|
---|
94 | {
|
---|
95 | PRInt32 index = count;
|
---|
96 | time_t rv;
|
---|
97 |
|
---|
98 | for (;index--;)
|
---|
99 | rv = ftime();
|
---|
100 | }
|
---|
101 |
|
---|
102 | static void timeGettimeofday(void)
|
---|
103 | {
|
---|
104 | PRInt32 index = count;
|
---|
105 | time_t rv;
|
---|
106 | struct timeval tp;
|
---|
107 |
|
---|
108 | for (;index--;)
|
---|
109 | rv = gettimeofday(&tp, NULL);
|
---|
110 | }
|
---|
111 |
|
---|
112 | static void timePRTime32(void)
|
---|
113 | {
|
---|
114 | PRInt32 index = count;
|
---|
115 | PRInt32 rv32;
|
---|
116 | PRTime q;
|
---|
117 | PRTime rv;
|
---|
118 |
|
---|
119 | LL_I2L(q, 1000000);
|
---|
120 |
|
---|
121 | for (;index--;) {
|
---|
122 | rv = PR_Now();
|
---|
123 | LL_DIV(rv, rv, q);
|
---|
124 | LL_L2I(rv32, rv);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | static void timePRTime64(void)
|
---|
129 | {
|
---|
130 | PRInt32 index = count;
|
---|
131 | PRTime rv;
|
---|
132 |
|
---|
133 | for (;index--;)
|
---|
134 | rv = PR_Now();
|
---|
135 | }
|
---|
136 |
|
---|
137 | /************************************************************************/
|
---|
138 |
|
---|
139 | static void Measure(void (*func)(void), const char *msg)
|
---|
140 | {
|
---|
141 | PRIntervalTime start, stop;
|
---|
142 | double d;
|
---|
143 | PRInt32 tot;
|
---|
144 |
|
---|
145 | start = PR_IntervalNow();
|
---|
146 | (*func)();
|
---|
147 | stop = PR_IntervalNow();
|
---|
148 |
|
---|
149 | d = (double)PR_IntervalToMicroseconds(stop - start);
|
---|
150 | tot = PR_IntervalToMilliseconds(stop-start);
|
---|
151 |
|
---|
152 | if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
|
---|
153 | }
|
---|
154 |
|
---|
155 | void main(int argc, char **argv)
|
---|
156 | {
|
---|
157 | /* The command line argument: -d is used to determine if the test is being run
|
---|
158 | in debug mode. The regress tool requires only one line output:PASS or FAIL.
|
---|
159 | All of the printfs associated with this test has been handled with a if (debug_mode)
|
---|
160 | test.
|
---|
161 | Usage: test_name -d
|
---|
162 | */
|
---|
163 | PLOptStatus os;
|
---|
164 | PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
|
---|
165 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
166 | {
|
---|
167 | if (PL_OPT_BAD == os) continue;
|
---|
168 | switch (opt->option)
|
---|
169 | {
|
---|
170 | case 'd': /* debug mode */
|
---|
171 | debug_mode = 1;
|
---|
172 | break;
|
---|
173 | default:
|
---|
174 | break;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | PL_DestroyOptState(opt);
|
---|
178 |
|
---|
179 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
|
---|
180 | PR_STDIO_INIT();
|
---|
181 |
|
---|
182 | if (argc > 1) {
|
---|
183 | count = atoi(argv[1]);
|
---|
184 | } else {
|
---|
185 | count = DEFAULT_COUNT;
|
---|
186 | }
|
---|
187 |
|
---|
188 | ftime_init();
|
---|
189 |
|
---|
190 | Measure(timeTime, "time to get time with time()");
|
---|
191 | Measure(timeGethrtime, "time to get time with gethrtime()");
|
---|
192 | Measure(timeGettimeofday, "time to get time with gettimeofday()");
|
---|
193 | Measure(timePRTime32, "time to get time with PR_Time() (32bit)");
|
---|
194 | Measure(timePRTime64, "time to get time with PR_Time() (64bit)");
|
---|
195 |
|
---|
196 | PR_Cleanup();
|
---|
197 | return 0;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 |
|
---|