1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/select2.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,322 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/*********************************************************************** 1.10 +** 1.11 +** Name: select2.c 1.12 +** 1.13 +** Description: Measure PR_Select and Empty_Select performance. 1.14 +** 1.15 +** Modification History: 1.16 +** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. 1.17 +** The debug mode will print all of the printfs associated with this test. 1.18 +** The regress mode will be the default mode. Since the regress tool limits 1.19 +** the output to a one line status:PASS or FAIL,all of the printf statements 1.20 +** have been handled with an if (debug_mode) statement. 1.21 +***********************************************************************/ 1.22 + 1.23 +/*********************************************************************** 1.24 +** Includes 1.25 +***********************************************************************/ 1.26 +/* Used to get the command line option */ 1.27 +#include "plgetopt.h" 1.28 +#include "prttools.h" 1.29 +#include "primpl.h" 1.30 + 1.31 +#include <stdio.h> 1.32 +#include <stdlib.h> 1.33 +#include <string.h> 1.34 +#if defined(OS2) 1.35 +#include <sys/time.h> 1.36 +#endif 1.37 + 1.38 +#define PORT 8000 1.39 +#define DEFAULT_COUNT 10 1.40 +PRInt32 count; 1.41 + 1.42 + 1.43 +/*********************************************************************** 1.44 +** PRIVATE FUNCTION: Test_Result 1.45 +** DESCRIPTION: Used in conjunction with the regress tool, prints out the 1.46 +** status of the test case. 1.47 +** INPUTS: PASS/FAIL 1.48 +** OUTPUTS: None 1.49 +** RETURN: None 1.50 +** SIDE EFFECTS: 1.51 +** 1.52 +** RESTRICTIONS: 1.53 +** None 1.54 +** MEMORY: NA 1.55 +** ALGORITHM: Determine what the status is and print accordingly. 1.56 +** 1.57 +***********************************************************************/ 1.58 + 1.59 + 1.60 +static void Test_Result (int result) 1.61 +{ 1.62 + switch (result) 1.63 + { 1.64 + case PASS: 1.65 + printf ("PASS\n"); 1.66 + break; 1.67 + case FAIL: 1.68 + printf ("FAIL\n"); 1.69 + break; 1.70 + default: 1.71 + printf ("NOSTATUS\n"); 1.72 + break; 1.73 + } 1.74 +} 1.75 + 1.76 +static void EmptyPRSelect(void) 1.77 +{ 1.78 + PRInt32 index = count; 1.79 + PRInt32 rv; 1.80 + 1.81 + for (; index--;) 1.82 + rv = PR_Select(0, NULL, NULL, NULL, PR_INTERVAL_NO_WAIT); 1.83 +} 1.84 + 1.85 +static void EmptyNativeSelect(void) 1.86 +{ 1.87 + PRInt32 rv; 1.88 + PRInt32 index = count; 1.89 + struct timeval timeout; 1.90 + 1.91 + timeout.tv_sec = timeout.tv_usec = 0; 1.92 + for (; index--;) 1.93 + rv = select(0, NULL, NULL, NULL, &timeout); 1.94 +} 1.95 + 1.96 +static void PRSelectTest(void) 1.97 +{ 1.98 + PRFileDesc *listenSocket; 1.99 + PRNetAddr serverAddr; 1.100 + 1.101 + if ( (listenSocket = PR_NewTCPSocket()) == NULL) { 1.102 + if (debug_mode) printf("\tServer error creating listen socket\n"); 1.103 + return; 1.104 + } 1.105 + 1.106 + memset(&serverAddr, 0, sizeof(PRNetAddr)); 1.107 + serverAddr.inet.family = AF_INET; 1.108 + serverAddr.inet.port = PR_htons(PORT); 1.109 + serverAddr.inet.ip = PR_htonl(INADDR_ANY); 1.110 + 1.111 + if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { 1.112 + if (debug_mode) printf("\tServer error binding to server address\n"); 1.113 + PR_Close(listenSocket); 1.114 + return; 1.115 + } 1.116 + 1.117 + if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { 1.118 + if (debug_mode) printf("\tServer error listening to server socket\n"); 1.119 + PR_Close(listenSocket); 1.120 + return; 1.121 + } 1.122 + if (debug_mode) printf("Listening on port %d\n", PORT); 1.123 + 1.124 + { 1.125 + PRFileDesc *newSock; 1.126 + PRNetAddr rAddr; 1.127 + PRInt32 loops = 0; 1.128 + PR_fd_set rdset; 1.129 + PRInt32 rv; 1.130 + PRInt32 bytesRead; 1.131 + char buf[11]; 1.132 + 1.133 + loops++; 1.134 + 1.135 + if (debug_mode) printf("Going into accept\n"); 1.136 + 1.137 + newSock = PR_Accept(listenSocket, 1.138 + &rAddr, 1.139 + PR_INTERVAL_NO_TIMEOUT); 1.140 + 1.141 + if (newSock) { 1.142 + if (debug_mode) printf("Got connection!\n"); 1.143 + } else { 1.144 + if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError()); 1.145 + else Test_Result (FAIL); 1.146 + } 1.147 + 1.148 + PR_FD_ZERO(&rdset); 1.149 + PR_FD_SET(newSock, &rdset); 1.150 + 1.151 + if (debug_mode) printf("Going into select \n"); 1.152 + 1.153 + rv = PR_Select(0, &rdset, 0, 0, PR_INTERVAL_NO_TIMEOUT); 1.154 + 1.155 + if (debug_mode) printf("return from select is %d\n", rv); 1.156 + 1.157 + if (PR_FD_ISSET(newSock, &rdset)) { 1.158 + if (debug_mode) printf("I can't believe it- the socket is ready okay!\n"); 1.159 + } else { 1.160 + if (debug_mode) printf("Damn; the select test failed...\n"); 1.161 + else Test_Result (FAIL); 1.162 + } 1.163 + 1.164 + strcpy(buf, "XXXXXXXXXX"); 1.165 + bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT); 1.166 + buf[10] = '\0'; 1.167 + 1.168 + if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf); 1.169 + 1.170 + PR_Close(newSock); 1.171 + } 1.172 + 1.173 +} 1.174 + 1.175 +#if defined(XP_UNIX) 1.176 + 1.177 +static void NativeSelectTest(void) 1.178 +{ 1.179 + PRFileDesc *listenSocket; 1.180 + PRNetAddr serverAddr; 1.181 + 1.182 + if ( (listenSocket = PR_NewTCPSocket()) == NULL) { 1.183 + if (debug_mode) printf("\tServer error creating listen socket\n"); 1.184 + return; 1.185 + } 1.186 + 1.187 + memset(&serverAddr, 0, sizeof(PRNetAddr)); 1.188 + serverAddr.inet.family = AF_INET; 1.189 + serverAddr.inet.port = PR_htons(PORT); 1.190 + serverAddr.inet.ip = PR_htonl(INADDR_ANY); 1.191 + 1.192 + if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { 1.193 + if (debug_mode) printf("\tServer error binding to server address\n"); 1.194 + PR_Close(listenSocket); 1.195 + return; 1.196 + } 1.197 + 1.198 + if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { 1.199 + if (debug_mode) printf("\tServer error listening to server socket\n"); 1.200 + PR_Close(listenSocket); 1.201 + return; 1.202 + } 1.203 + if (debug_mode) printf("Listening on port %d\n", PORT); 1.204 + 1.205 + { 1.206 + PRIntn osfd; 1.207 + char buf[11]; 1.208 + fd_set rdset; 1.209 + PRNetAddr rAddr; 1.210 + PRFileDesc *newSock; 1.211 + struct timeval timeout; 1.212 + PRInt32 bytesRead, rv, loops = 0; 1.213 + 1.214 + loops++; 1.215 + 1.216 + if (debug_mode) printf("Going into accept\n"); 1.217 + 1.218 + newSock = PR_Accept(listenSocket, &rAddr, PR_INTERVAL_NO_TIMEOUT); 1.219 + 1.220 + if (newSock) { 1.221 + if (debug_mode) printf("Got connection!\n"); 1.222 + } else { 1.223 + if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError()); 1.224 + else Test_Result (FAIL); 1.225 + } 1.226 + 1.227 + osfd = PR_FileDesc2NativeHandle(newSock); 1.228 + FD_ZERO(&rdset); 1.229 + FD_SET(osfd, &rdset); 1.230 + 1.231 + if (debug_mode) printf("Going into select \n"); 1.232 + 1.233 + 1.234 + timeout.tv_sec = 2; timeout.tv_usec = 0; 1.235 + rv = select(osfd + 1, &rdset, NULL, NULL, &timeout); 1.236 + 1.237 + if (debug_mode) printf("return from select is %d\n", rv); 1.238 + 1.239 + if (FD_ISSET(osfd, &rdset)) { 1.240 + if (debug_mode) 1.241 + printf("I can't believe it- the socket is ready okay!\n"); 1.242 + } else { 1.243 + if (debug_mode) printf("Damn; the select test failed...\n"); 1.244 + else Test_Result (FAIL); 1.245 + } 1.246 + 1.247 + strcpy(buf, "XXXXXXXXXX"); 1.248 + bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT); 1.249 + buf[10] = '\0'; 1.250 + 1.251 + if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf); 1.252 + 1.253 + PR_Close(newSock); 1.254 + } 1.255 + 1.256 +} /* NativeSelectTest */ 1.257 + 1.258 +#endif /* defined(XP_UNIX) */ 1.259 + 1.260 +/************************************************************************/ 1.261 + 1.262 +static void Measure(void (*func)(void), const char *msg) 1.263 +{ 1.264 + PRIntervalTime start, stop; 1.265 + double d; 1.266 + PRInt32 tot; 1.267 + 1.268 + start = PR_IntervalNow(); 1.269 + (*func)(); 1.270 + stop = PR_IntervalNow(); 1.271 + 1.272 + d = (double)PR_IntervalToMicroseconds(stop - start); 1.273 + tot = PR_IntervalToMilliseconds(stop-start); 1.274 + 1.275 + if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot); 1.276 +} 1.277 + 1.278 +int main(int argc, char **argv) 1.279 +{ 1.280 + 1.281 + /* The command line argument: -d is used to determine if the test is being run 1.282 + in debug mode. The regress tool requires only one line output:PASS or FAIL. 1.283 + All of the printfs associated with this test has been handled with a if (debug_mode) 1.284 + test. 1.285 + Usage: test_name -d 1.286 + */ 1.287 + PLOptStatus os; 1.288 + PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); 1.289 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.290 + { 1.291 + if (PL_OPT_BAD == os) continue; 1.292 + switch (opt->option) 1.293 + { 1.294 + case 'd': /* debug mode */ 1.295 + debug_mode = 1; 1.296 + break; 1.297 + default: 1.298 + break; 1.299 + } 1.300 + } 1.301 + PL_DestroyOptState(opt); 1.302 + 1.303 + /* main test */ 1.304 + 1.305 + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 1.306 + PR_STDIO_INIT(); 1.307 + 1.308 + if (argc > 2) { 1.309 + count = atoi(argv[2]); 1.310 + } else { 1.311 + count = DEFAULT_COUNT; 1.312 + } 1.313 + 1.314 +#if defined(XP_UNIX) 1.315 + Measure(NativeSelectTest, "time to call 1 element select()"); 1.316 +#endif 1.317 + Measure(EmptyPRSelect, "time to call Empty PR_select()"); 1.318 + Measure(EmptyNativeSelect, "time to call Empty select()"); 1.319 + Measure(PRSelectTest, "time to call 1 element PR_select()"); 1.320 + 1.321 + if (!debug_mode) Test_Result (NOSTATUS); 1.322 + PR_Cleanup(); 1.323 + 1.324 + 1.325 +}