summaryrefslogtreecommitdiffstats
path: root/nsprpub/pr/tests/select2.c
blob: da7360b0259f63eaf0f13c16ad344c0fce356dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/***********************************************************************
**
** Name: select2.c
**
** Description: Measure PR_Select and Empty_Select performance.
**
** Modification History:
** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
**           The debug mode will print all of the printfs associated with this test.
**           The regress mode will be the default mode. Since the regress tool limits
**           the output to a one line status:PASS or FAIL,all of the printf statements
**           have been handled with an if (debug_mode) statement.
***********************************************************************/

/***********************************************************************
** Includes
***********************************************************************/
/* Used to get the command line option */
#include "plgetopt.h"
#include "prttools.h"
#include "primpl.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(OS2)
#include <sys/time.h>
#endif

#define PORT 8000
#define DEFAULT_COUNT 10
PRInt32 count;


/***********************************************************************
** PRIVATE FUNCTION:    Test_Result
** DESCRIPTION: Used in conjunction with the regress tool, prints out the
**              status of the test case.
** INPUTS:      PASS/FAIL
** OUTPUTS:     None
** RETURN:      None
** SIDE EFFECTS:
**
** RESTRICTIONS:
**      None
** MEMORY:      NA
** ALGORITHM:   Determine what the status is and print accordingly.
**
***********************************************************************/


static void Test_Result (int result)
{
    switch (result)
    {
        case PASS:
            printf ("PASS\n");
            break;
        case FAIL:
            printf ("FAIL\n");
            break;
        default:
            printf ("NOSTATUS\n");
            break;
    }
}

static void EmptyPRSelect(void)
{
    PRInt32 index = count;
    PRInt32 rv;

    for (; index--;) {
        rv = PR_Select(0, NULL, NULL, NULL, PR_INTERVAL_NO_WAIT);
    }
}

static void EmptyNativeSelect(void)
{
    PRInt32 rv;
    PRInt32 index = count;
    struct timeval timeout;

    timeout.tv_sec = timeout.tv_usec = 0;
    for (; index--;) {
        rv = select(0, NULL, NULL, NULL, &timeout);
    }
}

static void PRSelectTest(void)
{
    PRFileDesc *listenSocket;
    PRNetAddr serverAddr;

    if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
        if (debug_mode) {
            printf("\tServer error creating listen socket\n");
        }
        return;
    }

    memset(&serverAddr, 0, sizeof(PRNetAddr));
    serverAddr.inet.family = AF_INET;
    serverAddr.inet.port = PR_htons(PORT);
    serverAddr.inet.ip = PR_htonl(INADDR_ANY);

    if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
        if (debug_mode) {
            printf("\tServer error binding to server address\n");
        }
        PR_Close(listenSocket);
        return;
    }

    if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
        if (debug_mode) {
            printf("\tServer error listening to server socket\n");
        }
        PR_Close(listenSocket);
        return;
    }
    if (debug_mode) {
        printf("Listening on port %d\n", PORT);
    }

    {
        PRFileDesc *newSock;
        PRNetAddr rAddr;
        PRInt32 loops = 0;
        PR_fd_set rdset;
        PRInt32 rv;
        PRInt32 bytesRead;
        char buf[11];

        loops++;

        if (debug_mode) {
            printf("Going into accept\n");
        }

        newSock = PR_Accept(listenSocket,
                            &rAddr,
                            PR_INTERVAL_NO_TIMEOUT);

        if (newSock) {
            if (debug_mode) {
                printf("Got connection!\n");
            }
        } else {
            if (debug_mode) {
                printf("PR_Accept failed: error code %d\n", PR_GetError());
            }
            else {
                Test_Result (FAIL);
            }
        }

        PR_FD_ZERO(&rdset);
        PR_FD_SET(newSock, &rdset);

        if (debug_mode) {
            printf("Going into select \n");
        }

        rv = PR_Select(0, &rdset, 0, 0, PR_INTERVAL_NO_TIMEOUT);

        if (debug_mode) {
            printf("return from select is %d\n", rv);
        }

        if (PR_FD_ISSET(newSock, &rdset)) {
            if (debug_mode) {
                printf("I can't believe it- the socket is ready okay!\n");
            }
        } else {
            if (debug_mode) {
                printf("Damn; the select test failed...\n");
            }
            else {
                Test_Result (FAIL);
            }
        }

        strcpy(buf, "XXXXXXXXXX");
        bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT);
        buf[10] = '\0';

        if (debug_mode) {
            printf("Recv completed with %d bytes, %s\n", bytesRead, buf);
        }

        PR_Close(newSock);
    }

}

