nsprpub/pr/tests/time.c

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /*
     7  * Program to test different ways to get the time; right now it is tuned
     8  * only for solaris.
     9  *   solaris results (100000 iterations):
    10  *          time to get time with time():   4.63 usec avg, 463 msec total
    11  *     time to get time with gethrtime():   2.17 usec avg, 217 msec total
    12  *  time to get time with gettimeofday():   1.25 usec avg, 125 msec total
    13  *
    14  *
    15  */
    16 /***********************************************************************
    17 ** Includes
    18 ***********************************************************************/
    19 /* Used to get the command line option */
    20 #include "plgetopt.h"
    22 #include "nspr.h"
    23 #include "prpriv.h"
    24 #include "prinrval.h"
    26 #include <stdio.h>
    27 #include <stdlib.h>
    28 #include <string.h>
    29 #include <sys/time.h>
    31 #define DEFAULT_COUNT 100000
    32 PRInt32 count;
    34 time_t itime;
    35 hrtime_t ihrtime;
    37 void
    38 ftime_init()
    39 {
    40    itime = time(NULL);
    41    ihrtime = gethrtime();
    42 }
    44 time_t
    45 ftime()
    46 {
    47         hrtime_t now = gethrtime();
    49         return itime + ((now - ihrtime) / 1000000000ll);
    50 }
    52 static void timeTime(void)
    53 {
    54     PRInt32 index = count;
    55     time_t rv;
    57     for (;index--;)
    58         rv = time(NULL);
    59 }
    61 static void timeGethrtime(void)
    62 {
    63     PRInt32 index = count;
    64     time_t rv;
    66     for (;index--;)
    67         rv = ftime();
    68 }
    70 static void timeGettimeofday(void)
    71 {
    72     PRInt32 index = count;
    73     time_t rv;
    74     struct timeval tp;
    76     for (;index--;)
    77         rv = gettimeofday(&tp, NULL);
    78 }
    80 static void timePRTime32(void)
    81 {
    82     PRInt32 index = count;
    83     PRInt32 rv32;
    84     PRTime q;
    85     PRTime rv;
    87     LL_I2L(q, 1000000);
    89     for (;index--;) {
    90         rv = PR_Now();
    91         LL_DIV(rv, rv, q);
    92         LL_L2I(rv32, rv);
    93     }
    94 }
    96 static void timePRTime64(void)
    97 {
    98     PRInt32 index = count;
    99     PRTime rv;
   101     for (;index--;)
   102         rv = PR_Now();
   103 }
   105 /************************************************************************/
   107 static void Measure(void (*func)(void), const char *msg)
   108 {
   109     PRIntervalTime start, stop;
   110     double d;
   111     PRInt32 tot;
   113     start = PR_IntervalNow();
   114     (*func)();
   115     stop = PR_IntervalNow();
   117     d = (double)PR_IntervalToMicroseconds(stop - start);
   118     tot = PR_IntervalToMilliseconds(stop-start);
   120     if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
   121 }
   123 int main(int argc, char **argv)
   124 {
   125 	/* The command line argument: -d is used to determine if the test is being run
   126 	in debug mode. The regress tool requires only one line output:PASS or FAIL.
   127 	All of the printfs associated with this test has been handled with a if (debug_mode)
   128 	test.
   129 	Usage: test_name -d
   130 	*/
   131 	PLOptStatus os;
   132 	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
   133 	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   134     {
   135 		if (PL_OPT_BAD == os) continue;
   136         switch (opt->option)
   137         {
   138         case 'd':  /* debug mode */
   139 			debug_mode = 1;
   140             break;
   141          default:
   142             break;
   143         }
   144     }
   145 	PL_DestroyOptState(opt);
   147     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   148     PR_STDIO_INIT();
   150     if (argc > 1) {
   151 	count = atoi(argv[1]);
   152     } else {
   153 	count = DEFAULT_COUNT;
   154     }
   156     ftime_init();
   158     Measure(timeTime, "time to get time with time()");
   159     Measure(timeGethrtime, "time to get time with gethrtime()");
   160     Measure(timeGettimeofday, "time to get time with gettimeofday()");
   161     Measure(timePRTime32, "time to get time with PR_Time() (32bit)");
   162     Measure(timePRTime64, "time to get time with PR_Time() (64bit)");
   164 	PR_Cleanup();
   165 	return 0;
   166 }

mercurial