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.
5 #include "mojo/public/cpp/test_support/test_utils.h"
7 #include "mojo/public/cpp/system/core.h"
8 #include "mojo/public/cpp/test_support/test_support.h"
13 bool WriteTextMessage(const MessagePipeHandle& handle,
14 const std::string& text) {
15 MojoResult rv = WriteMessageRaw(handle,
17 static_cast<uint32_t>(text.size()),
20 MOJO_WRITE_MESSAGE_FLAG_NONE);
21 return rv == MOJO_RESULT_OK;
24 bool ReadTextMessage(const MessagePipeHandle& handle, std::string* text) {
26 bool did_wait = false;
28 uint32_t num_bytes = 0, num_handles = 0;
30 rv = ReadMessageRaw(handle,
35 MOJO_READ_MESSAGE_FLAG_NONE);
36 if (rv == MOJO_RESULT_SHOULD_WAIT) {
38 assert(false); // Looping endlessly!?
41 rv = Wait(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE);
42 if (rv != MOJO_RESULT_OK)
51 text->resize(num_bytes);
52 rv = ReadMessageRaw(handle,
57 MOJO_READ_MESSAGE_FLAG_NONE);
58 return rv == MOJO_RESULT_OK;
61 bool DiscardMessage(const MessagePipeHandle& handle) {
62 MojoResult rv = ReadMessageRaw(handle, NULL, NULL, NULL, NULL,
63 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD);
64 return rv == MOJO_RESULT_OK;
67 void IterateAndReportPerf(const char* test_name,
68 PerfTestSingleIteration single_iteration,
70 // TODO(vtl): These should be specifiable using command-line flags.
71 static const size_t kGranularity = 100;
72 static const MojoTimeTicks kPerftestTimeMicroseconds = 3 * 1000000;
74 const MojoTimeTicks start_time = GetTimeTicksNow();
75 MojoTimeTicks end_time;
76 size_t iterations = 0;
78 for (size_t i = 0; i < kGranularity; i++)
79 (*single_iteration)(closure);
80 iterations += kGranularity;
82 end_time = GetTimeTicksNow();
83 } while (end_time - start_time < kPerftestTimeMicroseconds);
85 MojoTestSupportLogPerfResult(test_name,
86 1000000.0 * iterations / (end_time - start_time),