1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is the Netscape Portable Runtime (NSPR).
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
22
* Alternatively, the contents of this file may be used under the
23
* terms of the GNU General Public License Version 2 or later (the
24
* "GPL"), in which case the provisions of the GPL are applicable
25
* instead of those above. If you wish to allow use of your
26
* version of this file only under the terms of the GPL and not to
27
* allow others to use your version of this file under the MPL,
28
* indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by
30
* the GPL. If you do not delete the provisions above, a recipient
31
* may use your version of this file under either the MPL or the
35
/***********************************************************************
39
** Description: Measure PR_Select and Empty_Select performance.
41
** Modification History:
42
** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
43
** The debug mode will print all of the printfs associated with this test.
44
** The regress mode will be the default mode. Since the regress tool limits
45
** the output to a one line status:PASS or FAIL,all of the printf statements
46
** have been handled with an if (debug_mode) statement.
47
***********************************************************************/
49
/***********************************************************************
51
***********************************************************************/
52
/* Used to get the command line option */
65
#define DEFAULT_COUNT 10
69
/***********************************************************************
70
** PRIVATE FUNCTION: Test_Result
71
** DESCRIPTION: Used in conjunction with the regress tool, prints out the
72
** status of the test case.
81
** ALGORITHM: Determine what the status is and print accordingly.
83
***********************************************************************/
86
static void Test_Result (int result)
97
printf ("NOSTATUS\n");
102
static void EmptyPRSelect(void)
104
PRInt32 index = count;
108
rv = PR_Select(0, NULL, NULL, NULL, PR_INTERVAL_NO_WAIT);
111
static void EmptyNativeSelect(void)
114
PRInt32 index = count;
115
struct timeval timeout;
117
timeout.tv_sec = timeout.tv_usec = 0;
119
rv = select(0, NULL, NULL, NULL, &timeout);
122
static void PRSelectTest(void)
124
PRFileDesc *listenSocket;
125
PRNetAddr serverAddr;
127
if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
128
if (debug_mode) printf("\tServer error creating listen socket\n");
132
memset(&serverAddr, 0, sizeof(PRNetAddr));
133
serverAddr.inet.family = AF_INET;
134
serverAddr.inet.port = PR_htons(PORT);
135
serverAddr.inet.ip = PR_htonl(INADDR_ANY);
137
if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
138
if (debug_mode) printf("\tServer error binding to server address\n");
139
PR_Close(listenSocket);
143
if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
144
if (debug_mode) printf("\tServer error listening to server socket\n");
145
PR_Close(listenSocket);
148
if (debug_mode) printf("Listening on port %d\n", PORT);
161
if (debug_mode) printf("Going into accept\n");
163
newSock = PR_Accept(listenSocket,
165
PR_INTERVAL_NO_TIMEOUT);
168
if (debug_mode) printf("Got connection!\n");
170
if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError());
171
else Test_Result (FAIL);
175
PR_FD_SET(newSock, &rdset);
177
if (debug_mode) printf("Going into select \n");
179
rv = PR_Select(0, &rdset, 0, 0, PR_INTERVAL_NO_TIMEOUT);
181
if (debug_mode) printf("return from select is %d\n", rv);
183
if (PR_FD_ISSET(newSock, &rdset)) {
184
if (debug_mode) printf("I can't believe it- the socket is ready okay!\n");
186
if (debug_mode) printf("Damn; the select test failed...\n");
187
else Test_Result (FAIL);
190
strcpy(buf, "XXXXXXXXXX");
191
bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT);
194
if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf);
203
static void NativeSelectTest(void)
205
PRFileDesc *listenSocket;
206
PRNetAddr serverAddr;
208
if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
209
if (debug_mode) printf("\tServer error creating listen socket\n");
213
memset(&serverAddr, 0, sizeof(PRNetAddr));
214
serverAddr.inet.family = AF_INET;
215
serverAddr.inet.port = PR_htons(PORT);
216
serverAddr.inet.ip = PR_htonl(INADDR_ANY);
218
if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
219
if (debug_mode) printf("\tServer error binding to server address\n");
220
PR_Close(listenSocket);
224
if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
225
if (debug_mode) printf("\tServer error listening to server socket\n");
226
PR_Close(listenSocket);
229
if (debug_mode) printf("Listening on port %d\n", PORT);
237
struct timeval timeout;
238
PRInt32 bytesRead, rv, loops = 0;
242
if (debug_mode) printf("Going into accept\n");
244
newSock = PR_Accept(listenSocket, &rAddr, PR_INTERVAL_NO_TIMEOUT);
247
if (debug_mode) printf("Got connection!\n");
249
if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError());
250
else Test_Result (FAIL);
253
osfd = PR_FileDesc2NativeHandle(newSock);
255
FD_SET(osfd, &rdset);
257
if (debug_mode) printf("Going into select \n");
260
timeout.tv_sec = 2; timeout.tv_usec = 0;
261
rv = select(osfd + 1, &rdset, NULL, NULL, &timeout);
263
if (debug_mode) printf("return from select is %d\n", rv);
265
if (FD_ISSET(osfd, &rdset)) {
267
printf("I can't believe it- the socket is ready okay!\n");
269
if (debug_mode) printf("Damn; the select test failed...\n");
270
else Test_Result (FAIL);
273
strcpy(buf, "XXXXXXXXXX");
274
bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT);
277
if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf);
282
} /* NativeSelectTest */
284
#endif /* defined(XP_UNIX) */
286
/************************************************************************/
288
static void Measure(void (*func)(void), const char *msg)
290
PRIntervalTime start, stop;
294
start = PR_IntervalNow();
296
stop = PR_IntervalNow();
298
d = (double)PR_IntervalToMicroseconds(stop - start);
299
tot = PR_IntervalToMilliseconds(stop-start);
301
if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
304
void main(int argc, char **argv)
307
/* The command line argument: -d is used to determine if the test is being run
308
in debug mode. The regress tool requires only one line output:PASS or FAIL.
309
All of the printfs associated with this test has been handled with a if (debug_mode)
314
PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
315
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
317
if (PL_OPT_BAD == os) continue;
320
case 'd': /* debug mode */
327
PL_DestroyOptState(opt);
331
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
335
count = atoi(argv[2]);
337
count = DEFAULT_COUNT;
341
Measure(NativeSelectTest, "time to call 1 element select()");
343
Measure(EmptyPRSelect, "time to call Empty PR_select()");
344
Measure(EmptyNativeSelect, "time to call Empty select()");
345
Measure(PRSelectTest, "time to call 1 element PR_select()");
347
if (!debug_mode) Test_Result (NOSTATUS);