home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / inrval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.3 KB  |  245 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. ** file:            inrval.c
  21. ** description:     Interval conversion test.
  22. ** Modification History:
  23. ** 15-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  24. **             The debug mode will print all of the printfs associated with this test.
  25. **             The regress mode will be the default mode. Since the regress tool limits
  26. **           the output to a one line status:PASS or FAIL,all of the printf statements
  27. **             have been handled with an if (debug_mode) statement.
  28. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  29. **            recognize the return code from tha main program.
  30. **/
  31. /***********************************************************************
  32. ** Includes
  33. ***********************************************************************/
  34. /* Used to get the command line option */
  35. #include "plgetopt.h"
  36.  
  37. #include "prinit.h"
  38. #ifdef XP_MAC
  39. #include "pralarm.h"
  40. #else
  41. #include "obsolete/pralarm.h"
  42. #endif
  43. #include "prlock.h"
  44. #include "prlong.h"
  45. #include "prcvar.h"
  46. #include "prinrval.h"
  47. #include "prtime.h"
  48.  
  49. #include "plgetopt.h"
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53.  
  54. #ifdef XP_MAC
  55. #include "prlog.h"
  56. #define printf PR_LogPrint
  57. extern void SetupMacPrintfLog(char *logFile);
  58. #endif
  59.  
  60. PRIntn failed_already=0;
  61. PRIntn debug_mode;
  62.  
  63.  
  64. static void TestConversions(void)
  65. {
  66.     PRIntervalTime ticks = PR_TicksPerSecond();
  67.  
  68.     if (debug_mode) {
  69.     printf("PR_TicksPerSecond: %ld\n\n", ticks);
  70.     printf("PR_SecondsToInterval(1): %ld\n", PR_SecondsToInterval(1));
  71.     printf("PR_MillisecondsToInterval(1000): %ld\n", PR_MillisecondsToInterval(1000));
  72.     printf("PR_MicrosecondsToInterval(1000000): %ld\n\n", PR_MicrosecondsToInterval(1000000));
  73.  
  74.     printf("PR_SecondsToInterval(3): %ld\n", PR_SecondsToInterval(3));
  75.     printf("PR_MillisecondsToInterval(3000): %ld\n", PR_MillisecondsToInterval(3000));
  76.     printf("PR_MicrosecondsToInterval(3000000): %ld\n\n", PR_MicrosecondsToInterval(3000000));
  77.  
  78.     printf("PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks));
  79.     printf("PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks));
  80.     printf("PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks));
  81.  
  82.     ticks *= 3;
  83.     printf("PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks));
  84.     printf("PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks));
  85.     printf("PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks));
  86.     } /*end debug mode */
  87. }  /* TestConversions */
  88.  
  89. static void TestIntervals(void)
  90. {
  91.     PRStatus rv;
  92.     PRUint32 delta;
  93.     PRInt32 seconds;
  94.     PRUint64 elapsed, thousand;
  95.     PRTime timein, timeout;
  96.     PRLock *ml = PR_NewLock();
  97.     PRCondVar *cv = PR_NewCondVar(ml);
  98.     for (seconds = 0; seconds < 10; ++seconds)
  99.     {
  100.         PRIntervalTime ticks = PR_SecondsToInterval(seconds);
  101.         PR_Lock(ml);
  102.         timein = PR_Now();
  103.         rv = PR_WaitCondVar(cv, ticks);
  104.         timeout = PR_Now();
  105.         PR_Unlock(ml);
  106.         LL_SUB(elapsed, timeout, timein);
  107.         LL_I2L(thousand, 1000);
  108.         LL_DIV(elapsed, elapsed, thousand);
  109.         LL_L2UI(delta, elapsed);
  110.         if (debug_mode) printf(
  111.             "TestIntervals: %swaiting %ld seconds took %ld msecs\n",
  112.             ((rv == PR_SUCCESS) ? "" : "FAILED "), seconds, delta);
  113.     }
  114.     PR_DestroyCondVar(cv);
  115.     PR_DestroyLock(ml);
  116.     if (debug_mode) printf("\n");
  117. }  /* TestIntervals */
  118.  
  119. static PRUint32 GetInterval(PRUint32 loops)
  120. {
  121.     PRIntervalTime interval = 0;
  122.     while (loops-- > 0) interval += PR_IntervalNow();
  123.     return 0;
  124. }  /* GetInterval */
  125.  
  126. static PRUint32 TimeThis(
  127.     const char *msg, PRUint32 (*func)(PRUint32 loops), PRUint32 loops)
  128. {
  129.     PRUint32 overhead, usecs32;
  130.     PRTime timein, timeout, usecs;
  131.  
  132.     timein = PR_Now();
  133.     overhead = func(loops);
  134.     timeout = PR_Now();
  135.  
  136.     LL_SUB(usecs, timeout, timein);
  137.     LL_L2I(usecs32, usecs);
  138.    
  139.     if(usecs32 < overhead)
  140.     {
  141.         if (debug_mode) {
  142.             printf("%s ran in negative time\n", msg);
  143.             printf("  predicted overhead was %ld usecs\n", overhead);
  144.             printf("  test completed in %ld usecs\n\n", usecs);
  145.         }
  146.     }
  147.     else
  148.     {
  149.         if (debug_mode)
  150.             printf(
  151.             "%s [\n\ttotal: %ld usecs\n\toverhead: %ld usecs\n\tcost: %6.3f usecs]\n\n",
  152.             msg, usecs, overhead,
  153.             ((double)(usecs32 - overhead) / (double)loops));
  154.     }
  155.  
  156.     return overhead;
  157. }  /* TimeThis */
  158.  
  159. static PRIntn PR_CALLBACK RealMain(int argc, char** argv)
  160. {
  161.     PRUint32 cpu, cpus = 2, loops = 1000;
  162.  
  163.     /* The command line argument: -d is used to determine if the test is being run
  164.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  165.     All of the printfs associated with this test has been handled with a if (debug_mode)
  166.     test.
  167.     Usage: test_name -d
  168.     */
  169.  
  170.  /* main test */
  171.     
  172.     PLOptStatus os;
  173.     PLOptState *opt = PL_CreateOptState(argc, argv, "dl:c:");
  174.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  175.     {
  176.         if (PL_OPT_BAD == os) continue;
  177.         switch (opt->option)
  178.         {
  179.         case 'd':  /* debug mode */
  180.             debug_mode = 1;
  181.             break;
  182.         case 'c':  /* concurrency counter */
  183.             cpus = atoi(opt->value);
  184.             break;
  185.         case 'l':  /* loop counter */
  186.             loops = atoi(opt->value);
  187.             break;
  188.          default:
  189.             break;
  190.         }
  191.     }
  192.     PL_DestroyOptState(opt);
  193.     
  194.  
  195.     printf("inrval: Examine stdout to determine results.\n");
  196.  
  197.     if (cpus == 0) cpus = 2;
  198.     if (loops == 0) loops = 1000;
  199.     if (debug_mode) printf("Inrval: Using %d loops\n", loops);
  200.  
  201.  
  202.     cpus = (argc < 3) ? 2 : atoi(argv[2]);
  203.     if (cpus == 0) cpus = 2;
  204.     if (debug_mode) printf("Inrval: Using %d cpu(s)\n", cpus);
  205.  
  206.     if (debug_mode > 0)
  207.     {
  208.         printf("Inrval: Using %d loops\n", loops);
  209.         printf("Inrval: Using %d cpu(s)\n", cpus);
  210.     }
  211.  
  212. #ifdef XP_MAC
  213.     SetupMacPrintfLog("inrval.log");
  214.     debug_mode = 1;
  215. #endif
  216.  
  217.     for (cpu = 1; cpu <= cpus; ++cpu)
  218.     {
  219.  
  220.         if (debug_mode) printf("\nInrval: Using %d CPU(s)\n", cpu);
  221.         if (debug_mode > 0)
  222.             printf("\nInrval: Using %d CPU(s)\n", cpu);
  223.  
  224.         PR_SetConcurrency(cpu);
  225.  
  226.         TestConversions();
  227.         TestIntervals();
  228.  
  229.         (void)TimeThis("GetInterval", GetInterval, loops);
  230.     }
  231.         
  232.     return 0;
  233. }
  234.  
  235.  
  236. PRIntn main(PRIntn argc, char *argv[])
  237. {
  238.     PRIntn rv;
  239.     
  240.     PR_STDIO_INIT();
  241.     rv = PR_Initialize(RealMain, argc, argv, 0);
  242.     return rv;
  243. }  /* main */
  244.  
  245.