~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/select2.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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