Commit 649f34f7 authored by yzshen's avatar yzshen Committed by Commit bot

Mojo C++ bindings: add more perf tests for Router/MultiplexRouter.

These new tests measure performance of dispatching incoming messages on the same
thread.

Currently on Linux:
RouterDispatchCost  3.93483e+06 times/second
MultiplexRouterDispatchCost 3.39754e+06 times/second

BUG=594244

Review-Url: https://codereview.chromium.org/2340363002
Cr-Commit-Position: refs/heads/master@{#419007}
parent f225b263
...@@ -152,6 +152,7 @@ InterfaceEndpointClient::InterfaceEndpointClient( ...@@ -152,6 +152,7 @@ InterfaceEndpointClient::InterfaceEndpointClient(
// TODO(yzshen): the way to use validator (or message filter in general) // TODO(yzshen): the way to use validator (or message filter in general)
// directly is a little awkward. // directly is a little awkward.
if (payload_validator)
filters_.Append(std::move(payload_validator)); filters_.Append(std::move(payload_validator));
controller_ = handle_.group_controller()->AttachEndpointClient( controller_ = handle_.group_controller()->AttachEndpointClient(
......
...@@ -129,6 +129,10 @@ class MultiplexRouter ...@@ -129,6 +129,10 @@ class MultiplexRouter
return connector_.handle(); return connector_.handle();
} }
bool SimulateReceivingMessageForTesting(Message* message) {
return filters_.Accept(message);
}
private: private:
class InterfaceEndpoint; class InterfaceEndpoint;
struct Task; struct Task;
......
...@@ -132,6 +132,10 @@ class Router : public MessageReceiverWithResponder { ...@@ -132,6 +132,10 @@ class Router : public MessageReceiverWithResponder {
return &control_message_proxy_; return &control_message_proxy_;
} }
bool SimulateReceivingMessageForTesting(Message* message) {
return filters_.Accept(message);
}
private: private:
// Maps from the id of a response to the MessageReceiver that handles the // Maps from the id of a response to the MessageReceiver that handles the
// response. // response.
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/time/time.h"
#include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/interface_endpoint_client.h" #include "mojo/public/cpp/bindings/interface_endpoint_client.h"
#include "mojo/public/cpp/bindings/lib/message_builder.h" #include "mojo/public/cpp/bindings/lib/message_builder.h"
...@@ -149,6 +150,7 @@ class PingPongPaddle : public MessageReceiverWithResponderStatus { ...@@ -149,6 +150,7 @@ class PingPongPaddle : public MessageReceiverWithResponderStatus {
if (!quit_closure_.is_null()) { if (!quit_closure_.is_null()) {
count++; count++;
if (count >= expected_count_) { if (count >= expected_count_) {
end_time_ = base::TimeTicks::Now();
quit_closure_.Run(); quit_closure_.Run();
return true; return true;
} }
...@@ -166,20 +168,25 @@ class PingPongPaddle : public MessageReceiverWithResponderStatus { ...@@ -166,20 +168,25 @@ class PingPongPaddle : public MessageReceiverWithResponderStatus {
return true; return true;
} }
void Serve(uint32_t expected_count) { base::TimeDelta Serve(uint32_t expected_count) {
base::RunLoop run_loop; base::RunLoop run_loop;
expected_count_ = expected_count; expected_count_ = expected_count;
quit_closure_ = run_loop.QuitClosure(); quit_closure_ = run_loop.QuitClosure();
start_time_ = base::TimeTicks::Now();
internal::MessageBuilder builder(0, 8); internal::MessageBuilder builder(0, 8);
bool result = sender_->Accept(builder.message()); bool result = sender_->Accept(builder.message());
DCHECK(result); DCHECK(result);
run_loop.Run(); run_loop.Run();
return end_time_ - start_time_;
} }
private: private:
base::TimeTicks start_time_;
base::TimeTicks end_time_;
uint32_t expected_count_ = 0; uint32_t expected_count_ = 0;
MessageReceiver* sender_; MessageReceiver* sender_;
base::Closure quit_closure_; base::Closure quit_closure_;
...@@ -197,18 +204,14 @@ TEST_F(MojoBindingsPerftest, RouterPingPong) { ...@@ -197,18 +204,14 @@ TEST_F(MojoBindingsPerftest, RouterPingPong) {
router1.set_incoming_receiver(&paddle1); router1.set_incoming_receiver(&paddle1);
static const uint32_t kWarmUpIterations = 1000; static const uint32_t kWarmUpIterations = 1000;
static const uint32_t kTestIterations = 100000; static const uint32_t kTestIterations = 1000000;
paddle0.Serve(kWarmUpIterations); paddle0.Serve(kWarmUpIterations);
const MojoTimeTicks start_time = MojoGetTimeTicksNow(); base::TimeDelta duration = paddle0.Serve(kTestIterations);
paddle0.Serve(kTestIterations);
const MojoTimeTicks end_time = MojoGetTimeTicksNow();
test::LogPerfResult( test::LogPerfResult("RouterPingPong", nullptr,
"RouterPingPong", nullptr, kTestIterations / duration.InSecondsF(), "pings/second");
kTestIterations / MojoTicksToSeconds(end_time - start_time),
"pings/second");
} }
TEST_F(MojoBindingsPerftest, MultiplexRouterPingPong) { TEST_F(MojoBindingsPerftest, MultiplexRouterPingPong) {
...@@ -224,30 +227,109 @@ TEST_F(MojoBindingsPerftest, MultiplexRouterPingPong) { ...@@ -224,30 +227,109 @@ TEST_F(MojoBindingsPerftest, MultiplexRouterPingPong) {
PingPongPaddle paddle1(nullptr); PingPongPaddle paddle1(nullptr);
InterfaceEndpointClient client0( InterfaceEndpointClient client0(
router0->CreateLocalEndpointHandle(kMasterInterfaceId), &paddle0, router0->CreateLocalEndpointHandle(kMasterInterfaceId), &paddle0, nullptr,
base::MakeUnique<PassThroughFilter>(), false, false, base::ThreadTaskRunnerHandle::Get(), 0u);
base::ThreadTaskRunnerHandle::Get(), 0u);
InterfaceEndpointClient client1( InterfaceEndpointClient client1(
router1->CreateLocalEndpointHandle(kMasterInterfaceId), &paddle1, router1->CreateLocalEndpointHandle(kMasterInterfaceId), &paddle1, nullptr,
base::MakeUnique<PassThroughFilter>(), false, false, base::ThreadTaskRunnerHandle::Get(), 0u);
base::ThreadTaskRunnerHandle::Get(), 0u);
paddle0.set_sender(&client0); paddle0.set_sender(&client0);
paddle1.set_sender(&client1); paddle1.set_sender(&client1);
static const uint32_t kWarmUpIterations = 1000; static const uint32_t kWarmUpIterations = 1000;
static const uint32_t kTestIterations = 100000; static const uint32_t kTestIterations = 1000000;
paddle0.Serve(kWarmUpIterations); paddle0.Serve(kWarmUpIterations);
const MojoTimeTicks start_time = MojoGetTimeTicksNow(); base::TimeDelta duration = paddle0.Serve(kTestIterations);
paddle0.Serve(kTestIterations);
const MojoTimeTicks end_time = MojoGetTimeTicksNow();
test::LogPerfResult( test::LogPerfResult("MultiplexRouterPingPong", nullptr,
"MultiplexRouterPingPong", nullptr, kTestIterations / duration.InSecondsF(), "pings/second");
kTestIterations / MojoTicksToSeconds(end_time - start_time), }
"pings/second");
class CounterReceiver : public MessageReceiverWithResponderStatus {
public:
bool Accept(Message* message) override {
counter_++;
return true;
}
bool AcceptWithResponder(Message* message,
MessageReceiverWithStatus* responder) override {
NOTREACHED();
return true;
}
uint32_t counter() const { return counter_; }
void Reset() { counter_ = 0; }
private:
uint32_t counter_ = 0;
};
TEST_F(MojoBindingsPerftest, RouterDispatchCost) {
MessagePipe pipe;
internal::Router router(std::move(pipe.handle0), FilterChain(), false,
base::ThreadTaskRunnerHandle::Get(), 0u);
CounterReceiver receiver;
router.set_incoming_receiver(&receiver);
static const uint32_t kIterations[] = {1000, 3000000};
for (size_t i = 0; i < 2; ++i) {
receiver.Reset();
base::TimeTicks start_time = base::TimeTicks::Now();
for (size_t j = 0; j < kIterations[i]; ++j) {
internal::MessageBuilder builder(0, 8);
bool result =
router.SimulateReceivingMessageForTesting(builder.message());
DCHECK(result);
}
base::TimeTicks end_time = base::TimeTicks::Now();
base::TimeDelta duration = end_time - start_time;
CHECK_EQ(kIterations[i], receiver.counter());
if (i == 1) {
test::LogPerfResult("RouterDispatchCost", nullptr,
kIterations[i] / duration.InSecondsF(),
"times/second");
}
}
}
TEST_F(MojoBindingsPerftest, MultiplexRouterDispatchCost) {
MessagePipe pipe;
scoped_refptr<internal::MultiplexRouter> router(new internal::MultiplexRouter(
true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get()));
CounterReceiver receiver;
InterfaceEndpointClient client(
router->CreateLocalEndpointHandle(kMasterInterfaceId), &receiver, nullptr,
false, base::ThreadTaskRunnerHandle::Get(), 0u);
static const uint32_t kIterations[] = {1000, 3000000};
for (size_t i = 0; i < 2; ++i) {
receiver.Reset();
base::TimeTicks start_time = base::TimeTicks::Now();
for (size_t j = 0; j < kIterations[i]; ++j) {
internal::MessageBuilder builder(0, 8);
bool result =
router->SimulateReceivingMessageForTesting(builder.message());
DCHECK(result);
}
base::TimeTicks end_time = base::TimeTicks::Now();
base::TimeDelta duration = end_time - start_time;
CHECK_EQ(kIterations[i], receiver.counter());
if (i == 1) {
test::LogPerfResult("MultiplexRouterDispatchCost", nullptr,
kIterations[i] / duration.InSecondsF(),
"times/second");
}
}
} }
} // namespace } // namespace
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment