home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / select2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.3 KB  |  333 lines

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