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