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

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: Fixed CreateWithIncognitoLaunched() 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 // Factory function to create a TestIncognitoObserver object contained in a
93 // scoped_ptr<WindowedIncognitoObserver> object. |incognito_launched|
94 // simulates the presence of an open incognito window, or the lack thereof.
95 // Used for passing observers to ParseOutputProtoIfValid().
96 static scoped_ptr<WindowedIncognitoObserver> CreateWithIncognitoLaunched(
97 bool incognito_launched) {
98 TestIncognitoObserver* observer = new TestIncognitoObserver;
Simon Que 2015/07/07 01:32:57 I tried to create a scoped_ptr<WindowedIncognitoOb
Ilya Sherman 2015/07/07 01:33:05 Please declare this as a scoped_ptr.
Ilya Sherman 2015/07/07 01:33:52 Ah, you want a scoped_ptr<TestIncognitoObserver>.
99 observer->set_incognito_launched(incognito_launched);
100 return scoped_ptr<WindowedIncognitoObserver>(observer);
Ilya Sherman 2015/07/07 01:33:05 And this line would just be "return observer;"
101 }
102
103 private:
104 TestIncognitoObserver() {}
105
106 DISALLOW_COPY_AND_ASSIGN(TestIncognitoObserver);
107 };
108
109 // Allows access to PerfProvider::ParseOutputProtoIfValid() for testing.
110 class TestPerfProvider : public PerfProvider {
111 public:
112 TestPerfProvider() {}
113
114 using PerfProvider::ParseOutputProtoIfValid;
115
116 private:
117 std::vector<SampledProfile> stored_profiles_;
118
119 DISALLOW_COPY_AND_ASSIGN(TestPerfProvider);
120 };
121
122 } // namespace
123
124 class PerfProviderTest : public testing::Test {
125 public:
126 PerfProviderTest() : perf_data_proto_(GetExamplePerfDataProto()),
127 perf_stat_proto_(GetExamplePerfStatProto()) {}
128
129 void SetUp() override {
130 // PerfProvider requires chromeos::LoginState and
131 // chromeos::DBusThreadManagerto be initialized.
132 chromeos::LoginState::Initialize();
133 chromeos::DBusThreadManager::Initialize();
134
135 perf_provider_.reset(new TestPerfProvider);
136 }
137
138 void TearDown() override {
139 perf_provider_.reset();
140 chromeos::DBusThreadManager::Shutdown();
141 chromeos::LoginState::Shutdown();
142 }
143
144 protected:
145 scoped_ptr<TestPerfProvider> perf_provider_;
146
147 // These store example perf data/stat protobufs for testing.
148 PerfDataProto perf_data_proto_;
149 PerfStatProto perf_stat_proto_;
150
151 DISALLOW_COPY_AND_ASSIGN(PerfProviderTest);
152 };
153
154 TEST_F(PerfProviderTest, CheckSetup) {
155 EXPECT_GT(perf_data_proto_.ByteSize(), 0);
156 EXPECT_GT(perf_stat_proto_.ByteSize(), 0);
157
158 std::vector<SampledProfile> stored_profiles;
159 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
160 EXPECT_TRUE(stored_profiles.empty());
161
162 EXPECT_FALSE(
163 TestIncognitoObserver::CreateWithIncognitoLaunched(false)->
164 incognito_launched());
165 EXPECT_TRUE(
166 TestIncognitoObserver::CreateWithIncognitoLaunched(true)->
167 incognito_launched());
168 }
169
170 TEST_F(PerfProviderTest, NoPerfData) {
171 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
172 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
173
174 perf_provider_->ParseOutputProtoIfValid(
175 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
176 sampled_profile.Pass(),
177 kPerfSuccess,
178 std::vector<uint8_t>(),
179 std::vector<uint8_t>());
180
181 std::vector<SampledProfile> stored_profiles;
182 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
183 }
184
185 TEST_F(PerfProviderTest, PerfDataProtoOnly) {
186 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
187 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
188
189 perf_provider_->ParseOutputProtoIfValid(
190 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
191 sampled_profile.Pass(),
192 kPerfSuccess,
193 SerializeMessageToVector(perf_data_proto_),
194 std::vector<uint8_t>());
195
196 std::vector<SampledProfile> stored_profiles;
197 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles));
198 ASSERT_EQ(1U, stored_profiles.size());
199
200 const SampledProfile& profile = stored_profiles[0];
201 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
202 EXPECT_GT(profile.ms_after_login(), 0);
203
204 ASSERT_TRUE(profile.has_perf_data());
205 EXPECT_FALSE(profile.has_perf_stat());
206 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
207 SerializeMessageToVector(profile.perf_data()));
208 }
209
210 TEST_F(PerfProviderTest, PerfStatProtoOnly) {
211 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
212 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
213
214 perf_provider_->ParseOutputProtoIfValid(
215 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
216 sampled_profile.Pass(),
217 kPerfSuccess,
218 std::vector<uint8_t>(),
219 SerializeMessageToVector(perf_stat_proto_));
220
221 std::vector<SampledProfile> stored_profiles;
222 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles));
223 ASSERT_EQ(1U, stored_profiles.size());
224
225 const SampledProfile& profile = stored_profiles[0];
226 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
227 EXPECT_GT(profile.ms_after_login(), 0);
228
229 EXPECT_FALSE(profile.has_perf_data());
230 ASSERT_TRUE(profile.has_perf_stat());
231 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
232 SerializeMessageToVector(profile.perf_stat()));
233 }
234
235 TEST_F(PerfProviderTest, BothPerfDataProtoAndPerfStatProto) {
236 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
237 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
238
239 perf_provider_->ParseOutputProtoIfValid(
240 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
241 sampled_profile.Pass(),
242 kPerfSuccess,
243 SerializeMessageToVector(perf_data_proto_),
244 SerializeMessageToVector(perf_stat_proto_));
245
246 std::vector<SampledProfile> stored_profiles;
247 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
248 EXPECT_TRUE(stored_profiles.empty());
249 }
250
251 TEST_F(PerfProviderTest, InvalidPerfOutputResult) {
252 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
253 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
254
255 perf_provider_->ParseOutputProtoIfValid(
256 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
257 sampled_profile.Pass(),
258 kPerfFailure,
259 SerializeMessageToVector(perf_data_proto_),
260 std::vector<uint8_t>());
261
262 // Should not have been stored.
263 std::vector<SampledProfile> stored_profiles;
264 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
265 EXPECT_TRUE(stored_profiles.empty());
266 }
267
268 // Change |sampled_profile| between calls to ParseOutputProtoIfValid().
269 TEST_F(PerfProviderTest, MultipleCalls) {
270 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
271 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
272
273 perf_provider_->ParseOutputProtoIfValid(
274 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
275 sampled_profile.Pass(),
276 kPerfSuccess,
277 SerializeMessageToVector(perf_data_proto_),
278 std::vector<uint8_t>());
279
280 sampled_profile.reset(new SampledProfile);
281 sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION);
282 sampled_profile->set_ms_after_login(23456);
283 sampled_profile->set_ms_after_restore(3000);
284 perf_provider_->ParseOutputProtoIfValid(
285 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
286 sampled_profile.Pass(),
287 kPerfSuccess,
288 std::vector<uint8_t>(),
289 SerializeMessageToVector(perf_stat_proto_));
290
291 sampled_profile.reset(new SampledProfile);
292 sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
293 sampled_profile->set_ms_after_login(34567);
294 sampled_profile->set_suspend_duration_ms(60000);
295 sampled_profile->set_ms_after_resume(1500);
296 perf_provider_->ParseOutputProtoIfValid(
297 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
298 sampled_profile.Pass(),
299 kPerfSuccess,
300 SerializeMessageToVector(perf_data_proto_),
301 std::vector<uint8_t>());
302
303 sampled_profile.reset(new SampledProfile);
304 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
305 perf_provider_->ParseOutputProtoIfValid(
306 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
307 sampled_profile.Pass(),
308 kPerfSuccess,
309 std::vector<uint8_t>(),
310 SerializeMessageToVector(perf_stat_proto_));
311
312 std::vector<SampledProfile> stored_profiles;
313 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles));
314 ASSERT_EQ(4U, stored_profiles.size());
315
316 const SampledProfile& profile1 = stored_profiles[0];
317 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
318 EXPECT_GT(profile1.ms_after_login(), 0);
319 ASSERT_TRUE(profile1.has_perf_data());
320 EXPECT_FALSE(profile1.has_perf_stat());
321 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
322 SerializeMessageToVector(profile1.perf_data()));
323
324 const SampledProfile& profile2 = stored_profiles[1];
325 EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
326 EXPECT_GT(profile2.ms_after_login(), 0);
327 EXPECT_EQ(3000, profile2.ms_after_restore());
328 EXPECT_FALSE(profile2.has_perf_data());
329 ASSERT_TRUE(profile2.has_perf_stat());
330 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
331 SerializeMessageToVector(profile2.perf_stat()));
332
333 const SampledProfile& profile3 = stored_profiles[2];
334 EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
335 EXPECT_GT(profile3.ms_after_login(), 0);
336 EXPECT_EQ(60000, profile3.suspend_duration_ms());
337 EXPECT_EQ(1500, profile3.ms_after_resume());
338 ASSERT_TRUE(profile3.has_perf_data());
339 EXPECT_FALSE(profile3.has_perf_stat());
340 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
341 SerializeMessageToVector(profile3.perf_data()));
342
343 const SampledProfile& profile4 = stored_profiles[3];
344 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile4.trigger_event());
345 EXPECT_GT(profile4.ms_after_login(), 0);
346 EXPECT_FALSE(profile4.has_perf_data());
347 ASSERT_TRUE(profile4.has_perf_stat());
348 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
349 SerializeMessageToVector(profile4.perf_stat()));
350 }
351
352 // Simulate opening and closing of incognito window in between calls to
353 // ParseOutputProtoIfValid().
354 TEST_F(PerfProviderTest, IncognitoWindowOpened) {
355 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
356 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
357
358 perf_provider_->ParseOutputProtoIfValid(
359 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
360 sampled_profile.Pass(),
361 kPerfSuccess,
362 SerializeMessageToVector(perf_data_proto_),
363 std::vector<uint8_t>());
364
365 std::vector<SampledProfile> stored_profiles1;
366 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles1));
367 ASSERT_EQ(1U, stored_profiles1.size());
368
369 const SampledProfile& profile1 = stored_profiles1[0];
370 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
371 EXPECT_GT(profile1.ms_after_login(), 0);
372 ASSERT_TRUE(profile1.has_perf_data());
373 EXPECT_FALSE(profile1.has_perf_stat());
374 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
375 SerializeMessageToVector(profile1.perf_data()));
376
377 sampled_profile.reset(new SampledProfile);
378 sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION);
379 sampled_profile->set_ms_after_login(23456);
380 sampled_profile->set_ms_after_restore(3000);
381 perf_provider_->ParseOutputProtoIfValid(
382 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
383 sampled_profile.Pass(),
384 kPerfSuccess,
385 std::vector<uint8_t>(),
386 SerializeMessageToVector(perf_stat_proto_));
387
388 std::vector<SampledProfile> stored_profiles2;
389 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles2));
390 ASSERT_EQ(1U, stored_profiles2.size());
391
392 const SampledProfile& profile2 = stored_profiles2[0];
393 EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
394 EXPECT_GT(profile2.ms_after_login(), 0);
395 EXPECT_EQ(3000, profile2.ms_after_restore());
396 EXPECT_FALSE(profile2.has_perf_data());
397 ASSERT_TRUE(profile2.has_perf_stat());
398 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
399 SerializeMessageToVector(profile2.perf_stat()));
400
401 sampled_profile.reset(new SampledProfile);
402 sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
403 // An incognito window opens.
404 perf_provider_->ParseOutputProtoIfValid(
405 TestIncognitoObserver::CreateWithIncognitoLaunched(true),
406 sampled_profile.Pass(),
407 kPerfSuccess,
408 SerializeMessageToVector(perf_data_proto_),
409 std::vector<uint8_t>());
410
411 std::vector<SampledProfile> stored_profiles_empty;
412 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles_empty));
413
414 sampled_profile.reset(new SampledProfile);
415 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
416 // Incognito window is still open.
417 perf_provider_->ParseOutputProtoIfValid(
418 TestIncognitoObserver::CreateWithIncognitoLaunched(true),
419 sampled_profile.Pass(),
420 kPerfSuccess,
421 std::vector<uint8_t>(),
422 SerializeMessageToVector(perf_stat_proto_));
423
424 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles_empty));
425
426 sampled_profile.reset(new SampledProfile);
427 sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
428 sampled_profile->set_ms_after_login(34567);
429 sampled_profile->set_suspend_duration_ms(60000);
430 sampled_profile->set_ms_after_resume(1500);
431 // Incognito window closes.
432 perf_provider_->ParseOutputProtoIfValid(
433 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
434 sampled_profile.Pass(),
435 kPerfSuccess,
436 SerializeMessageToVector(perf_data_proto_),
437 std::vector<uint8_t>());
438
439 std::vector<SampledProfile> stored_profiles3;
440 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles3));
441 ASSERT_EQ(1U, stored_profiles3.size());
442
443 const SampledProfile& profile3 = stored_profiles3[0];
444 EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
445 EXPECT_GT(profile3.ms_after_login(), 0);
446 EXPECT_EQ(60000, profile3.suspend_duration_ms());
447 EXPECT_EQ(1500, profile3.ms_after_resume());
448 ASSERT_TRUE(profile3.has_perf_data());
449 EXPECT_FALSE(profile3.has_perf_stat());
450 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
451 SerializeMessageToVector(profile3.perf_data()));
452 }
453
454 } // 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