Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(828)

Side by Side Diff: chrome/browser/metrics/perf_provider_chromeos_unittest.cc

Issue 1218583002: metrics: Add dbus interface for GetRandomPerfOutput (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Create TestSimpleTaskRunner and handle Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "chrome/browser/metrics/perf_provider_chromeos.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/test/test_simple_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/browser/metrics/windowed_incognito_observer.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/login/login_state.h"
17 #include "components/metrics/proto/sampled_profile.pb.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace metrics {
21
22 namespace {
23
24 // Return values for perf.
25 const int kPerfSuccess = 0;
26 const int kPerfFailure = 1;
27
28 // Converts a protobuf to serialized format as a byte vector.
29 std::vector<uint8_t> SerializeMessageToVector(
30 const google::protobuf::MessageLite& message) {
31 std::vector<uint8_t> result(message.ByteSize());
32 message.SerializeToArray(result.data(), result.size());
33 return result;
34 }
35
36 // Returns an example PerfDataProto. The contents don't have to make sense. They
37 // just need to constitute a semantically valid protobuf.
38 // |proto| is an output parameter that will contain the created protobuf.
39 PerfDataProto GetExamplePerfDataProto() {
40 PerfDataProto proto;
41 proto.set_timestamp_sec(1435604013); // Time since epoch in seconds->
42
43 PerfDataProto_PerfFileAttr* file_attr = proto.add_file_attrs();
44 file_attr->add_ids(61);
45 file_attr->add_ids(62);
46 file_attr->add_ids(63);
47
48 PerfDataProto_PerfEventAttr* attr = file_attr->mutable_attr();
49 attr->set_type(1);
50 attr->set_size(2);
51 attr->set_config(3);
52 attr->set_sample_period(4);
53 attr->set_sample_freq(5);
54
55 PerfDataProto_PerfEventStats* stats = proto.mutable_stats();
56 stats->set_num_events_read(100);
57 stats->set_num_sample_events(200);
58 stats->set_num_mmap_events(300);
59 stats->set_num_fork_events(400);
60 stats->set_num_exit_events(500);
61
62 return proto;
63 }
64
65 // Returns an example PerfStatProto. The contents don't have to make sense. They
66 // just need to constitute a semantically valid protobuf.
67 // |result| is an output parameter that will contain the created protobuf.
68 PerfStatProto GetExamplePerfStatProto() {
69 PerfStatProto proto;
70 proto.set_command_line(
71 "perf stat -a -e cycles -e instructions -e branches -- sleep 2");
72
73 PerfStatProto_PerfStatLine* line1 = proto.add_line();
74 line1->set_time_ms(1000);
75 line1->set_count(2000);
76 line1->set_event("cycles");
77
78 PerfStatProto_PerfStatLine* line2 = proto.add_line();
79 line2->set_time_ms(2000);
80 line2->set_count(5678);
81 line2->set_event("instructions");
82
83 PerfStatProto_PerfStatLine* line3 = proto.add_line();
84 line3->set_time_ms(3000);
85 line3->set_count(9999);
86 line3->set_event("branches");
87
88 return proto;
89 }
90
91 // Allows testing of PerfProvider behavior when an incognito window is opened.
92 class TestIncognitoObserver : public WindowedIncognitoObserver {
93 public:
94 // Factory function to create a TestIncognitoObserver object contained in a
95 // scoped_ptr<WindowedIncognitoObserver> object. |incognito_launched|
96 // simulates the presence of an open incognito window, or the lack thereof.
97 // Used for passing observers to ParseOutputProtoIfValid().
98 static scoped_ptr<WindowedIncognitoObserver> CreateWithIncognitoLaunched(
99 bool incognito_launched) {
100 scoped_ptr<TestIncognitoObserver> observer(new TestIncognitoObserver);
101 observer->set_incognito_launched(incognito_launched);
102 return observer.Pass();
103 }
104
105 private:
106 TestIncognitoObserver() {}
107
108 DISALLOW_COPY_AND_ASSIGN(TestIncognitoObserver);
109 };
110
111 // Allows access to PerfProvider::ParseOutputProtoIfValid() for testing.
112 class TestPerfProvider : public PerfProvider {
113 public:
114 TestPerfProvider() {}
115
116 using PerfProvider::ParseOutputProtoIfValid;
117
118 private:
119 std::vector<SampledProfile> stored_profiles_;
120
121 DISALLOW_COPY_AND_ASSIGN(TestPerfProvider);
122 };
123
124 } // namespace
125
126 class PerfProviderTest : public testing::Test {
127 public:
128 PerfProviderTest() : task_runner_(new base::TestSimpleTaskRunner),
129 task_runner_handle_(task_runner_),
130 perf_data_proto_(GetExamplePerfDataProto()),
131 perf_stat_proto_(GetExamplePerfStatProto()) {}
132
133 void SetUp() override {
134 // PerfProvider requires chromeos::LoginState and
135 // chromeos::DBusThreadManagerto be initialized.
136 chromeos::LoginState::Initialize();
137 chromeos::DBusThreadManager::Initialize();
138
139 perf_provider_.reset(new TestPerfProvider);
140
141 // PerfProvider requires the user to be logged in.
142 chromeos::LoginState::Get()->SetLoggedInState(
143 chromeos::LoginState::LOGGED_IN_ACTIVE,
144 chromeos::LoginState::LOGGED_IN_USER_REGULAR);
Ilya Sherman 2015/07/08 01:59:12 By the way, this looks like it's affecting global
Simon Que 2015/07/08 07:55:41 It shouldn't be, since TearDown() will call LoginS
145 }
146
147 void TearDown() override {
148 perf_provider_.reset();
149 chromeos::DBusThreadManager::Shutdown();
150 chromeos::LoginState::Shutdown();
151 }
152
153 protected:
154 scoped_ptr<TestPerfProvider> perf_provider_;
155
156 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
157 base::ThreadTaskRunnerHandle task_runner_handle_;
158
159 // These store example perf data/stat protobufs for testing.
160 PerfDataProto perf_data_proto_;
161 PerfStatProto perf_stat_proto_;
162
163 DISALLOW_COPY_AND_ASSIGN(PerfProviderTest);
164 };
165
166 TEST_F(PerfProviderTest, CheckSetup) {
167 EXPECT_GT(perf_data_proto_.ByteSize(), 0);
168 EXPECT_GT(perf_stat_proto_.ByteSize(), 0);
169
170 std::vector<SampledProfile> stored_profiles;
171 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
172 EXPECT_TRUE(stored_profiles.empty());
173
174 EXPECT_FALSE(
175 TestIncognitoObserver::CreateWithIncognitoLaunched(false)->
176 incognito_launched());
177 EXPECT_TRUE(
178 TestIncognitoObserver::CreateWithIncognitoLaunched(true)->
179 incognito_launched());
180 }
181
182 TEST_F(PerfProviderTest, NoPerfData) {
183 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
184 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
185
186 perf_provider_->ParseOutputProtoIfValid(
187 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
188 sampled_profile.Pass(),
189 kPerfSuccess,
190 std::vector<uint8_t>(),
191 std::vector<uint8_t>());
192
193 std::vector<SampledProfile> stored_profiles;
194 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
195 }
196
197 TEST_F(PerfProviderTest, PerfDataProtoOnly) {
198 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
199 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
200
201 perf_provider_->ParseOutputProtoIfValid(
202 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
203 sampled_profile.Pass(),
204 kPerfSuccess,
205 SerializeMessageToVector(perf_data_proto_),
206 std::vector<uint8_t>());
207
208 std::vector<SampledProfile> stored_profiles;
209 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles));
210 ASSERT_EQ(1U, stored_profiles.size());
211
212 const SampledProfile& profile = stored_profiles[0];
213 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
214 EXPECT_GT(profile.ms_after_login(), 0);
215
216 ASSERT_TRUE(profile.has_perf_data());
217 EXPECT_FALSE(profile.has_perf_stat());
218 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
219 SerializeMessageToVector(profile.perf_data()));
220 }
221
222 TEST_F(PerfProviderTest, PerfStatProtoOnly) {
223 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
224 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
225
226 perf_provider_->ParseOutputProtoIfValid(
227 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
228 sampled_profile.Pass(),
229 kPerfSuccess,
230 std::vector<uint8_t>(),
231 SerializeMessageToVector(perf_stat_proto_));
232
233 std::vector<SampledProfile> stored_profiles;
234 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles));
235 ASSERT_EQ(1U, stored_profiles.size());
236
237 const SampledProfile& profile = stored_profiles[0];
238 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
239 EXPECT_GT(profile.ms_after_login(), 0);
240
241 EXPECT_FALSE(profile.has_perf_data());
242 ASSERT_TRUE(profile.has_perf_stat());
243 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
244 SerializeMessageToVector(profile.perf_stat()));
245 }
246
247 TEST_F(PerfProviderTest, BothPerfDataProtoAndPerfStatProto) {
248 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
249 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
250
251 perf_provider_->ParseOutputProtoIfValid(
252 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
253 sampled_profile.Pass(),
254 kPerfSuccess,
255 SerializeMessageToVector(perf_data_proto_),
256 SerializeMessageToVector(perf_stat_proto_));
257
258 std::vector<SampledProfile> stored_profiles;
259 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
260 EXPECT_TRUE(stored_profiles.empty());
261 }
262
263 TEST_F(PerfProviderTest, InvalidPerfOutputResult) {
264 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
265 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
266
267 perf_provider_->ParseOutputProtoIfValid(
268 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
269 sampled_profile.Pass(),
270 kPerfFailure,
271 SerializeMessageToVector(perf_data_proto_),
272 std::vector<uint8_t>());
273
274 // Should not have been stored.
275 std::vector<SampledProfile> stored_profiles;
276 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
277 EXPECT_TRUE(stored_profiles.empty());
278 }
279
280 // Change |sampled_profile| between calls to ParseOutputProtoIfValid().
281 TEST_F(PerfProviderTest, MultipleCalls) {
282 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
283 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
284
285 perf_provider_->ParseOutputProtoIfValid(
286 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
287 sampled_profile.Pass(),
288 kPerfSuccess,
289 SerializeMessageToVector(perf_data_proto_),
290 std::vector<uint8_t>());
291
292 sampled_profile.reset(new SampledProfile);
293 sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION);
294 sampled_profile->set_ms_after_login(23456);
295 sampled_profile->set_ms_after_restore(3000);
296 perf_provider_->ParseOutputProtoIfValid(
297 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
298 sampled_profile.Pass(),
299 kPerfSuccess,
300 std::vector<uint8_t>(),
301 SerializeMessageToVector(perf_stat_proto_));
302
303 sampled_profile.reset(new SampledProfile);
304 sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
305 sampled_profile->set_ms_after_login(34567);
306 sampled_profile->set_suspend_duration_ms(60000);
307 sampled_profile->set_ms_after_resume(1500);
308 perf_provider_->ParseOutputProtoIfValid(
309 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
310 sampled_profile.Pass(),
311 kPerfSuccess,
312 SerializeMessageToVector(perf_data_proto_),
313 std::vector<uint8_t>());
314
315 sampled_profile.reset(new SampledProfile);
316 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
317 perf_provider_->ParseOutputProtoIfValid(
318 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
319 sampled_profile.Pass(),
320 kPerfSuccess,
321 std::vector<uint8_t>(),
322 SerializeMessageToVector(perf_stat_proto_));
323
324 std::vector<SampledProfile> stored_profiles;
325 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles));
326 ASSERT_EQ(4U, stored_profiles.size());
327
328 const SampledProfile& profile1 = stored_profiles[0];
329 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
330 EXPECT_GT(profile1.ms_after_login(), 0);
331 ASSERT_TRUE(profile1.has_perf_data());
332 EXPECT_FALSE(profile1.has_perf_stat());
333 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
334 SerializeMessageToVector(profile1.perf_data()));
335
336 const SampledProfile& profile2 = stored_profiles[1];
337 EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
338 EXPECT_GT(profile2.ms_after_login(), 0);
339 EXPECT_EQ(3000, profile2.ms_after_restore());
340 EXPECT_FALSE(profile2.has_perf_data());
341 ASSERT_TRUE(profile2.has_perf_stat());
342 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
343 SerializeMessageToVector(profile2.perf_stat()));
344
345 const SampledProfile& profile3 = stored_profiles[2];
346 EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
347 EXPECT_GT(profile3.ms_after_login(), 0);
348 EXPECT_EQ(60000, profile3.suspend_duration_ms());
349 EXPECT_EQ(1500, profile3.ms_after_resume());
350 ASSERT_TRUE(profile3.has_perf_data());
351 EXPECT_FALSE(profile3.has_perf_stat());
352 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
353 SerializeMessageToVector(profile3.perf_data()));
354
355 const SampledProfile& profile4 = stored_profiles[3];
356 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile4.trigger_event());
357 EXPECT_GT(profile4.ms_after_login(), 0);
358 EXPECT_FALSE(profile4.has_perf_data());
359 ASSERT_TRUE(profile4.has_perf_stat());
360 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
361 SerializeMessageToVector(profile4.perf_stat()));
362 }
363
364 // Simulate opening and closing of incognito window in between calls to
365 // ParseOutputProtoIfValid().
366 TEST_F(PerfProviderTest, IncognitoWindowOpened) {
367 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
368 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
369
370 perf_provider_->ParseOutputProtoIfValid(
371 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
372 sampled_profile.Pass(),
373 kPerfSuccess,
374 SerializeMessageToVector(perf_data_proto_),
375 std::vector<uint8_t>());
376
377 std::vector<SampledProfile> stored_profiles1;
378 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles1));
379 ASSERT_EQ(1U, stored_profiles1.size());
380
381 const SampledProfile& profile1 = stored_profiles1[0];
382 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
383 EXPECT_GT(profile1.ms_after_login(), 0);
384 ASSERT_TRUE(profile1.has_perf_data());
385 EXPECT_FALSE(profile1.has_perf_stat());
386 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
387 SerializeMessageToVector(profile1.perf_data()));
388
389 sampled_profile.reset(new SampledProfile);
390 sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION);
391 sampled_profile->set_ms_after_login(23456);
392 sampled_profile->set_ms_after_restore(3000);
393 perf_provider_->ParseOutputProtoIfValid(
394 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
395 sampled_profile.Pass(),
396 kPerfSuccess,
397 std::vector<uint8_t>(),
398 SerializeMessageToVector(perf_stat_proto_));
399
400 std::vector<SampledProfile> stored_profiles2;
401 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles2));
402 ASSERT_EQ(1U, stored_profiles2.size());
403
404 const SampledProfile& profile2 = stored_profiles2[0];
405 EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
406 EXPECT_GT(profile2.ms_after_login(), 0);
407 EXPECT_EQ(3000, profile2.ms_after_restore());
408 EXPECT_FALSE(profile2.has_perf_data());
409 ASSERT_TRUE(profile2.has_perf_stat());
410 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
411 SerializeMessageToVector(profile2.perf_stat()));
412
413 sampled_profile.reset(new SampledProfile);
414 sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
415 // An incognito window opens.
416 perf_provider_->ParseOutputProtoIfValid(
417 TestIncognitoObserver::CreateWithIncognitoLaunched(true),
418 sampled_profile.Pass(),
419 kPerfSuccess,
420 SerializeMessageToVector(perf_data_proto_),
421 std::vector<uint8_t>());
422
423 std::vector<SampledProfile> stored_profiles_empty;
424 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles_empty));
425
426 sampled_profile.reset(new SampledProfile);
427 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
428 // Incognito window is still open.
429 perf_provider_->ParseOutputProtoIfValid(
430 TestIncognitoObserver::CreateWithIncognitoLaunched(true),
431 sampled_profile.Pass(),
432 kPerfSuccess,
433 std::vector<uint8_t>(),
434 SerializeMessageToVector(perf_stat_proto_));
435
436 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles_empty));
437
438 sampled_profile.reset(new SampledProfile);
439 sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
440 sampled_profile->set_ms_after_login(34567);
441 sampled_profile->set_suspend_duration_ms(60000);
442 sampled_profile->set_ms_after_resume(1500);
443 // Incognito window closes.
444 perf_provider_->ParseOutputProtoIfValid(
445 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
446 sampled_profile.Pass(),
447 kPerfSuccess,
448 SerializeMessageToVector(perf_data_proto_),
449 std::vector<uint8_t>());
450
451 std::vector<SampledProfile> stored_profiles3;
452 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles3));
453 ASSERT_EQ(1U, stored_profiles3.size());
454
455 const SampledProfile& profile3 = stored_profiles3[0];
456 EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
457 EXPECT_GT(profile3.ms_after_login(), 0);
458 EXPECT_EQ(60000, profile3.suspend_duration_ms());
459 EXPECT_EQ(1500, profile3.ms_after_resume());
460 ASSERT_TRUE(profile3.has_perf_data());
461 EXPECT_FALSE(profile3.has_perf_stat());
462 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
463 SerializeMessageToVector(profile3.perf_data()));
464 }
465
466 } // namespace metrics
OLDNEW
« no previous file with comments | « chrome/browser/metrics/perf_provider_chromeos.cc ('k') | chrome/browser/metrics/windowed_incognito_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698