nsprpub/pr/tests/select2.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     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 ***********************************************************************/
    20 /***********************************************************************
    21 ** Includes
    22 ***********************************************************************/
    23 /* Used to get the command line option */
    24 #include "plgetopt.h"
    25 #include "prttools.h"
    26 #include "primpl.h"
    28 #include <stdio.h>
    29 #include <stdlib.h>
    30 #include <string.h>
    31 #if defined(OS2)
    32 #include <sys/time.h>
    33 #endif
    35 #define PORT 8000
    36 #define DEFAULT_COUNT 10
    37 PRInt32 count;
    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 ***********************************************************************/
    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 }
    73 static void EmptyPRSelect(void)
    74 {
    75     PRInt32 index = count;
    76     PRInt32 rv;
    78     for (; index--;)
    79         rv = PR_Select(0, NULL, NULL, NULL, PR_INTERVAL_NO_WAIT);
    80 }
    82 static void EmptyNativeSelect(void)
    83 {
    84     PRInt32 rv;
    85     PRInt32 index = count;
    86     struct timeval timeout;
    88     timeout.tv_sec = timeout.tv_usec = 0;
    89     for (; index--;)
    90         rv = select(0, NULL, NULL, NULL, &timeout);
    91 }
    93 static void PRSelectTest(void)
    94 {
    95     PRFileDesc *listenSocket;
    96     PRNetAddr serverAddr;
    98     if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
    99         if (debug_mode) printf("\tServer error creating listen socket\n");
   100         return;
   101     }
   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);
   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     }
   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);
   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];
   130         loops++;
   132         if (debug_mode) printf("Going into accept\n"); 
   134         newSock = PR_Accept(listenSocket, 
   135                               &rAddr,
   136                               PR_INTERVAL_NO_TIMEOUT);
   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         }
   145         PR_FD_ZERO(&rdset);
   146         PR_FD_SET(newSock, &rdset);
   148         if (debug_mode) printf("Going into select \n");
   150         rv = PR_Select(0, &rdset, 0, 0, PR_INTERVAL_NO_TIMEOUT);
   152         if (debug_mode) printf("return from select is %d\n", rv);
   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         }
   161         strcpy(buf, "XXXXXXXXXX");
   162         bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT);
   163 	buf[10] = '\0';
   165         if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf);
   167         PR_Close(newSock);
   168     }
   170 }
   172 #if defined(XP_UNIX)
   174 static void NativeSelectTest(void)
   175 {
   176     PRFileDesc *listenSocket;
   177     PRNetAddr serverAddr;
   179     if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
   180         if (debug_mode) printf("\tServer error creating listen socket\n");
   181         return;
   182     }
   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);
   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     }
   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);
   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;
   211         loops++;
   213         if (debug_mode) printf("Going into accept\n"); 
   215         newSock = PR_Accept(listenSocket, &rAddr, PR_INTERVAL_NO_TIMEOUT);
   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         }
   224         osfd = PR_FileDesc2NativeHandle(newSock);
   225         FD_ZERO(&rdset);
   226         FD_SET(osfd, &rdset);
   228         if (debug_mode) printf("Going into select \n");
   231         timeout.tv_sec = 2; timeout.tv_usec = 0;
   232         rv = select(osfd + 1, &rdset, NULL, NULL, &timeout);
   234         if (debug_mode) printf("return from select is %d\n", rv);
   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         }
   244         strcpy(buf, "XXXXXXXXXX");
   245         bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT);
   246 	buf[10] = '\0';
   248         if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf);
   250         PR_Close(newSock);
   251     }
   253 }  /* NativeSelectTest */
   255 #endif /* defined(XP_UNIX) */
   257 /************************************************************************/
   259 static void Measure(void (*func)(void), const char *msg)
   260 {
   261     PRIntervalTime start, stop;
   262     double d;
   263     PRInt32 tot;
   265     start = PR_IntervalNow();
   266     (*func)();
   267     stop = PR_IntervalNow();
   269     d = (double)PR_IntervalToMicroseconds(stop - start);
   270     tot = PR_IntervalToMilliseconds(stop-start);
   272     if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
   273 }
   275 int main(int argc, char **argv)
   276 {
   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);
   300  /* main test */
   302     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   303     PR_STDIO_INIT();
   305     if (argc > 2) {
   306 	count = atoi(argv[2]);
   307     } else {
   308 	count = DEFAULT_COUNT;
   309     }
   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()");
   318 	if (!debug_mode) Test_Result (NOSTATUS);
   319     PR_Cleanup();
   322 }

mercurial