michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /*********************************************************************** michael@0: ** michael@0: ** Name: select2.c michael@0: ** michael@0: ** Description: Measure PR_Select and Empty_Select performance. michael@0: ** michael@0: ** Modification History: michael@0: ** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. michael@0: ** The debug mode will print all of the printfs associated with this test. michael@0: ** The regress mode will be the default mode. Since the regress tool limits michael@0: ** the output to a one line status:PASS or FAIL,all of the printf statements michael@0: ** have been handled with an if (debug_mode) statement. michael@0: ***********************************************************************/ michael@0: michael@0: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: /* Used to get the command line option */ michael@0: #include "plgetopt.h" michael@0: #include "prttools.h" michael@0: #include "primpl.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #if defined(OS2) michael@0: #include michael@0: #endif michael@0: michael@0: #define PORT 8000 michael@0: #define DEFAULT_COUNT 10 michael@0: PRInt32 count; michael@0: michael@0: michael@0: /*********************************************************************** michael@0: ** PRIVATE FUNCTION: Test_Result michael@0: ** DESCRIPTION: Used in conjunction with the regress tool, prints out the michael@0: ** status of the test case. michael@0: ** INPUTS: PASS/FAIL michael@0: ** OUTPUTS: None michael@0: ** RETURN: None michael@0: ** SIDE EFFECTS: michael@0: ** michael@0: ** RESTRICTIONS: michael@0: ** None michael@0: ** MEMORY: NA michael@0: ** ALGORITHM: Determine what the status is and print accordingly. michael@0: ** michael@0: ***********************************************************************/ michael@0: michael@0: michael@0: static void Test_Result (int result) michael@0: { michael@0: switch (result) michael@0: { michael@0: case PASS: michael@0: printf ("PASS\n"); michael@0: break; michael@0: case FAIL: michael@0: printf ("FAIL\n"); michael@0: break; michael@0: default: michael@0: printf ("NOSTATUS\n"); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: static void EmptyPRSelect(void) michael@0: { michael@0: PRInt32 index = count; michael@0: PRInt32 rv; michael@0: michael@0: for (; index--;) michael@0: rv = PR_Select(0, NULL, NULL, NULL, PR_INTERVAL_NO_WAIT); michael@0: } michael@0: michael@0: static void EmptyNativeSelect(void) michael@0: { michael@0: PRInt32 rv; michael@0: PRInt32 index = count; michael@0: struct timeval timeout; michael@0: michael@0: timeout.tv_sec = timeout.tv_usec = 0; michael@0: for (; index--;) michael@0: rv = select(0, NULL, NULL, NULL, &timeout); michael@0: } michael@0: michael@0: static void PRSelectTest(void) michael@0: { michael@0: PRFileDesc *listenSocket; michael@0: PRNetAddr serverAddr; michael@0: michael@0: if ( (listenSocket = PR_NewTCPSocket()) == NULL) { michael@0: if (debug_mode) printf("\tServer error creating listen socket\n"); michael@0: return; michael@0: } michael@0: michael@0: memset(&serverAddr, 0, sizeof(PRNetAddr)); michael@0: serverAddr.inet.family = AF_INET; michael@0: serverAddr.inet.port = PR_htons(PORT); michael@0: serverAddr.inet.ip = PR_htonl(INADDR_ANY); michael@0: michael@0: if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { michael@0: if (debug_mode) printf("\tServer error binding to server address\n"); michael@0: PR_Close(listenSocket); michael@0: return; michael@0: } michael@0: michael@0: if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { michael@0: if (debug_mode) printf("\tServer error listening to server socket\n"); michael@0: PR_Close(listenSocket); michael@0: return; michael@0: } michael@0: if (debug_mode) printf("Listening on port %d\n", PORT); michael@0: michael@0: { michael@0: PRFileDesc *newSock; michael@0: PRNetAddr rAddr; michael@0: PRInt32 loops = 0; michael@0: PR_fd_set rdset; michael@0: PRInt32 rv; michael@0: PRInt32 bytesRead; michael@0: char buf[11]; michael@0: michael@0: loops++; michael@0: michael@0: if (debug_mode) printf("Going into accept\n"); michael@0: michael@0: newSock = PR_Accept(listenSocket, michael@0: &rAddr, michael@0: PR_INTERVAL_NO_TIMEOUT); michael@0: michael@0: if (newSock) { michael@0: if (debug_mode) printf("Got connection!\n"); michael@0: } else { michael@0: if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError()); michael@0: else Test_Result (FAIL); michael@0: } michael@0: michael@0: PR_FD_ZERO(&rdset); michael@0: PR_FD_SET(newSock, &rdset); michael@0: michael@0: if (debug_mode) printf("Going into select \n"); michael@0: michael@0: rv = PR_Select(0, &rdset, 0, 0, PR_INTERVAL_NO_TIMEOUT); michael@0: michael@0: if (debug_mode) printf("return from select is %d\n", rv); michael@0: michael@0: if (PR_FD_ISSET(newSock, &rdset)) { michael@0: if (debug_mode) printf("I can't believe it- the socket is ready okay!\n"); michael@0: } else { michael@0: if (debug_mode) printf("Damn; the select test failed...\n"); michael@0: else Test_Result (FAIL); michael@0: } michael@0: michael@0: strcpy(buf, "XXXXXXXXXX"); michael@0: bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT); michael@0: buf[10] = '\0'; michael@0: michael@0: if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf); michael@0: michael@0: PR_Close(newSock); michael@0: } michael@0: michael@0: } michael@0: michael@0: #if defined(XP_UNIX) michael@0: michael@0: static void NativeSelectTest(void) michael@0: { michael@0: PRFileDesc *listenSocket; michael@0: PRNetAddr serverAddr; michael@0: michael@0: if ( (listenSocket = PR_NewTCPSocket()) == NULL) { michael@0: if (debug_mode) printf("\tServer error creating listen socket\n"); michael@0: return; michael@0: } michael@0: michael@0: memset(&serverAddr, 0, sizeof(PRNetAddr)); michael@0: serverAddr.inet.family = AF_INET; michael@0: serverAddr.inet.port = PR_htons(PORT); michael@0: serverAddr.inet.ip = PR_htonl(INADDR_ANY); michael@0: michael@0: if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { michael@0: if (debug_mode) printf("\tServer error binding to server address\n"); michael@0: PR_Close(listenSocket); michael@0: return; michael@0: } michael@0: michael@0: if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { michael@0: if (debug_mode) printf("\tServer error listening to server socket\n"); michael@0: PR_Close(listenSocket); michael@0: return; michael@0: } michael@0: if (debug_mode) printf("Listening on port %d\n", PORT); michael@0: michael@0: { michael@0: PRIntn osfd; michael@0: char buf[11]; michael@0: fd_set rdset; michael@0: PRNetAddr rAddr; michael@0: PRFileDesc *newSock; michael@0: struct timeval timeout; michael@0: PRInt32 bytesRead, rv, loops = 0; michael@0: michael@0: loops++; michael@0: michael@0: if (debug_mode) printf("Going into accept\n"); michael@0: michael@0: newSock = PR_Accept(listenSocket, &rAddr, PR_INTERVAL_NO_TIMEOUT); michael@0: michael@0: if (newSock) { michael@0: if (debug_mode) printf("Got connection!\n"); michael@0: } else { michael@0: if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError()); michael@0: else Test_Result (FAIL); michael@0: } michael@0: michael@0: osfd = PR_FileDesc2NativeHandle(newSock); michael@0: FD_ZERO(&rdset); michael@0: FD_SET(osfd, &rdset); michael@0: michael@0: if (debug_mode) printf("Going into select \n"); michael@0: michael@0: michael@0: timeout.tv_sec = 2; timeout.tv_usec = 0; michael@0: rv = select(osfd + 1, &rdset, NULL, NULL, &timeout); michael@0: michael@0: if (debug_mode) printf("return from select is %d\n", rv); michael@0: michael@0: if (FD_ISSET(osfd, &rdset)) { michael@0: if (debug_mode) michael@0: printf("I can't believe it- the socket is ready okay!\n"); michael@0: } else { michael@0: if (debug_mode) printf("Damn; the select test failed...\n"); michael@0: else Test_Result (FAIL); michael@0: } michael@0: michael@0: strcpy(buf, "XXXXXXXXXX"); michael@0: bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT); michael@0: buf[10] = '\0'; michael@0: michael@0: if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf); michael@0: michael@0: PR_Close(newSock); michael@0: } michael@0: michael@0: } /* NativeSelectTest */ michael@0: michael@0: #endif /* defined(XP_UNIX) */ michael@0: michael@0: /************************************************************************/ michael@0: michael@0: static void Measure(void (*func)(void), const char *msg) michael@0: { michael@0: PRIntervalTime start, stop; michael@0: double d; michael@0: PRInt32 tot; michael@0: michael@0: start = PR_IntervalNow(); michael@0: (*func)(); michael@0: stop = PR_IntervalNow(); michael@0: michael@0: d = (double)PR_IntervalToMicroseconds(stop - start); michael@0: tot = PR_IntervalToMilliseconds(stop-start); michael@0: michael@0: if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: michael@0: /* The command line argument: -d is used to determine if the test is being run michael@0: in debug mode. The regress tool requires only one line output:PASS or FAIL. michael@0: All of the printfs associated with this test has been handled with a if (debug_mode) michael@0: test. michael@0: Usage: test_name -d michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug mode */ michael@0: debug_mode = 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: /* main test */ michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: PR_STDIO_INIT(); michael@0: michael@0: if (argc > 2) { michael@0: count = atoi(argv[2]); michael@0: } else { michael@0: count = DEFAULT_COUNT; michael@0: } michael@0: michael@0: #if defined(XP_UNIX) michael@0: Measure(NativeSelectTest, "time to call 1 element select()"); michael@0: #endif michael@0: Measure(EmptyPRSelect, "time to call Empty PR_select()"); michael@0: Measure(EmptyNativeSelect, "time to call Empty select()"); michael@0: Measure(PRSelectTest, "time to call 1 element PR_select()"); michael@0: michael@0: if (!debug_mode) Test_Result (NOSTATUS); michael@0: PR_Cleanup(); michael@0: michael@0: michael@0: }