Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / mojo / public / tests / test_support.cc
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 "mojo/public/system/core_cpp.h"
9
10 namespace mojo {
11 namespace test {
12
13 bool WriteTextMessage(MessagePipeHandle handle, const std::string& text) {
14   MojoResult rv = WriteMessageRaw(handle,
15                                   text.data(),
16                                   static_cast<uint32_t>(text.size()),
17                                   NULL,
18                                   0,
19                                   MOJO_WRITE_MESSAGE_FLAG_NONE);
20   return rv == MOJO_RESULT_OK;
21 }
22
23 bool ReadTextMessage(MessagePipeHandle handle, std::string* text) {
24   MojoResult rv;
25   bool did_wait = false;
26
27   uint32_t num_bytes = 0, num_handles = 0;
28   for (;;) {
29     rv = ReadMessageRaw(handle,
30                         NULL,
31                         &num_bytes,
32                         NULL,
33                         &num_handles,
34                         MOJO_READ_MESSAGE_FLAG_NONE);
35     if (rv == MOJO_RESULT_SHOULD_WAIT) {
36       if (did_wait) {
37         assert(false);  // Looping endlessly!?
38         return false;
39       }
40       rv = Wait(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE);
41       if (rv != MOJO_RESULT_OK)
42         return false;
43       did_wait = true;
44     } else {
45       assert(!num_handles);
46       break;
47     }
48   }
49
50   text->resize(num_bytes);
51   rv = ReadMessageRaw(handle,
52                       &text->at(0),
53                       &num_bytes,
54                       NULL,
55                       &num_handles,
56                       MOJO_READ_MESSAGE_FLAG_NONE);
57   return rv == MOJO_RESULT_OK;
58 }
59
60 void IterateAndReportPerf(const char* test_name,
61                           PerfTestSingleIteration single_iteration,
62                           void* closure) {
63   // TODO(vtl): These should be specifiable using command-line flags.
64   static const size_t kGranularity = 100;
65   static const MojoTimeTicks kPerftestTimeMicroseconds = 3 * 1000000;
66
67   const MojoTimeTicks start_time = GetTimeTicksNow();
68   MojoTimeTicks end_time;
69   size_t iterations = 0;
70   do {
71     for (size_t i = 0; i < kGranularity; i++)
72       (*single_iteration)(closure);
73     iterations += kGranularity;
74
75     end_time = GetTimeTicksNow();
76   } while (end_time - start_time < kPerftestTimeMicroseconds);
77
78   base::LogPerfResult(test_name,
79                       1000000.0 * iterations / (end_time - start_time),
80                       "iterations/second");
81 }
82
83 MojoResult WriteEmptyMessage(const MessagePipeHandle& handle) {
84   return WriteMessageRaw(handle, NULL, 0, NULL, 0,
85                          MOJO_WRITE_MESSAGE_FLAG_NONE);
86 }
87
88 MojoResult ReadEmptyMessage(const MessagePipeHandle& handle) {
89   return ReadMessageRaw(handle, NULL, NULL, NULL, NULL,
90                         MOJO_READ_MESSAGE_FLAG_MAY_DISCARD);
91 }
92
93 }  // namespace test
94 }  // namespace mojo