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

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