nsprpub/pr/tests/time.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/time.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,169 @@
     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 + * Program to test different ways to get the time; right now it is tuned
    1.11 + * only for solaris.
    1.12 + *   solaris results (100000 iterations):
    1.13 + *          time to get time with time():   4.63 usec avg, 463 msec total
    1.14 + *     time to get time with gethrtime():   2.17 usec avg, 217 msec total
    1.15 + *  time to get time with gettimeofday():   1.25 usec avg, 125 msec total
    1.16 + *
    1.17 + *
    1.18 + */
    1.19 +/***********************************************************************
    1.20 +** Includes
    1.21 +***********************************************************************/
    1.22 +/* Used to get the command line option */
    1.23 +#include "plgetopt.h"
    1.24 +
    1.25 +#include "nspr.h"
    1.26 +#include "prpriv.h"
    1.27 +#include "prinrval.h"
    1.28 +
    1.29 +#include <stdio.h>
    1.30 +#include <stdlib.h>
    1.31 +#include <string.h>
    1.32 +#include <sys/time.h>
    1.33 +
    1.34 +#define DEFAULT_COUNT 100000
    1.35 +PRInt32 count;
    1.36 +
    1.37 +time_t itime;
    1.38 +hrtime_t ihrtime;
    1.39 +
    1.40 +void
    1.41 +ftime_init()
    1.42 +{
    1.43 +   itime = time(NULL);
    1.44 +   ihrtime = gethrtime();
    1.45 +}
    1.46 +
    1.47 +time_t
    1.48 +ftime()
    1.49 +{
    1.50 +        hrtime_t now = gethrtime();
    1.51 +
    1.52 +        return itime + ((now - ihrtime) / 1000000000ll);
    1.53 +}
    1.54 +
    1.55 +static void timeTime(void)
    1.56 +{
    1.57 +    PRInt32 index = count;
    1.58 +    time_t rv;
    1.59 + 
    1.60 +    for (;index--;)
    1.61 +        rv = time(NULL);
    1.62 +}
    1.63 +
    1.64 +static void timeGethrtime(void)
    1.65 +{
    1.66 +    PRInt32 index = count;
    1.67 +    time_t rv;
    1.68 + 
    1.69 +    for (;index--;)
    1.70 +        rv = ftime();
    1.71 +}
    1.72 +
    1.73 +static void timeGettimeofday(void)
    1.74 +{
    1.75 +    PRInt32 index = count;
    1.76 +    time_t rv;
    1.77 +    struct timeval tp;
    1.78 + 
    1.79 +    for (;index--;)
    1.80 +        rv = gettimeofday(&tp, NULL);
    1.81 +}
    1.82 +
    1.83 +static void timePRTime32(void)
    1.84 +{
    1.85 +    PRInt32 index = count;
    1.86 +    PRInt32 rv32;
    1.87 +    PRTime q;
    1.88 +    PRTime rv;
    1.89 +
    1.90 +    LL_I2L(q, 1000000);
    1.91 + 
    1.92 +    for (;index--;) {
    1.93 +        rv = PR_Now();
    1.94 +        LL_DIV(rv, rv, q);
    1.95 +        LL_L2I(rv32, rv);
    1.96 +    }
    1.97 +}
    1.98 +
    1.99 +static void timePRTime64(void)
   1.100 +{
   1.101 +    PRInt32 index = count;
   1.102 +    PRTime rv;
   1.103 + 
   1.104 +    for (;index--;)
   1.105 +        rv = PR_Now();
   1.106 +}
   1.107 +
   1.108 +/************************************************************************/
   1.109 +
   1.110 +static void Measure(void (*func)(void), const char *msg)
   1.111 +{
   1.112 +    PRIntervalTime start, stop;
   1.113 +    double d;
   1.114 +    PRInt32 tot;
   1.115 +
   1.116 +    start = PR_IntervalNow();
   1.117 +    (*func)();
   1.118 +    stop = PR_IntervalNow();
   1.119 +
   1.120 +    d = (double)PR_IntervalToMicroseconds(stop - start);
   1.121 +    tot = PR_IntervalToMilliseconds(stop-start);
   1.122 +
   1.123 +    if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
   1.124 +}
   1.125 +
   1.126 +int main(int argc, char **argv)
   1.127 +{
   1.128 +	/* The command line argument: -d is used to determine if the test is being run
   1.129 +	in debug mode. The regress tool requires only one line output:PASS or FAIL.
   1.130 +	All of the printfs associated with this test has been handled with a if (debug_mode)
   1.131 +	test.
   1.132 +	Usage: test_name -d
   1.133 +	*/
   1.134 +	PLOptStatus os;
   1.135 +	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
   1.136 +	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   1.137 +    {
   1.138 +		if (PL_OPT_BAD == os) continue;
   1.139 +        switch (opt->option)
   1.140 +        {
   1.141 +        case 'd':  /* debug mode */
   1.142 +			debug_mode = 1;
   1.143 +            break;
   1.144 +         default:
   1.145 +            break;
   1.146 +        }
   1.147 +    }
   1.148 +	PL_DestroyOptState(opt);
   1.149 +
   1.150 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   1.151 +    PR_STDIO_INIT();
   1.152 +
   1.153 +    if (argc > 1) {
   1.154 +	count = atoi(argv[1]);
   1.155 +    } else {
   1.156 +	count = DEFAULT_COUNT;
   1.157 +    }
   1.158 +
   1.159 +    ftime_init();
   1.160 +
   1.161 +    Measure(timeTime, "time to get time with time()");
   1.162 +    Measure(timeGethrtime, "time to get time with gethrtime()");
   1.163 +    Measure(timeGettimeofday, "time to get time with gettimeofday()");
   1.164 +    Measure(timePRTime32, "time to get time with PR_Time() (32bit)");
   1.165 +    Measure(timePRTime64, "time to get time with PR_Time() (64bit)");
   1.166 +
   1.167 +	PR_Cleanup();
   1.168 +	return 0;
   1.169 +}
   1.170 +
   1.171 +
   1.172 +

mercurial