Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /*********************************************************************** |
michael@0 | 7 | ** |
michael@0 | 8 | ** Name: select2.c |
michael@0 | 9 | ** |
michael@0 | 10 | ** Description: Measure PR_Select and Empty_Select performance. |
michael@0 | 11 | ** |
michael@0 | 12 | ** Modification History: |
michael@0 | 13 | ** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. |
michael@0 | 14 | ** The debug mode will print all of the printfs associated with this test. |
michael@0 | 15 | ** The regress mode will be the default mode. Since the regress tool limits |
michael@0 | 16 | ** the output to a one line status:PASS or FAIL,all of the printf statements |
michael@0 | 17 | ** have been handled with an if (debug_mode) statement. |
michael@0 | 18 | ***********************************************************************/ |
michael@0 | 19 | |
michael@0 | 20 | /*********************************************************************** |
michael@0 | 21 | ** Includes |
michael@0 | 22 | ***********************************************************************/ |
michael@0 | 23 | /* Used to get the command line option */ |
michael@0 | 24 | #include "plgetopt.h" |
michael@0 | 25 | #include "prttools.h" |
michael@0 | 26 | #include "primpl.h" |
michael@0 | 27 | |
michael@0 | 28 | #include <stdio.h> |
michael@0 | 29 | #include <stdlib.h> |
michael@0 | 30 | #include <string.h> |
michael@0 | 31 | #if defined(OS2) |
michael@0 | 32 | #include <sys/time.h> |
michael@0 | 33 | #endif |
michael@0 | 34 | |
michael@0 | 35 | #define PORT 8000 |
michael@0 | 36 | #define DEFAULT_COUNT 10 |
michael@0 | 37 | PRInt32 count; |
michael@0 | 38 | |
michael@0 | 39 | |
michael@0 | 40 | /*********************************************************************** |
michael@0 | 41 | ** PRIVATE FUNCTION: Test_Result |
michael@0 | 42 | ** DESCRIPTION: Used in conjunction with the regress tool, prints out the |
michael@0 | 43 | ** status of the test case. |
michael@0 | 44 | ** INPUTS: PASS/FAIL |
michael@0 | 45 | ** OUTPUTS: None |
michael@0 | 46 | ** RETURN: None |
michael@0 | 47 | ** SIDE EFFECTS: |
michael@0 | 48 | ** |
michael@0 | 49 | ** RESTRICTIONS: |
michael@0 | 50 | ** None |
michael@0 | 51 | ** MEMORY: NA |
michael@0 | 52 | ** ALGORITHM: Determine what the status is and print accordingly. |
michael@0 | 53 | ** |
michael@0 | 54 | ***********************************************************************/ |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | static void Test_Result (int result) |
michael@0 | 58 | { |
michael@0 | 59 | switch (result) |
michael@0 | 60 | { |
michael@0 | 61 | case PASS: |
michael@0 | 62 | printf ("PASS\n"); |
michael@0 | 63 | break; |
michael@0 | 64 | case FAIL: |
michael@0 | 65 | printf ("FAIL\n"); |
michael@0 | 66 | break; |
michael@0 | 67 | default: |
michael@0 | 68 | printf ("NOSTATUS\n"); |
michael@0 | 69 | break; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | static void EmptyPRSelect(void) |
michael@0 | 74 | { |
michael@0 | 75 | PRInt32 index = count; |
michael@0 | 76 | PRInt32 rv; |
michael@0 | 77 | |
michael@0 | 78 | for (; index--;) |
michael@0 | 79 | rv = PR_Select(0, NULL, NULL, NULL, PR_INTERVAL_NO_WAIT); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | static void EmptyNativeSelect(void) |
michael@0 | 83 | { |
michael@0 | 84 | PRInt32 rv; |
michael@0 | 85 | PRInt32 index = count; |
michael@0 | 86 | struct timeval timeout; |
michael@0 | 87 | |
michael@0 | 88 | timeout.tv_sec = timeout.tv_usec = 0; |
michael@0 | 89 | for (; index--;) |
michael@0 | 90 | rv = select(0, NULL, NULL, NULL, &timeout); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | static void PRSelectTest(void) |
michael@0 | 94 | { |
michael@0 | 95 | PRFileDesc *listenSocket; |
michael@0 | 96 | PRNetAddr serverAddr; |
michael@0 | 97 | |
michael@0 | 98 | if ( (listenSocket = PR_NewTCPSocket()) == NULL) { |
michael@0 | 99 | if (debug_mode) printf("\tServer error creating listen socket\n"); |
michael@0 | 100 | return; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | memset(&serverAddr, 0, sizeof(PRNetAddr)); |
michael@0 | 104 | serverAddr.inet.family = AF_INET; |
michael@0 | 105 | serverAddr.inet.port = PR_htons(PORT); |
michael@0 | 106 | serverAddr.inet.ip = PR_htonl(INADDR_ANY); |
michael@0 | 107 | |
michael@0 | 108 | if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { |
michael@0 | 109 | if (debug_mode) printf("\tServer error binding to server address\n"); |
michael@0 | 110 | PR_Close(listenSocket); |
michael@0 | 111 | return; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { |
michael@0 | 115 | if (debug_mode) printf("\tServer error listening to server socket\n"); |
michael@0 | 116 | PR_Close(listenSocket); |
michael@0 | 117 | return; |
michael@0 | 118 | } |
michael@0 | 119 | if (debug_mode) printf("Listening on port %d\n", PORT); |
michael@0 | 120 | |
michael@0 | 121 | { |
michael@0 | 122 | PRFileDesc *newSock; |
michael@0 | 123 | PRNetAddr rAddr; |
michael@0 | 124 | PRInt32 loops = 0; |
michael@0 | 125 | PR_fd_set rdset; |
michael@0 | 126 | PRInt32 rv; |
michael@0 | 127 | PRInt32 bytesRead; |
michael@0 | 128 | char buf[11]; |
michael@0 | 129 | |
michael@0 | 130 | loops++; |
michael@0 | 131 | |
michael@0 | 132 | if (debug_mode) printf("Going into accept\n"); |
michael@0 | 133 | |
michael@0 | 134 | newSock = PR_Accept(listenSocket, |
michael@0 | 135 | &rAddr, |
michael@0 | 136 | PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 137 | |
michael@0 | 138 | if (newSock) { |
michael@0 | 139 | if (debug_mode) printf("Got connection!\n"); |
michael@0 | 140 | } else { |
michael@0 | 141 | if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError()); |
michael@0 | 142 | else Test_Result (FAIL); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | PR_FD_ZERO(&rdset); |
michael@0 | 146 | PR_FD_SET(newSock, &rdset); |
michael@0 | 147 | |
michael@0 | 148 | if (debug_mode) printf("Going into select \n"); |
michael@0 | 149 | |
michael@0 | 150 | rv = PR_Select(0, &rdset, 0, 0, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 151 | |
michael@0 | 152 | if (debug_mode) printf("return from select is %d\n", rv); |
michael@0 | 153 | |
michael@0 | 154 | if (PR_FD_ISSET(newSock, &rdset)) { |
michael@0 | 155 | if (debug_mode) printf("I can't believe it- the socket is ready okay!\n"); |
michael@0 | 156 | } else { |
michael@0 | 157 | if (debug_mode) printf("Damn; the select test failed...\n"); |
michael@0 | 158 | else Test_Result (FAIL); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | strcpy(buf, "XXXXXXXXXX"); |
michael@0 | 162 | bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 163 | buf[10] = '\0'; |
michael@0 | 164 | |
michael@0 | 165 | if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf); |
michael@0 | 166 | |
michael@0 | 167 | PR_Close(newSock); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | #if defined(XP_UNIX) |
michael@0 | 173 | |
michael@0 | 174 | static void NativeSelectTest(void) |
michael@0 | 175 | { |
michael@0 | 176 | PRFileDesc *listenSocket; |
michael@0 | 177 | PRNetAddr serverAddr; |
michael@0 | 178 | |
michael@0 | 179 | if ( (listenSocket = PR_NewTCPSocket()) == NULL) { |
michael@0 | 180 | if (debug_mode) printf("\tServer error creating listen socket\n"); |
michael@0 | 181 | return; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | memset(&serverAddr, 0, sizeof(PRNetAddr)); |
michael@0 | 185 | serverAddr.inet.family = AF_INET; |
michael@0 | 186 | serverAddr.inet.port = PR_htons(PORT); |
michael@0 | 187 | serverAddr.inet.ip = PR_htonl(INADDR_ANY); |
michael@0 | 188 | |
michael@0 | 189 | if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { |
michael@0 | 190 | if (debug_mode) printf("\tServer error binding to server address\n"); |
michael@0 | 191 | PR_Close(listenSocket); |
michael@0 | 192 | return; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { |
michael@0 | 196 | if (debug_mode) printf("\tServer error listening to server socket\n"); |
michael@0 | 197 | PR_Close(listenSocket); |
michael@0 | 198 | return; |
michael@0 | 199 | } |
michael@0 | 200 | if (debug_mode) printf("Listening on port %d\n", PORT); |
michael@0 | 201 | |
michael@0 | 202 | { |
michael@0 | 203 | PRIntn osfd; |
michael@0 | 204 | char buf[11]; |
michael@0 | 205 | fd_set rdset; |
michael@0 | 206 | PRNetAddr rAddr; |
michael@0 | 207 | PRFileDesc *newSock; |
michael@0 | 208 | struct timeval timeout; |
michael@0 | 209 | PRInt32 bytesRead, rv, loops = 0; |
michael@0 | 210 | |
michael@0 | 211 | loops++; |
michael@0 | 212 | |
michael@0 | 213 | if (debug_mode) printf("Going into accept\n"); |
michael@0 | 214 | |
michael@0 | 215 | newSock = PR_Accept(listenSocket, &rAddr, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 216 | |
michael@0 | 217 | if (newSock) { |
michael@0 | 218 | if (debug_mode) printf("Got connection!\n"); |
michael@0 | 219 | } else { |
michael@0 | 220 | if (debug_mode) printf("PR_Accept failed: error code %d\n", PR_GetError()); |
michael@0 | 221 | else Test_Result (FAIL); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | osfd = PR_FileDesc2NativeHandle(newSock); |
michael@0 | 225 | FD_ZERO(&rdset); |
michael@0 | 226 | FD_SET(osfd, &rdset); |
michael@0 | 227 | |
michael@0 | 228 | if (debug_mode) printf("Going into select \n"); |
michael@0 | 229 | |
michael@0 | 230 | |
michael@0 | 231 | timeout.tv_sec = 2; timeout.tv_usec = 0; |
michael@0 | 232 | rv = select(osfd + 1, &rdset, NULL, NULL, &timeout); |
michael@0 | 233 | |
michael@0 | 234 | if (debug_mode) printf("return from select is %d\n", rv); |
michael@0 | 235 | |
michael@0 | 236 | if (FD_ISSET(osfd, &rdset)) { |
michael@0 | 237 | if (debug_mode) |
michael@0 | 238 | printf("I can't believe it- the socket is ready okay!\n"); |
michael@0 | 239 | } else { |
michael@0 | 240 | if (debug_mode) printf("Damn; the select test failed...\n"); |
michael@0 | 241 | else Test_Result (FAIL); |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | strcpy(buf, "XXXXXXXXXX"); |
michael@0 | 245 | bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 246 | buf[10] = '\0'; |
michael@0 | 247 | |
michael@0 | 248 | if (debug_mode) printf("Recv completed with %d bytes, %s\n", bytesRead, buf); |
michael@0 | 249 | |
michael@0 | 250 | PR_Close(newSock); |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | } /* NativeSelectTest */ |
michael@0 | 254 | |
michael@0 | 255 | #endif /* defined(XP_UNIX) */ |
michael@0 | 256 | |
michael@0 | 257 | /************************************************************************/ |
michael@0 | 258 | |
michael@0 | 259 | static void Measure(void (*func)(void), const char *msg) |
michael@0 | 260 | { |
michael@0 | 261 | PRIntervalTime start, stop; |
michael@0 | 262 | double d; |
michael@0 | 263 | PRInt32 tot; |
michael@0 | 264 | |
michael@0 | 265 | start = PR_IntervalNow(); |
michael@0 | 266 | (*func)(); |
michael@0 | 267 | stop = PR_IntervalNow(); |
michael@0 | 268 | |
michael@0 | 269 | d = (double)PR_IntervalToMicroseconds(stop - start); |
michael@0 | 270 | tot = PR_IntervalToMilliseconds(stop-start); |
michael@0 | 271 | |
michael@0 | 272 | if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot); |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | int main(int argc, char **argv) |
michael@0 | 276 | { |
michael@0 | 277 | |
michael@0 | 278 | /* The command line argument: -d is used to determine if the test is being run |
michael@0 | 279 | in debug mode. The regress tool requires only one line output:PASS or FAIL. |
michael@0 | 280 | All of the printfs associated with this test has been handled with a if (debug_mode) |
michael@0 | 281 | test. |
michael@0 | 282 | Usage: test_name -d |
michael@0 | 283 | */ |
michael@0 | 284 | PLOptStatus os; |
michael@0 | 285 | PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); |
michael@0 | 286 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
michael@0 | 287 | { |
michael@0 | 288 | if (PL_OPT_BAD == os) continue; |
michael@0 | 289 | switch (opt->option) |
michael@0 | 290 | { |
michael@0 | 291 | case 'd': /* debug mode */ |
michael@0 | 292 | debug_mode = 1; |
michael@0 | 293 | break; |
michael@0 | 294 | default: |
michael@0 | 295 | break; |
michael@0 | 296 | } |
michael@0 | 297 | } |
michael@0 | 298 | PL_DestroyOptState(opt); |
michael@0 | 299 | |
michael@0 | 300 | /* main test */ |
michael@0 | 301 | |
michael@0 | 302 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
michael@0 | 303 | PR_STDIO_INIT(); |
michael@0 | 304 | |
michael@0 | 305 | if (argc > 2) { |
michael@0 | 306 | count = atoi(argv[2]); |
michael@0 | 307 | } else { |
michael@0 | 308 | count = DEFAULT_COUNT; |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | #if defined(XP_UNIX) |
michael@0 | 312 | Measure(NativeSelectTest, "time to call 1 element select()"); |
michael@0 | 313 | #endif |
michael@0 | 314 | Measure(EmptyPRSelect, "time to call Empty PR_select()"); |
michael@0 | 315 | Measure(EmptyNativeSelect, "time to call Empty select()"); |
michael@0 | 316 | Measure(PRSelectTest, "time to call 1 element PR_select()"); |
michael@0 | 317 | |
michael@0 | 318 | if (!debug_mode) Test_Result (NOSTATUS); |
michael@0 | 319 | PR_Cleanup(); |
michael@0 | 320 | |
michael@0 | 321 | |
michael@0 | 322 | } |