#if defined(XP_UNIX)

static void NativeSelectTest(void)
{
    PRFileDesc *listenSocket;
    PRNetAddr serverAddr;

    if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
        if (debug_mode) {
            printf("\tServer error creating listen socket\n");
        }
        return;
    }

    memset(&serverAddr, 0, sizeof(PRNetAddr));
    serverAddr.inet.family = AF_INET;
    serverAddr.inet.port = PR_htons(PORT);
    serverAddr.inet.ip = PR_htonl(INADDR_ANY);

    if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
        if (debug_mode) {
            printf("\tServer error binding to server address\n");
        }
        PR_Close(listenSocket);
        return;
    }

    if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
        if (debug_mode) {
            printf("\tServer error listening to server socket\n");
        }
        PR_Close(listenSocket);
        return;
    }
    if (debug_mode) {
        printf("Listening on port %d\n", PORT);
    }

    {
        PRIntn osfd;
        char buf[11];
        fd_set rdset;
        PRNetAddr rAddr;
        PRFileDesc *newSock;
        struct timeval timeout;
        PRInt32 bytesRead, rv, loops = 0;

        loops++;

        if (debug_mode) {
            printf("Going into accept\n");
        }

        newSock = PR_Accept(listenSocket, &rAddr, PR_INTERVAL_NO_TIMEOUT);

        if (newSock) {
            if (debug_mode) {
                printf("Got connection!\n");
            }
        } else {
            if (debug_mode) {
                printf("PR_Accept failed: error code %d\n", PR_GetError());
            }
            else {
                Test_Result (FAIL);
            }
        }

        osfd = PR_FileDesc2NativeHandle(newSock);
        FD_ZERO(&rdset);
        FD_SET(osfd, &rdset);

        if (debug_mode) {
            printf("Going into select \n");
        }


        timeout.tv_sec = 2; timeout.tv_usec = 0;
        rv = select(osfd + 1, &rdset, NULL, NULL, &timeout);

        if (debug_mode) {
            printf("return from select is %d\n", rv);
        }

        if (FD_ISSET(osfd, &rdset)) {
            if (debug_mode) {
                printf("I can't believe it- the socket is ready okay!\n");
            }
        } else {
            if (debug_mode) {
                printf("Damn; the select test failed...\n");
            }
            else {
                Test_Result (FAIL);
            }
        }

        strcpy(buf, "XXXXXXXXXX");
        bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT);
        buf[10] = '\0';

        if (debug_mode) {
            printf("Recv completed with %d bytes, %s\n", bytesRead, buf);
        }

        PR_Close(newSock);
    }

}  /* NativeSelectTest */

#endif /* defined(XP_UNIX) */

/************************************************************************/

static void Measure(void (*func)(void), const char *msg)
{
    PRIntervalTime start, stop;
    double d;
    PRInt32 tot;

    start = PR_IntervalNow();
    (*func)();
    stop = PR_IntervalNow();

    d = (double)PR_IntervalToMicroseconds(stop - start);
    tot = PR_IntervalToMilliseconds(stop-start);

    if (debug_mode) {
        printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
    }
}

int main(int argc, char **argv)
{

    /* The command line argument: -d is used to determine if the test is being run
    in debug mode. The regress tool requires only one line output:PASS or FAIL.
    All of the printfs associated with this test has been handled with a if (debug_mode)
    test.
    Usage: test_name -d
    */
    PLOptStatus os;
    PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    {
        if (PL_OPT_BAD == os) {
            continue;
        }
        switch (opt->option)
        {
            case 'd':  /* debug mode */
                debug_mode = 1;
                break;
            default:
                break;
        }
    }
    PL_DestroyOptState(opt);

    /* main test */

    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
    PR_STDIO_INIT();

    if (argc > 2) {
        count = atoi(argv[2]);
    } else {
        count = DEFAULT_COUNT;
    }

#if defined(XP_UNIX)
    Measure(NativeSelectTest, "time to call 1 element select()");
#endif
    Measure(EmptyPRSelect, "time to call Empty PR_select()");
    Measure(EmptyNativeSelect, "time to call Empty select()");
    Measure(PRSelectTest, "time to call 1 element PR_select()");

    if (!debug_mode) {
        Test_Result (NOSTATUS);
    }
    PR_Cleanup();


}