Home | History | Annotate | Download | only in tests
      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/tests/test_support.h"
      6 
      7 #include "base/test/perf_log.h"
      8 #include "base/time/time.h"
      9 
     10 namespace mojo {
     11 namespace test {
     12 
     13 MessagePipe::MessagePipe() {
     14   CreateMessagePipe(&handle_0, &handle_1);
     15 }
     16 
     17 MessagePipe::~MessagePipe() {
     18 }
     19 
     20 bool WriteTextMessage(MessagePipeHandle handle, const std::string& text) {
     21   MojoResult rv = WriteMessageRaw(handle,
     22                                   text.data(),
     23                                   static_cast<uint32_t>(text.size()),
     24                                   NULL,
     25                                   0,
     26                                   MOJO_WRITE_MESSAGE_FLAG_NONE);
     27   return rv == MOJO_RESULT_OK;
     28 }
     29 
     30 bool ReadTextMessage(MessagePipeHandle handle, std::string* text) {
     31   MojoResult rv;
     32   bool did_wait = false;
     33 
     34   uint32_t num_bytes = 0, num_handles = 0;
     35   for (;;) {
     36     rv = ReadMessageRaw(handle,
     37                         NULL,
     38                         &num_bytes,
     39                         NULL,
     40                         &num_handles,
     41                         MOJO_READ_MESSAGE_FLAG_NONE);
     42     if (rv == MOJO_RESULT_NOT_FOUND) {
     43       if (did_wait) {
     44         assert(false);  // Looping endlessly!?
     45         return false;
     46       }
     47       rv = Wait(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE);
     48       if (rv != MOJO_RESULT_OK)
     49         return false;
     50       did_wait = true;
     51     } else {
     52       assert(!num_handles);
     53       break;
     54     }
     55   }
     56 
     57   text->resize(num_bytes);
     58   rv = ReadMessageRaw(handle,
     59                       &text->at(0),
     60                       &num_bytes,
     61                       NULL,
     62                       &num_handles,
     63                       MOJO_READ_MESSAGE_FLAG_NONE);
     64   return rv == MOJO_RESULT_OK;
     65 }
     66 
     67 void IterateAndReportPerf(const char* test_name,
     68                           base::Callback<void()> single_iteration) {
     69   // TODO(vtl): These should be specifiable using command-line flags.
     70   static const size_t kGranularity = 100;
     71   static const double kPerftestTimeSeconds = 3.0;
     72 
     73   const base::TimeTicks start_time = base::TimeTicks::HighResNow();
     74   base::TimeTicks end_time;
     75   size_t iterations = 0;
     76   do {
     77     for (size_t i = 0; i < kGranularity; i++)
     78       single_iteration.Run();
     79     iterations += kGranularity;
     80 
     81     end_time = base::TimeTicks::HighResNow();
     82   } while ((end_time - start_time).InSecondsF() < kPerftestTimeSeconds);
     83 
     84   base::LogPerfResult(test_name,
     85                       iterations / (end_time - start_time).InSecondsF(),
     86                       "iterations/second");
     87 }
     88 
     89 MojoResult WriteEmptyMessage(const MessagePipeHandle& handle) {
     90   return WriteMessageRaw(handle, NULL, 0, NULL, 0,
     91                          MOJO_WRITE_MESSAGE_FLAG_NONE);
     92 }
     93 
     94 MojoResult ReadEmptyMessage(const MessagePipeHandle& handle) {
     95   return ReadMessageRaw(handle, NULL, NULL, NULL, NULL,
     96                         MOJO_READ_MESSAGE_FLAG_MAY_DISCARD);
     97 }
     98 
     99 }  // namespace test
    100 }  // namespace mojo
    101