Home | History | Annotate | Download | only in lib
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "mojo/public/cpp/test_support/test_utils.h"
      6 
      7 #include <stddef.h>
      8 #include <stdint.h>
      9 
     10 #include "mojo/public/cpp/system/core.h"
     11 #include "mojo/public/cpp/test_support/test_support.h"
     12 
     13 namespace mojo {
     14 namespace test {
     15 
     16 bool WriteTextMessage(const MessagePipeHandle& handle,
     17                       const std::string& text) {
     18   MojoResult rv = WriteMessageRaw(handle,
     19                                   text.data(),
     20                                   static_cast<uint32_t>(text.size()),
     21                                   nullptr,
     22                                   0,
     23                                   MOJO_WRITE_MESSAGE_FLAG_NONE);
     24   return rv == MOJO_RESULT_OK;
     25 }
     26 
     27 bool ReadTextMessage(const MessagePipeHandle& handle, std::string* text) {
     28   MojoResult rv;
     29   bool did_wait = false;
     30 
     31   uint32_t num_bytes = 0, num_handles = 0;
     32   for (;;) {
     33     rv = ReadMessageRaw(handle,
     34                         nullptr,
     35                         &num_bytes,
     36                         nullptr,
     37                         &num_handles,
     38                         MOJO_READ_MESSAGE_FLAG_NONE);
     39     if (rv == MOJO_RESULT_SHOULD_WAIT) {
     40       if (did_wait) {
     41         assert(false);  // Looping endlessly!?
     42         return false;
     43       }
     44       rv = Wait(handle, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE,
     45                 nullptr);
     46       if (rv != MOJO_RESULT_OK)
     47         return false;
     48       did_wait = true;
     49     } else {
     50       assert(!num_handles);
     51       break;
     52     }
     53   }
     54 
     55   text->resize(num_bytes);
     56   rv = ReadMessageRaw(handle,
     57                       &text->at(0),
     58                       &num_bytes,
     59                       nullptr,
     60                       &num_handles,
     61                       MOJO_READ_MESSAGE_FLAG_NONE);
     62   return rv == MOJO_RESULT_OK;
     63 }
     64 
     65 bool DiscardMessage(const MessagePipeHandle& handle) {
     66   MojoResult rv = ReadMessageRaw(handle,
     67                                  nullptr,
     68                                  nullptr,
     69                                  nullptr,
     70                                  nullptr,
     71                                  MOJO_READ_MESSAGE_FLAG_MAY_DISCARD);
     72   return rv == MOJO_RESULT_OK;
     73 }
     74 
     75 void IterateAndReportPerf(const char* test_name,
     76                           const char* sub_test_name,
     77                           PerfTestSingleIteration single_iteration,
     78                           void* closure) {
     79   // TODO(vtl): These should be specifiable using command-line flags.
     80   static const size_t kGranularity = 100;
     81   static const MojoTimeTicks kPerftestTimeMicroseconds = 3 * 1000000;
     82 
     83   const MojoTimeTicks start_time = GetTimeTicksNow();
     84   MojoTimeTicks end_time;
     85   size_t iterations = 0;
     86   do {
     87     for (size_t i = 0; i < kGranularity; i++)
     88       (*single_iteration)(closure);
     89     iterations += kGranularity;
     90 
     91     end_time = GetTimeTicksNow();
     92   } while (end_time - start_time < kPerftestTimeMicroseconds);
     93 
     94   MojoTestSupportLogPerfResult(test_name, sub_test_name,
     95                                1000000.0 * iterations / (end_time - start_time),
     96                                "iterations/second");
     97 }
     98 
     99 }  // namespace test
    100 }  // namespace mojo
    101