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