nsprpub/pr/src/cplus/tests/interval.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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 /* interval.cpp - a test program */
michael@0 7
michael@0 8 #include "rclock.h"
michael@0 9 #include "rcthread.h"
michael@0 10 #include "rcinrval.h"
michael@0 11 #include "rccv.h"
michael@0 12
michael@0 13 #include <prio.h>
michael@0 14 #include <prlog.h>
michael@0 15 #include <prprf.h>
michael@0 16
michael@0 17 #define DEFAULT_ITERATIONS 100
michael@0 18
michael@0 19 PRIntn main(PRIntn argc, char **argv)
michael@0 20 {
michael@0 21 RCLock ml;
michael@0 22 PRStatus rv;
michael@0 23 RCCondition cv(&ml);
michael@0 24
michael@0 25 RCInterval now, timeout, epoch, elapsed;
michael@0 26 PRFileDesc *output = PR_GetSpecialFD(PR_StandardOutput);
michael@0 27 PRIntn msecs, seconds, loops, iterations = DEFAULT_ITERATIONS;
michael@0 28
michael@0 29 /* slow, agonizing waits */
michael@0 30 for (seconds = 0; seconds < 10; ++seconds)
michael@0 31 {
michael@0 32 timeout = RCInterval::FromSeconds(seconds);
michael@0 33 cv.SetTimeout(timeout);
michael@0 34 {
michael@0 35 RCEnter lock(&ml);
michael@0 36
michael@0 37 epoch.SetToNow();
michael@0 38
michael@0 39 rv = cv.Wait();
michael@0 40 PR_ASSERT(PR_SUCCESS == rv);
michael@0 41
michael@0 42 now = RCInterval(RCInterval::now);
michael@0 43 elapsed = now - epoch;
michael@0 44 }
michael@0 45
michael@0 46 PR_fprintf(
michael@0 47 output, "Waiting %u seconds took %s%u milliseconds\n",
michael@0 48 seconds, ((elapsed < timeout)? "**" : ""),
michael@0 49 elapsed.ToMilliseconds());
michael@0 50 }
michael@0 51
michael@0 52 /* more slow, agonizing sleeps */
michael@0 53 for (seconds = 0; seconds < 10; ++seconds)
michael@0 54 {
michael@0 55 timeout = RCInterval::FromSeconds(seconds);
michael@0 56 {
michael@0 57 epoch.SetToNow();
michael@0 58
michael@0 59 rv = RCThread::Sleep(timeout);
michael@0 60 PR_ASSERT(PR_SUCCESS == rv);
michael@0 61
michael@0 62 now = RCInterval(RCInterval::now);
michael@0 63 elapsed = now - epoch;
michael@0 64 }
michael@0 65
michael@0 66 PR_fprintf(
michael@0 67 output, "Sleeping %u seconds took %s%u milliseconds\n",
michael@0 68 seconds, ((elapsed < timeout)? "**" : ""),
michael@0 69 elapsed.ToMilliseconds());
michael@0 70 }
michael@0 71
michael@0 72 /* fast, spritely little devils */
michael@0 73 for (msecs = 10; msecs < 100; msecs += 10)
michael@0 74 {
michael@0 75 timeout = RCInterval::FromMilliseconds(msecs);
michael@0 76 cv.SetTimeout(timeout);
michael@0 77 {
michael@0 78 RCEnter lock(&ml);
michael@0 79
michael@0 80 epoch.SetToNow();
michael@0 81
michael@0 82 for (loops = 0; loops < iterations; ++loops)
michael@0 83 {
michael@0 84 rv = cv.Wait();
michael@0 85 PR_ASSERT(PR_SUCCESS == rv);
michael@0 86 }
michael@0 87
michael@0 88 now = RCInterval(RCInterval::now);
michael@0 89 elapsed = now - epoch;
michael@0 90 }
michael@0 91 elapsed /= iterations;
michael@0 92
michael@0 93 PR_fprintf(
michael@0 94 output, "Waiting %u msecs took %s%u milliseconds average\n",
michael@0 95 msecs, ((elapsed < timeout)? "**" : ""), elapsed.ToMilliseconds());
michael@0 96 }
michael@0 97 return 0;
michael@0 98 } /* main */
michael@0 99
michael@0 100 /* interval.cpp */
michael@0 101

mercurial