summaryrefslogtreecommitdiffstats
path: root/mojo/message_pump/handle_watcher_perftest.cc
blob: 19f78101661d8700203589041a2abb14abc451b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include <string>
#include <utility>

#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "mojo/message_pump/handle_watcher.h"
#include "mojo/message_pump/message_pump_mojo.h"
#include "mojo/public/cpp/test_support/test_support.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace common {
namespace test {

enum MessageLoopConfig {
  MESSAGE_LOOP_CONFIG_DEFAULT = 0,
  MESSAGE_LOOP_CONFIG_MOJO = 1
};

scoped_ptr<base::MessageLoop> CreateMessageLoop(MessageLoopConfig config) {
  scoped_ptr<base::MessageLoop> loop;
  if (config == MESSAGE_LOOP_CONFIG_DEFAULT)
    loop.reset(new base::MessageLoop());
  else
    loop.reset(new base::MessageLoop(MessagePumpMojo::Create()));
  return loop;
}

void OnWatcherSignaled(const base::Closure& callback, MojoResult /* result */) {
  callback.Run();
}

class ScopedPerfTimer {
 public:
  ScopedPerfTimer(const std::string& test_name,
                  const std::string& sub_test_name,
                  uint64_t iterations)
      : test_name_(test_name),
        sub_test_name_(sub_test_name),
        iterations_(iterations),
        start_time_(base::TimeTicks::Now()) {}
  ~ScopedPerfTimer() {
    base::TimeTicks end_time = base::TimeTicks::Now();
    mojo::test::LogPerfResult(
        test_name_.c_str(), sub_test_name_.c_str(),
        iterations_ / (end_time - start_time_).InSecondsF(),
        "iterations/second");
  }

 private:
  const std::string test_name_;
  const std::string sub_test_name_;
  const uint64_t iterations_;
  base::TimeTicks start_time_;

  DISALLOW_COPY_AND_ASSIGN(ScopedPerfTimer);
};

class HandleWatcherPerftest : public testing::TestWithParam<MessageLoopConfig> {
 public:
  HandleWatcherPerftest() : message_loop_(CreateMessageLoop(GetParam())) {}

 protected:
  std::string GetMessageLoopName() const {
    return (GetParam() == MESSAGE_LOOP_CONFIG_DEFAULT) ? "DefaultMessageLoop"
                                                       : "MojoMessageLoop";
  }

 private:
  scoped_ptr<base::MessageLoop> message_loop_;

  DISALLOW_COPY_AND_ASSIGN(HandleWatcherPerftest);
};

INSTANTIATE_TEST_CASE_P(MultipleMessageLoopConfigs,
                        HandleWatcherPerftest,
                        testing::Values(MESSAGE_LOOP_CONFIG_DEFAULT,
                                        MESSAGE_LOOP_CONFIG_MOJO));

void NeverReached(MojoResult result) {
  FAIL() << "Callback should never be invoked " << result;
}

TEST_P(HandleWatcherPerftest, StartStop) {
  const uint64_t kIterations = 100000;
  MessagePipe pipe;
  HandleWatcher watcher;

  ScopedPerfTimer timer("StartStop", GetMessageLoopName(), kIterations);
  for (uint64_t i = 0; i < kIterations; i++) {
    watcher.Start(pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
                  MOJO_DEADLINE_INDEFINITE, base::Bind(&NeverReached));
    watcher.Stop();
  }
}

TEST_P(HandleWatcherPerftest, StartAllThenStop_1000Handles) {
  const uint64_t kIterations = 100;
  const uint64_t kHandles = 1000;

  struct TestData {
    MessagePipe pipe;
    HandleWatcher watcher;
  };
  ScopedVector<TestData> data_vector;
  // Create separately from the start/stop loops to avoid affecting the
  // benchmark.
  for (uint64_t i = 0; i < kHandles; i++) {
    scoped_ptr<TestData> test_data(new TestData);
    ASSERT_TRUE(test_data->pipe.handle0.is_valid());
    data_vector.push_back(std::move(test_data));
  }

  ScopedPerfTimer timer("StartAllThenStop_1000Handles", GetMessageLoopName(),
                        kIterations * kHandles);
  for (uint64_t iter = 0; iter < kIterations; iter++) {
    for (uint64_t i = 0; i < kHandles; i++) {
      TestData* test_data = data_vector[i];
      test_data->watcher.Start(
          test_data->pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
          MOJO_DEADLINE_INDEFINITE, base::Bind(&NeverReached));
    }
    for (uint64_t i = 0; i < kHandles; i++) {
      TestData* test_data = data_vector[i];
      test_data->watcher.Stop();
    }
  }
}

TEST_P(HandleWatcherPerftest, StartAndSignal) {
  const uint64_t kIterations = 10000;
  const std::string kMessage = "hello";
  MessagePipe pipe;
  HandleWatcher watcher;
  std::string received_message;

  ScopedPerfTimer timer("StartAndSignal", GetMessageLoopName(), kIterations);
  for (uint64_t i = 0; i < kIterations; i++) {
    base::RunLoop run_loop;
    watcher.Start(pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
                  MOJO_DEADLINE_INDEFINITE,
                  base::Bind(&OnWatcherSignaled, run_loop.QuitClosure()));
    ASSERT_TRUE(mojo::test::WriteTextMessage(pipe.handle1.get(), kMessage));
    run_loop.Run();
    watcher.Stop();

    ASSERT_TRUE(
        mojo::test::ReadTextMessage(pipe.handle0.get(), &received_message));
    EXPECT_EQ(kMessage, received_message);
    received_message.clear();
  }
}

TEST_P(HandleWatcherPerftest, StartAndSignal_1000Waiting) {
  const uint64_t kIterations = 10000;
  const uint64_t kWaitingHandles = 1000;
  const std::string kMessage = "hello";
  MessagePipe pipe;
  HandleWatcher watcher;
  std::string received_message;

  struct TestData {
    MessagePipe pipe;
    HandleWatcher watcher;
  };
  ScopedVector<TestData> data_vector;
  for (uint64_t i = 0; i < kWaitingHandles; i++) {
    scoped_ptr<TestData> test_data(new TestData);
    ASSERT_TRUE(test_data->pipe.handle0.is_valid());
    test_data->watcher.Start(
        test_data->pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
        MOJO_DEADLINE_INDEFINITE, base::Bind(&NeverReached));
    data_vector.push_back(std::move(test_data));
  }

  ScopedPerfTimer timer("StartAndSignal_1000Waiting", GetMessageLoopName(),
                        kIterations);
  for (uint64_t i = 0; i < kIterations; i++) {
    base::RunLoop run_loop;
    watcher.Start(pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
                  MOJO_DEADLINE_INDEFINITE,
                  base::Bind(&OnWatcherSignaled, run_loop.QuitClosure()));
    ASSERT_TRUE(mojo::test::WriteTextMessage(pipe.handle1.get(), kMessage));
    run_loop.Run();
    watcher.Stop();

    ASSERT_TRUE(
        mojo::test::ReadTextMessage(pipe.handle0.get(), &received_message));
    EXPECT_EQ(kMessage, received_message);
    received_message.clear();
  }
}

}  // namespace test
}  // namespace common
}  // namespace mojo