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/tests/test_support.h"
7 #include "base/test/perf_log.h"
8 #include "mojo/public/system/core_cpp.h"
13 bool WriteTextMessage(MessagePipeHandle handle, const std::string& text) {
14 MojoResult rv = WriteMessageRaw(handle,
16 static_cast<uint32_t>(text.size()),
19 MOJO_WRITE_MESSAGE_FLAG_NONE);
20 return rv == MOJO_RESULT_OK;
23 bool ReadTextMessage(MessagePipeHandle handle, std::string* text) {
25 bool did_wait = false;
27 uint32_t num_bytes = 0, num_handles = 0;
29 rv = ReadMessageRaw(handle,
34 MOJO_READ_MESSAGE_FLAG_NONE);
35 if (rv == MOJO_RESULT_SHOULD_WAIT) {
37 assert(false); // Looping endlessly!?
40 rv = Wait(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE);
41 if (rv != MOJO_RESULT_OK)
50 text->resize(num_bytes);
51 rv = ReadMessageRaw(handle,
56 MOJO_READ_MESSAGE_FLAG_NONE);
57 return rv == MOJO_RESULT_OK;
60 void IterateAndReportPerf(const char* test_name,
61 PerfTestSingleIteration single_iteration,
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;
67 const MojoTimeTicks start_time = GetTimeTicksNow();
68 MojoTimeTicks end_time;
69 size_t iterations = 0;
71 for (size_t i = 0; i < kGranularity; i++)
72 (*single_iteration)(closure);
73 iterations += kGranularity;
75 end_time = GetTimeTicksNow();
76 } while (end_time - start_time < kPerftestTimeMicroseconds);
78 base::LogPerfResult(test_name,
79 1000000.0 * iterations / (end_time - start_time),
83 MojoResult WriteEmptyMessage(const MessagePipeHandle& handle) {
84 return WriteMessageRaw(handle, NULL, 0, NULL, 0,
85 MOJO_WRITE_MESSAGE_FLAG_NONE);
88 MojoResult ReadEmptyMessage(const MessagePipeHandle& handle) {
89 return ReadMessageRaw(handle, NULL, NULL, NULL, NULL,
90 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD);