ipc/ipdl/test/cxx/TestLatency.cpp

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

     1 #include "TestLatency.h"
     3 #include "IPDLUnitTests.h"      // fail etc.
     5 // A ping/pong trial takes O(100us) or more, so if we don't have 10us
     6 // resolution or better, the results will not be terribly useful
     7 static const double kTimingResolutionCutoff = 0.00001; // 10us
     9 namespace mozilla {
    10 namespace _ipdltest {
    12 //-----------------------------------------------------------------------------
    13 // parent
    15 TestLatencyParent::TestLatencyParent() :
    16     mStart(),
    17     mPPTimeTotal(),
    18     mPP5TimeTotal(),
    19     mRpcTimeTotal(),
    20     mPPTrialsToGo(NR_TRIALS),
    21     mPP5TrialsToGo(NR_TRIALS),
    22     mNumChildProcessedCompressedSpams(0)
    23 {
    24     MOZ_COUNT_CTOR(TestLatencyParent);
    25 }
    27 TestLatencyParent::~TestLatencyParent()
    28 {
    29     MOZ_COUNT_DTOR(TestLatencyParent);
    30 }
    32 void
    33 TestLatencyParent::Main()
    34 {
    35     TimeDuration resolution = TimeDuration::Resolution();
    36     if (resolution.ToSeconds() > kTimingResolutionCutoff) {
    37         puts("  (skipping TestLatency, timing resolution is too poor)");
    38         Close();
    39         return;
    40     }
    42     printf("  timing resolution: %g seconds\n",
    43            resolution.ToSecondsSigDigits());
    45     if (mozilla::ipc::LoggingEnabled())
    46         NS_RUNTIMEABORT("you really don't want to log all IPC messages during this test, trust me");
    48     PingPongTrial();
    49 }
    51 void
    52 TestLatencyParent::PingPongTrial()
    53 {
    54     mStart = TimeStamp::Now();
    55     if (!SendPing())
    56         fail("sending Ping()");
    57 }
    59 void
    60 TestLatencyParent::Ping5Pong5Trial()
    61 {
    62     mStart = TimeStamp::Now();
    64     if (!SendPing5() ||
    65         !SendPing5() ||
    66         !SendPing5() ||
    67         !SendPing5() ||
    68         !SendPing5())
    69         fail("sending Ping5()");
    70 }
    72 bool
    73 TestLatencyParent::RecvPong()
    74 {
    75     TimeDuration thisTrial = (TimeStamp::Now() - mStart);
    76     mPPTimeTotal += thisTrial;
    78     if (0 == (mPPTrialsToGo % 1000))
    79         printf("  PP trial %d: %g\n",
    80                mPPTrialsToGo, thisTrial.ToSecondsSigDigits());
    82     if (--mPPTrialsToGo > 0)
    83         PingPongTrial();
    84     else
    85         Ping5Pong5Trial();
    86     return true;
    87 }
    89 bool
    90 TestLatencyParent::RecvPong5()
    91 {
    92     if (PTestLatency::PING5 != state())
    93         return true;
    95     TimeDuration thisTrial = (TimeStamp::Now() - mStart);
    96     mPP5TimeTotal += thisTrial;
    98     if (0 == (mPP5TrialsToGo % 1000))
    99         printf("  PP5 trial %d: %g\n",
   100                mPP5TrialsToGo, thisTrial.ToSecondsSigDigits());
   102     if (0 < --mPP5TrialsToGo)
   103         Ping5Pong5Trial();
   104     else
   105         RpcTrials();
   107     return true;
   108 }
   110 void
   111 TestLatencyParent::RpcTrials()
   112 {
   113     TimeStamp start = TimeStamp::Now();
   114     for (int i = 0; i < NR_TRIALS; ++i) {
   115         if (!CallRpc())
   116             fail("can't call Rpc()");
   117         if (0 == (i % 1000))
   118             printf("  Rpc trial %d\n", i);
   119     }
   120     mRpcTimeTotal = (TimeStamp::Now() - start);
   122     SpamTrial();
   123 }
   125 void
   126 TestLatencyParent::SpamTrial()
   127 {
   128     TimeStamp start = TimeStamp::Now();
   129     for (int i = 0; i < NR_SPAMS - 1; ++i) {
   130         if (!SendSpam())
   131             fail("sending Spam()");
   132         if (0 == (i % 10000))
   133             printf("  Spam trial %d\n", i);
   134     }
   136     // Synchronize with the child process to ensure all messages have
   137     // been processed.  This adds the overhead of a reply message from
   138     // child-->here, but should be insignificant compared to >>
   139     // NR_SPAMS.
   140     if (!CallSynchro())
   141         fail("calling Synchro()");
   143     mSpamTimeTotal = (TimeStamp::Now() - start);
   145     CompressedSpamTrial();
   146 }
   148 void
   149 TestLatencyParent::CompressedSpamTrial()
   150 {
   151     for (int i = 0; i < NR_SPAMS; ++i) {
   152         if (!SendCompressedSpam(i + 1))
   153             fail("sending CompressedSpam()");
   154         if (0 == (i % 10000))
   155             printf("  CompressedSpam trial %d\n", i);
   156     }
   158     uint32_t lastSeqno;
   159     if (!CallSynchro2(&lastSeqno, &mNumChildProcessedCompressedSpams))
   160         fail("calling Synchro2()");
   162     if (lastSeqno != NR_SPAMS)
   163         fail("last seqno was %u, expected %u", lastSeqno, NR_SPAMS);
   165     // NB: since this is testing an optimization, it's somewhat bogus.
   166     // Need to make a warning if it actually intermittently fails in
   167     // practice, which is doubtful.
   168     if (!(mNumChildProcessedCompressedSpams < NR_SPAMS))
   169         fail("Didn't compress any messages?");
   171     Exit();
   172 }
   174 void
   175 TestLatencyParent::Exit()
   176 {
   177     Close();
   178 }
   180 //-----------------------------------------------------------------------------
   181 // child
   183 TestLatencyChild::TestLatencyChild()
   184     : mLastSeqno(0)
   185     , mNumProcessedCompressedSpams(0)
   186 {
   187     MOZ_COUNT_CTOR(TestLatencyChild);
   188 }
   190 TestLatencyChild::~TestLatencyChild()
   191 {
   192     MOZ_COUNT_DTOR(TestLatencyChild);
   193 }
   195 bool
   196 TestLatencyChild::RecvPing()
   197 {
   198     SendPong();
   199     return true;
   200 }
   202 bool
   203 TestLatencyChild::RecvPing5()
   204 {
   205     if (PTestLatency::PONG1 != state())
   206         return true;
   208     if (!SendPong5() ||
   209         !SendPong5() ||
   210         !SendPong5() ||
   211         !SendPong5() ||
   212         !SendPong5())
   213         fail("sending Pong5()");
   215     return true;
   216 }
   218 bool
   219 TestLatencyChild::AnswerRpc()
   220 {
   221     return true;
   222 }
   224 bool
   225 TestLatencyChild::RecvSpam()
   226 {
   227     // no-op
   228     return true;
   229 }
   231 bool
   232 TestLatencyChild::AnswerSynchro()
   233 {
   234     return true;
   235 }
   237 bool
   238 TestLatencyChild::RecvCompressedSpam(const uint32_t& seqno)
   239 {
   240     if (seqno <= mLastSeqno)
   241         fail("compressed seqnos must monotonically increase");
   243     mLastSeqno = seqno;
   244     ++mNumProcessedCompressedSpams;
   245     return true;
   246 }
   248 bool
   249 TestLatencyChild::AnswerSynchro2(uint32_t* lastSeqno,
   250                                  uint32_t* numMessagesDispatched)
   251 {
   252     *lastSeqno = mLastSeqno;
   253     *numMessagesDispatched = mNumProcessedCompressedSpams;
   254     return true;
   255 }
   257 } // namespace _ipdltest
   258 } // namespace mozilla

mercurial