• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
11 
12 #include "mojo/public/cpp/system/core.h"
13 #include "mojo/public/cpp/system/wait.h"
14 #include "mojo/public/cpp/test_support/test_support.h"
15 
16 namespace mojo {
17 namespace test {
18 
WriteTextMessage(const MessagePipeHandle & handle,const std::string & text)19 bool WriteTextMessage(const MessagePipeHandle& handle,
20                       const std::string& text) {
21   MojoResult rv = WriteMessageRaw(handle,
22                                   text.data(),
23                                   static_cast<uint32_t>(text.size()),
24                                   nullptr,
25                                   0,
26                                   MOJO_WRITE_MESSAGE_FLAG_NONE);
27   return rv == MOJO_RESULT_OK;
28 }
29 
ReadTextMessage(const MessagePipeHandle & handle,std::string * text)30 bool ReadTextMessage(const MessagePipeHandle& handle, std::string* text) {
31   if (Wait(handle, MOJO_HANDLE_SIGNAL_READABLE) != MOJO_RESULT_OK)
32     return false;
33 
34   std::vector<uint8_t> bytes;
35   std::vector<ScopedHandle> handles;
36   if (ReadMessageRaw(handle, &bytes, &handles, MOJO_READ_MESSAGE_FLAG_NONE) !=
37       MOJO_RESULT_OK) {
38     return false;
39   }
40 
41   assert(handles.empty());
42   text->resize(bytes.size());
43   std::copy(bytes.begin(), bytes.end(), text->begin());
44   return true;
45 }
46 
DiscardMessage(const MessagePipeHandle & handle)47 bool DiscardMessage(const MessagePipeHandle& handle) {
48   MojoMessageHandle message;
49   int rv = MojoReadMessage(handle.value(), nullptr, &message);
50   if (rv != MOJO_RESULT_OK)
51     return false;
52   MojoDestroyMessage(message);
53   return true;
54 }
55 
IterateAndReportPerf(const char * test_name,const char * sub_test_name,PerfTestSingleIteration single_iteration,void * closure)56 void IterateAndReportPerf(const char* test_name,
57                           const char* sub_test_name,
58                           PerfTestSingleIteration single_iteration,
59                           void* closure) {
60   // TODO(vtl): These should be specifiable using command-line flags.
61   static const size_t kGranularity = 100;
62   static const MojoTimeTicks kPerftestTimeMicroseconds = 3 * 1000000;
63 
64   const MojoTimeTicks start_time = GetTimeTicksNow();
65   MojoTimeTicks end_time;
66   size_t iterations = 0;
67   do {
68     for (size_t i = 0; i < kGranularity; i++)
69       (*single_iteration)(closure);
70     iterations += kGranularity;
71 
72     end_time = GetTimeTicksNow();
73   } while (end_time - start_time < kPerftestTimeMicroseconds);
74 
75   MojoTestSupportLogPerfResult(test_name, sub_test_name,
76                                1000000.0 * iterations / (end_time - start_time),
77                                "iterations/second");
78 }
79 
80 }  // namespace test
81 }  // namespace mojo
82