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

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