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 | **
|
---|
40 | ** Name: select2.c
|
---|
41 | **
|
---|
42 | ** Description: Measure PR_Select and Empty_Select performance.
|
---|
43 | **
|
---|
44 | ** Modification History:
|
---|
45 | ** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
|
---|
46 | ** The debug mode will print all of the printfs associated with this test.
|
---|
47 | ** The regress mode will be the default mode. Since the regress tool limits
|
---|
48 | ** the output to a one line status:PASS or FAIL,all of the printf statements
|
---|
49 | ** have been handled with an if (debug_mode) statement.
|
---|
50 | ***********************************************************************/
|
---|
51 |
|
---|
52 | /***********************************************************************
|
---|
53 | ** Includes
|
---|
54 | ***********************************************************************/
|
---|
55 | /* Used to get the command line option */
|
---|
56 | #include "plgetopt.h"
|
---|
57 | #include "prttools.h"
|
---|
58 | #include "primpl.h"
|
---|
59 |
|
---|
60 | #include <stdio.h>
|
---|
61 | #include <stdlib.h>
|
---|
62 | #include <string.h>
|
---|
63 | #if defined(OS2)
|
---|
64 | #include <sys/time.h>
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | #define PORT 8000
|
---|
68 | #define DEFAULT_COUNT 10
|
---|
69 | PRInt32 count;
|
---|
70 |
|
---|
71 |
|
---|
72 | /***********************************************************************
|
---|
73 | ** PRIVATE FUNCTION: Test_Result
|
---|
74 | ** DESCRIPTION: Used in conjunction with the regress tool, prints out the
|
---|
75 | ** status of the test case.
|
---|
76 | ** INPUTS: PASS/FAIL
|
---|
77 | ** OUTPUTS: None
|
---|
78 | ** RETURN: None
|
---|
79 | ** SIDE EFFECTS:
|
---|
80 | **
|
---|
81 | ** RESTRICTIONS:
|
---|
82 | ** None
|
---|
83 | ** MEMORY: NA
|
---|
84 | ** ALGORITHM: Determine what the status is and print accordingly.
|
---|
85 | **
|
---|
86 | ***********************************************************************/
|
---|
87 |
|
---|
88 |
|
---|
89 | static void Test_Result (int result)
|
---|
90 | {
|
---|
91 | switch (result)
|
---|
92 | {
|
---|
93 | case PASS:
|
---|
94 | printf ("PASS\n");
|
---|
95 | break;
|
---|
96 | case FAIL:
|
---|
97 | printf ("FAIL\n");
|
---|
98 | break;
|
---|
99 | default:
|
---|
100 | printf ("NOSTATUS\n");
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | static void EmptyPRSelect(void)
|
---|
106 | {
|
---|
107 | PRInt32 index = count;
|
---|
108 | PRInt32 rv;
|
---|
109 |
|
---|
110 | for (; index--;)
|
---|
111 | rv = PR_Select(0, NULL, NULL, NULL, PR_INTERVAL_NO_WAIT);
|
---|
112 | }
|
---|
113 |
|
---|
114 | static void EmptyNativeSelect(void)
|
---|
115 | {
|
---|
116 | PRInt32 rv;
|
---|
117 | PRInt32 index = count;
|
---|
118 | struct timeval timeout;
|
---|
119 |
|
---|
120 | timeout.tv_sec = timeout.tv_usec = 0;
|
---|
121 | for (; index--;)
|
---|
122 | rv = select(0, NULL, NULL, NULL, &timeout);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static void PRSelectTest(void)
|
---|
126 | {
|
---|
127 | PRFileDesc *listenSocket;
|
---|
128 | PRNetAddr serverAddr;
|
---|
129 |
|
---|
130 | if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
|
---|
131 | if (debug_mode) printf("\tServer error creating listen socket\n");
|
---|
132 | return;
|
---|
133 | }
|
---|
134 |
|
---|
135 | memset(&serverAddr, 0, sizeof(PRNetAddr));
|
---|
136 | serverAddr.inet.family = AF_INET;
|
---|
137 | serverAddr.inet.port = PR_htons(PORT);
|
---|
138 | serverAddr.inet.ip = PR_htonl(INADDR_ANY);
|
---|
139 |
|
---|
140 | if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
|
---|
141 | if (debug_mode) printf("\tServer error binding to server address\n");
|
---|
142 | PR_Close(listenSocket);
|
---|
143 | return;
|
---|
144 | }
|
---|
145 |
|
---|
146 | if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
|
---|
147 | if (debug_mode) printf("\tServer error listening to server socket\n");
|
---|
148 | PR_Close(listenSocket);
|
---|
149 | return;
|
---|
150 | }
|
---|
151 | if (debug_mode) printf("Listening on port %d\n", PORT);
|
---|
152 |
|
---|
153 | {
|
---|
154 | PRFileDesc *newSock;
|
---|
155 | PRNetAddr rAddr;
|
---|
156 | PRInt32 loops = 0;
|
---|
157 | PR_fd_set rdset;
|
---|
158 | PRInt32 rv;
|
---|
159 | PRInt32 bytesRead;
|
---|
160 | char buf[11];
|
---|
161 |
|
---|
162 | loops++;
|
---|
163 |
|
---|
164 | if (debug_mode) printf("Going into accept\n");
|
---|
165 |
|
---|
166 | newSock = PR_Accept(listenSocket,
|
---|
167 | &rAddr,
|
---|
168 | PR_INTERVAL_NO_TIMEOUT);
|
---|
169 |
|
---|
170 | if (newSock) {
|
---|
171 | if (debug_mode) printf("Got connection!\n");
|
---|
172 | } else {
|
---|
173 | if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError());
|
---|
174 | else Test_Result (FAIL);
|
---|
175 | }
|
---|
176 |
|
---|
177 | PR_FD_ZERO(&rdset);
|
---|
178 | PR_FD_SET(newSock, &rdset);
|
---|
179 |
|
---|
180 | if (debug_mode) printf("Going into select \n");
|
---|
181 |
|
---|
182 | rv = PR_Select(0, &rdset, 0, 0, PR_INTERVAL_NO_TIMEOUT);
|
---|
183 |
|
---|
184 | if (debug_mode) printf("return from select is %d\n", rv);
|
---|
185 |
|
---|
186 | if (PR_FD_ISSET(newSock, &rdset)) {
|
---|
187 | if (debug_mode) printf("I can't believe it- the socket is ready okay!\n");
|
---|
188 | } else {
|
---|
189 | if (debug_mode) printf("Damn; the select test failed...\n");
|
---|
190 | else Test_Result (FAIL);
|
---|
191 | }
|
---|
192 |
|
---|
193 | strcpy(buf, "XXXXXXXXXX");
|
---|
194 | bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT);
|
---|
195 | buf[10] = '\0';
|
---|
196 |
|
---|
197 | if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf);
|
---|
198 |
|
---|
199 | PR_Close(newSock);
|
---|
200 | }
|
---|
201 |
|
---|
202 | }
|
---|
203 |
|
---|
204 | #if defined(XP_UNIX)
|
---|
205 |
|
---|
206 | static void NativeSelectTest(void)
|
---|
207 | {
|
---|
208 | PRFileDesc *listenSocket;
|
---|
209 | PRNetAddr serverAddr;
|
---|
210 |
|
---|
211 | if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
|
---|
212 | if (debug_mode) printf("\tServer error creating listen socket\n");
|
---|
213 | return;
|
---|
214 | }
|
---|
215 |
|
---|
216 | memset(&serverAddr, 0, sizeof(PRNetAddr));
|
---|
217 | serverAddr.inet.family = AF_INET;
|
---|
218 | serverAddr.inet.port = PR_htons(PORT);
|
---|
219 | serverAddr.inet.ip = PR_htonl(INADDR_ANY);
|
---|
220 |
|
---|
221 | if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
|
---|
222 | if (debug_mode) printf("\tServer error binding to server address\n");
|
---|
223 | PR_Close(listenSocket);
|
---|
224 | return;
|
---|
225 | }
|
---|
226 |
|
---|
227 | if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
|
---|
228 | if (debug_mode) printf("\tServer error listening to server socket\n");
|
---|
229 | PR_Close(listenSocket);
|
---|
230 | return;
|
---|
231 | }
|
---|
232 | if (debug_mode) printf("Listening on port %d\n", PORT);
|
---|
233 |
|
---|
234 | {
|
---|
235 | PRIntn osfd;
|
---|
236 | char buf[11];
|
---|
237 | fd_set rdset;
|
---|
238 | PRNetAddr rAddr;
|
---|
239 | PRFileDesc *newSock;
|
---|
240 | struct timeval timeout;
|
---|
241 | PRInt32 bytesRead, rv, loops = 0;
|
---|
242 |
|
---|
243 | loops++;
|
---|
244 |
|
---|
245 | if (debug_mode) printf("Going into accept\n");
|
---|
246 |
|
---|
247 | newSock = PR_Accept(listenSocket, &rAddr, PR_INTERVAL_NO_TIMEOUT);
|
---|
248 |
|
---|
249 | if (newSock) {
|
---|
250 | if (debug_mode) printf("Got connection!\n");
|
---|
251 | } else {
|
---|
252 | if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError());
|
---|
253 | else Test_Result (FAIL);
|
---|
254 | }
|
---|
255 |
|
---|
256 | osfd = PR_FileDesc2NativeHandle(newSock);
|
---|
257 | FD_ZERO(&rdset);
|
---|
258 | FD_SET(osfd, &rdset);
|
---|
259 |
|
---|
260 | if (debug_mode) printf("Going into select \n");
|
---|
261 |
|
---|
262 |
|
---|
263 | timeout.tv_sec = 2; timeout.tv_usec = 0;
|
---|
264 | rv = select(osfd + 1, &rdset, NULL, NULL, &timeout);
|
---|
265 |
|
---|
266 | if (debug_mode) printf("return from select is %d\n", rv);
|
---|
267 |
|
---|
268 | if (FD_ISSET(osfd, &rdset)) {
|
---|
269 | if (debug_mode)
|
---|
270 | printf("I can't believe it- the socket is ready okay!\n");
|
---|
271 | } else {
|
---|
272 | if (debug_mode) printf("Damn; the select test failed...\n");
|
---|
273 | else Test_Result (FAIL);
|
---|
274 | }
|
---|
275 |
|
---|
276 | strcpy(buf, "XXXXXXXXXX");
|
---|
277 | bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT);
|
---|
278 | buf[10] = '\0';
|
---|
279 |
|
---|
280 | if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf);
|
---|
281 |
|
---|
282 | PR_Close(newSock);
|
---|
283 | }
|
---|
284 |
|
---|
285 | } /* NativeSelectTest */
|
---|
286 |
|
---|
287 | #endif /* defined(XP_UNIX) */
|
---|
288 |
|
---|
289 | /************************************************************************/
|
---|
290 |
|
---|
291 | static void Measure(void (*func)(void), const char *msg)
|
---|
292 | {
|
---|
293 | PRIntervalTime start, stop;
|
---|
294 | double d;
|
---|
295 | PRInt32 tot;
|
---|
296 |
|
---|
297 | start = PR_IntervalNow();
|
---|
298 | (*func)();
|
---|
299 | stop = PR_IntervalNow();
|
---|
300 |
|
---|
301 | d = (double)PR_IntervalToMicroseconds(stop - start);
|
---|
302 | tot = PR_IntervalToMilliseconds(stop-start);
|
---|
303 |
|
---|
304 | if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
|
---|
305 | }
|
---|
306 |
|
---|
307 | void main(int argc, char **argv)
|
---|
308 | {
|
---|
309 |
|
---|
310 | /* The command line argument: -d is used to determine if the test is being run
|
---|
311 | in debug mode. The regress tool requires only one line output:PASS or FAIL.
|
---|
312 | All of the printfs associated with this test has been handled with a if (debug_mode)
|
---|
313 | test.
|
---|
314 | Usage: test_name -d
|
---|
315 | */
|
---|
316 | PLOptStatus os;
|
---|
317 | PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
|
---|
318 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
319 | {
|
---|
320 | if (PL_OPT_BAD == os) continue;
|
---|
321 | switch (opt->option)
|
---|
322 | {
|
---|
323 | case 'd': /* debug mode */
|
---|
324 | debug_mode = 1;
|
---|
325 | break;
|
---|
326 | default:
|
---|
327 | break;
|
---|
328 | }
|
---|
329 | }
|
---|
330 | PL_DestroyOptState(opt);
|
---|
331 |
|
---|
332 | /* main test */
|
---|
333 |
|
---|
334 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
|
---|
335 | PR_STDIO_INIT();
|
---|
336 |
|
---|
337 | if (argc > 2) {
|
---|
338 | count = atoi(argv[2]);
|
---|
339 | } else {
|
---|
340 | count = DEFAULT_COUNT;
|
---|
341 | }
|
---|
342 |
|
---|
343 | #if defined(XP_UNIX)
|
---|
344 | Measure(NativeSelectTest, "time to call 1 element select()");
|
---|
345 | #endif
|
---|
346 | Measure(EmptyPRSelect, "time to call Empty PR_select()");
|
---|
347 | Measure(EmptyNativeSelect, "time to call Empty select()");
|
---|
348 | Measure(PRSelectTest, "time to call 1 element PR_select()");
|
---|
349 |
|
---|
350 | if (!debug_mode) Test_Result (NOSTATUS);
|
---|
351 | PR_Cleanup();
|
---|
352 |
|
---|
353 |
|
---|
354 | }
|
---|