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

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: Do not allow both perf_stat and perf_data to be passed in; treat it as an error 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 "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/login/login_state.h"
14 #include "components/metrics/proto/sampled_profile.pb.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace metrics {
18
19 namespace {
20
21 // Returns an example PerfDataProto. The contents don't have to make sense. They
22 // just need to constitute a semantically valid protobuf.
23 // |result| is an output parameter that will contain the created protobuf.
24 // |result_raw| is |result| in serialized format.
25 void GetExamplePerfDataProto(PerfDataProto* result,
26 std::vector<uint8_t>* result_raw) {
27 PerfDataProto& proto = *result;
Ilya Sherman 2015/07/01 03:09:03 Again, why don't you use the arrow operator? It c
Simon Que 2015/07/01 22:14:11 Done.
28 proto.set_timestamp_sec(1435604013); // Time since epoch in seconds.
29
30 PerfDataProto_PerfFileAttr& file_attr = *proto.add_file_attrs();
Ilya Sherman 2015/07/01 03:09:02 I'd use a pointer here, too. In general, bare ref
Simon Que 2015/07/01 22:14:11 Done.
31 file_attr.add_ids(61);
32 file_attr.add_ids(62);
33 file_attr.add_ids(63);
34
35 PerfDataProto_PerfEventAttr& attr = *file_attr.mutable_attr();
36 attr.set_type(1);
37 attr.set_size(2);
38 attr.set_config(3);
39 attr.set_sample_period(4);
40 attr.set_sample_freq(5);
41
42 PerfDataProto_PerfEventStats& stats = *proto.mutable_stats();
43 stats.set_num_events_read(100);
44 stats.set_num_sample_events(200);
45 stats.set_num_mmap_events(300);
46 stats.set_num_fork_events(400);
47 stats.set_num_exit_events(500);
48
49 std::string result_string;
50 ASSERT_TRUE(proto.SerializeToString(&result_string));
51 *result_raw =
52 std::vector<uint8_t>(result_string.begin(), result_string.end());
53 }
54
55 // Returns an example PerfStatProto. The contents don't have to make sense. They
56 // just need to constitute a semantically valid protobuf.
57 // |result| is an output parameter that will contain the created protobuf.
58 // |result_raw| is |result| in serialized format.
59 void GetExamplePerfStatProto(PerfStatProto* result,
60 std::vector<uint8_t>* result_raw) {
61 PerfStatProto& proto = *result;
62 proto.set_command_line(
63 "perf stat -a -e cycles -e instructions -e branches -- sleep 2");
64
65 PerfStatProto_PerfStatLine& line1 = *proto.add_line();
66 line1.set_time_ms(1000);
67 line1.set_count(2000);
68 line1.set_event("cycles");
69
70 PerfStatProto_PerfStatLine& line2 = *proto.add_line();
71 line2.set_time_ms(2000);
72 line2.set_count(5678);
73 line2.set_event("instructions");
74
75 PerfStatProto_PerfStatLine& line3 = *proto.add_line();
76 line3.set_time_ms(3000);
77 line3.set_count(9999);
78 line3.set_event("branches");
79
80 std::string result_string;
81 ASSERT_TRUE(proto.SerializeToString(&result_string));
82 *result_raw =
83 std::vector<uint8_t>(result_string.begin(), result_string.end());
Ilya Sherman 2015/07/01 03:09:03 Why not either (a) return a string, or (b) call Se
Simon Que 2015/07/01 22:14:11 Done.
84 }
85
86 std::vector<uint8_t> SerializeMessageToVector(
87 const google::protobuf::MessageLite& message) {
88 std::vector<uint8_t> result(message.ByteSize());
89 message.SerializeToArray(result.data(), result.size());
90 return result;
91 }
92
93 } // namespace
94
95 using IncognitoObserverInterface = PerfProvider::IncognitoObserverInterface;
96
97 class PerfProviderTest : public testing::Test {
98 public:
99 // Allows testing of PerfProvider behavior when an incognito window is opened.
100 class MockIncognitoObserver : public IncognitoObserverInterface {
Ilya Sherman 2015/07/01 03:09:03 nit: Please name this "TestIncognitoObserver" or "
Simon Que 2015/07/01 21:08:03 Done.
101 public:
102 MockIncognitoObserver() : incognito_launched_(false) {}
103
104 ~MockIncognitoObserver() {}
105
106 bool incognito_launched() const override {
107 return incognito_launched_;
108 }
109 void set_incognito_launched(bool value) {
110 incognito_launched_ = value;
111 }
112
113 private:
114 bool incognito_launched_;
115 };
Ilya Sherman 2015/07/01 03:09:02 nit: Please move this class to the anonymous names
Simon Que 2015/07/01 21:08:02 Done.
116
117 PerfProviderTest() {
118 GetExamplePerfDataProto(&perf_data_proto_, &perf_data_raw_);
119 GetExamplePerfStatProto(&perf_stat_proto_, &perf_stat_raw_);
120 }
121
122 void SetUp() override {
123 // PerfProvider requires chromeos::LoginState and
124 // chromeos::DBusThreadManagerto be initialized.
125 chromeos::LoginState::Initialize();
126 chromeos::DBusThreadManager::Initialize();
127
128 perf_provider_.reset(new PerfProvider);
129 EXPECT_TRUE(cached_perf_data().empty());
Ilya Sherman 2015/07/01 03:09:03 Rather than running this in the setup for each tes
Simon Que 2015/07/01 22:14:11 Done.
130
131 EXPECT_FALSE(incognito_observer_.incognito_launched());\
Ilya Sherman 2015/07/01 03:09:03 It's a little weird to be setting expectations lik
Ilya Sherman 2015/07/01 03:09:03 nit: Please remove the trailing "\"
Simon Que 2015/07/01 22:14:11 Done.
Simon Que 2015/07/01 22:14:11 Done.
132
133 sampled_profile_.reset(new SampledProfile);
134 sampled_profile_->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
Ilya Sherman 2015/07/01 03:09:02 Could all of this be done in the constructor, and
135 }
136
137 void TearDown() override {
138 perf_provider_.reset(NULL);
Ilya Sherman 2015/07/01 03:09:02 nit: Just reset() is fine; but FYI, "nullptr" is p
Simon Que 2015/07/01 22:14:11 Done.
139 chromeos::DBusThreadManager::Shutdown();
140 chromeos::LoginState::Shutdown();
141
142 incognito_observer_.set_incognito_launched(false);
Ilya Sherman 2015/07/01 03:09:03 No need to do this, as the observer will be recrea
Simon Que 2015/07/01 22:14:11 Done.
143 }
144
145 protected:
146 // Accessor for derived classes to access |PerfProvider::cached_perf_data_|.
147 const std::vector<SampledProfile>& cached_perf_data() const {
148 return perf_provider_->cached_perf_data_;
149 }
Ilya Sherman 2015/07/01 03:09:02 nit: Tests should just access this directly. If t
Simon Que 2015/07/01 22:14:11 Done.
150
151 // Accessor for derived classes to call PerfProvider::ParseOutputProtoIfValid.
152 void ParseOutputProtoIfValid(
153 const MockIncognitoObserver& incognito_observer,
154 const SampledProfile& sampled_profile,
Ilya Sherman 2015/07/01 03:09:03 Why not just pass scoped_ptr's to this function di
Simon Que 2015/07/01 22:14:11 Done.
155 int result,
156 const std::vector<uint8>& perf_data,
157 const std::vector<uint8>& perf_stat) {
158 // Create copies of |incognito_observer| and |sampled_profile| to pass to
159 // PerfProvider::ParseOutputProtoIfValid.
160 MockIncognitoObserver* incognito_observer_copy = new MockIncognitoObserver;
Ilya Sherman 2015/07/01 03:09:03 nit: Please declare this as a scoped ptr. Also, c
Simon Que 2015/07/01 22:14:11 Done.
161 SampledProfile* sampled_profile_copy = new SampledProfile;
Ilya Sherman 2015/07/01 03:09:02 Ditto.
Simon Que 2015/07/01 22:14:11 Done
162
163 *incognito_observer_copy = incognito_observer;
164 // The contents of the protobuf cannot be copied using a simple assignment.
165 EXPECT_TRUE(sampled_profile_copy->ParseFromString(
166 sampled_profile.SerializeAsString()));
167
168 perf_provider_->PerfProvider::ParseOutputProtoIfValid(
169 scoped_ptr<IncognitoObserverInterface>(incognito_observer_copy).Pass(),
170 scoped_ptr<SampledProfile>(sampled_profile_copy).Pass(),
171 result,
172 perf_data,
173 perf_stat);
174 }
175
176 scoped_ptr<PerfProvider> perf_provider_;
177
178 // For simulating whether an incognito window is open.
179 MockIncognitoObserver incognito_observer_;
180
181 // The default template for the SampledProfile that will contain the incoming
182 // perf protobuf. Can be modified between calls to ParseOutputProtoIfValid().
183 // SetUp() resets it to the default value.
Ilya Sherman 2015/07/01 03:09:03 The constructor runs precisely as often as SetUp()
Simon Que 2015/07/01 22:14:11 Acknowledged.
184 scoped_ptr<SampledProfile> sampled_profile_;
Ilya Sherman 2015/07/01 03:09:02 IMO it would be clearer not to define this as part
Simon Que 2015/07/01 22:14:11 Done.
185
186 // These store example perf data/stat protobufs for testing.
187 PerfDataProto perf_data_proto_;
188 PerfStatProto perf_stat_proto_;
189 // These are the equivalents of the above protobufs as serialized raw data.
190 std::vector<uint8_t> perf_data_raw_;
191 std::vector<uint8_t> perf_stat_raw_;
Ilya Sherman 2015/07/01 03:09:03 Why do you need both variants here?
Simon Que 2015/07/01 22:14:11 It's used by BothPerfDataProtoAndPerfStatProto().
192
193 DISALLOW_COPY_AND_ASSIGN(PerfProviderTest);
194 };
195
196 TEST_F(PerfProviderTest, PerfDataProtoOnly) {
197 ParseOutputProtoIfValid(incognito_observer_,
198 *sampled_profile_,
199 0, // Zero means successful.
Ilya Sherman 2015/07/01 03:09:03 nit: Perhaps define constants like "const int kSuc
Simon Que 2015/07/01 22:14:11 Done.
200 perf_data_raw_,
201 std::vector<uint8_t>());
202 ASSERT_EQ(1U, cached_perf_data().size());
203
204 const SampledProfile& profile = cached_perf_data()[0];
205 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
206 EXPECT_GT(profile.ms_after_login(), 0);
207
208 ASSERT_TRUE(profile.has_perf_data());
209 EXPECT_FALSE(profile.has_perf_stat());
210 EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile.perf_data()));
211 }
212
213 TEST_F(PerfProviderTest, PerfStatProtoOnly) {
214 ParseOutputProtoIfValid(incognito_observer_,
215 *sampled_profile_,
216 0,
217 std::vector<uint8_t>(),
218 perf_stat_raw_);
219 ASSERT_EQ(1U, cached_perf_data().size());
220
221 const SampledProfile& profile = cached_perf_data()[0];
222 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
223 EXPECT_GT(profile.ms_after_login(), 0);
224
225 EXPECT_FALSE(profile.has_perf_data());
226 ASSERT_TRUE(profile.has_perf_stat());
227 EXPECT_EQ(perf_stat_raw_, SerializeMessageToVector(profile.perf_stat()));
228 }
229
230 TEST_F(PerfProviderTest, BothPerfDataProtoAndPerfStatProto) {
231 ParseOutputProtoIfValid(incognito_observer_,
232 *sampled_profile_,
233 0,
234 perf_data_raw_,
235 perf_stat_raw_);
236 ASSERT_TRUE(cached_perf_data().empty());
237 }
238
239 TEST_F(PerfProviderTest, InvalidPerfOutputResult) {
240 ParseOutputProtoIfValid(incognito_observer_,
241 *sampled_profile_,
242 1, // Nonzero means unsuccessful.
243 perf_data_raw_,
244 std::vector<uint8_t>());
245
246 // Should not have been stored.
247 EXPECT_TRUE(cached_perf_data().empty());
248 }
249
250 // Change |sampled_profile_| between calls to ParseOutputProtoIfValid().
251 TEST_F(PerfProviderTest, MultipleCalls) {
252 ParseOutputProtoIfValid(incognito_observer_,
253 *sampled_profile_,
254 0,
255 perf_data_raw_,
256 std::vector<uint8_t>());
257 EXPECT_EQ(1U, cached_perf_data().size());
258
259 sampled_profile_.reset(new SampledProfile);
260 sampled_profile_->set_trigger_event(SampledProfile::RESTORE_SESSION);
261 sampled_profile_->set_ms_after_login(23456);
262 sampled_profile_->set_ms_after_restore(3000);
263 ParseOutputProtoIfValid(incognito_observer_,
264 *sampled_profile_,
265 0,
266 std::vector<uint8_t>(),
267 perf_stat_raw_);
268 EXPECT_EQ(2U, cached_perf_data().size());
269
270 sampled_profile_.reset(new SampledProfile);
271 sampled_profile_->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
272 sampled_profile_->set_ms_after_login(34567);
273 sampled_profile_->set_suspend_duration_ms(60000);
274 sampled_profile_->set_ms_after_resume(1500);
275 ParseOutputProtoIfValid(incognito_observer_,
276 *sampled_profile_,
277 0,
278 perf_data_raw_,
279 std::vector<uint8_t>());
280 EXPECT_EQ(3U, cached_perf_data().size());
281
282 sampled_profile_.reset(new SampledProfile);
283 sampled_profile_->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
284 ParseOutputProtoIfValid(incognito_observer_,
285 *sampled_profile_,
286 0,
287 std::vector<uint8_t>(),
288 perf_stat_raw_);
289 ASSERT_EQ(4U, cached_perf_data().size());
290
291 const SampledProfile& profile1 = cached_perf_data()[0];
292 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
293 EXPECT_GT(profile1.ms_after_login(), 0);
294 ASSERT_TRUE(profile1.has_perf_data());
295 EXPECT_FALSE(profile1.has_perf_stat());
296 EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile1.perf_data()));
297
298 const SampledProfile& profile2 = cached_perf_data()[1];
299 EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
300 EXPECT_GT(profile2.ms_after_login(), 0);
301 EXPECT_EQ(3000, profile2.ms_after_restore());
302 EXPECT_FALSE(profile2.has_perf_data());
303 ASSERT_TRUE(profile2.has_perf_stat());
304 EXPECT_EQ(perf_stat_raw_, SerializeMessageToVector(profile2.perf_stat()));
305
306 const SampledProfile& profile3 = cached_perf_data()[2];
307 EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
308 EXPECT_GT(profile3.ms_after_login(), 0);
309 EXPECT_EQ(60000, profile3.suspend_duration_ms());
310 EXPECT_EQ(1500, profile3.ms_after_resume());
311 ASSERT_TRUE(profile3.has_perf_data());
312 EXPECT_FALSE(profile3.has_perf_stat());
313 EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile3.perf_data()));
314
315 const SampledProfile& profile4 = cached_perf_data()[3];
316 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile4.trigger_event());
317 EXPECT_GT(profile4.ms_after_login(), 0);
318 EXPECT_FALSE(profile4.has_perf_data());
319 ASSERT_TRUE(profile4.has_perf_stat());
320 EXPECT_EQ(perf_stat_raw_, SerializeMessageToVector(profile4.perf_stat()));
321 }
322
323 // Simulate opening and closing of incognito window in between calls to
324 // ParseOutputProtoIfValid().
325 TEST_F(PerfProviderTest, IncognitoWindowOpened) {
326 ParseOutputProtoIfValid(incognito_observer_,
327 *sampled_profile_,
328 0,
329 perf_data_raw_,
330 std::vector<uint8_t>());
331 EXPECT_EQ(1U, cached_perf_data().size());
332
333 sampled_profile_.reset(new SampledProfile);
334 sampled_profile_->set_trigger_event(SampledProfile::RESTORE_SESSION);
335 sampled_profile_->set_ms_after_login(23456);
336 sampled_profile_->set_ms_after_restore(3000);
337 ParseOutputProtoIfValid(incognito_observer_,
338 *sampled_profile_,
339 0,
340 std::vector<uint8_t>(),
341 perf_stat_raw_);
342 EXPECT_EQ(2U, cached_perf_data().size());
343
344 // An incognito window opens.
345 incognito_observer_.set_incognito_launched(true);
346
347 sampled_profile_.reset(new SampledProfile);
348 sampled_profile_->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
349 ParseOutputProtoIfValid(incognito_observer_,
350 *sampled_profile_,
351 0,
352 perf_data_raw_,
353 std::vector<uint8_t>());
354 EXPECT_EQ(2U, cached_perf_data().size());
355
356 sampled_profile_.reset(new SampledProfile);
357 sampled_profile_->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
358 ParseOutputProtoIfValid(incognito_observer_,
359 *sampled_profile_,
360 0,
361 std::vector<uint8_t>(),
362 perf_stat_raw_);
363 ASSERT_EQ(2U, cached_perf_data().size());
364
365 // Incognito window closes, should be good to go again.
366 incognito_observer_.set_incognito_launched(false);
367
368 sampled_profile_.reset(new SampledProfile);
369 sampled_profile_->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
370 sampled_profile_->set_ms_after_login(34567);
371 sampled_profile_->set_suspend_duration_ms(60000);
372 sampled_profile_->set_ms_after_resume(1500);
373 ParseOutputProtoIfValid(incognito_observer_,
374 *sampled_profile_,
375 0,
376 perf_data_raw_,
377 std::vector<uint8_t>());
378 EXPECT_EQ(3U, cached_perf_data().size());
379
380 const SampledProfile& profile1 = cached_perf_data()[0];
381 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
382 EXPECT_GT(profile1.ms_after_login(), 0);
383 ASSERT_TRUE(profile1.has_perf_data());
384 EXPECT_FALSE(profile1.has_perf_stat());
385 EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile1.perf_data()));
386
387 const SampledProfile& profile2 = cached_perf_data()[1];
388 EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
389 EXPECT_GT(profile2.ms_after_login(), 0);
390 EXPECT_EQ(3000, profile2.ms_after_restore());
391 EXPECT_FALSE(profile2.has_perf_data());
392 ASSERT_TRUE(profile2.has_perf_stat());
393 EXPECT_EQ(perf_stat_raw_, SerializeMessageToVector(profile2.perf_stat()));
394
395 const SampledProfile& profile3 = cached_perf_data()[2];
396 EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
397 EXPECT_GT(profile3.ms_after_login(), 0);
398 EXPECT_EQ(60000, profile3.suspend_duration_ms());
399 EXPECT_EQ(1500, profile3.ms_after_resume());
400 ASSERT_TRUE(profile3.has_perf_data());
401 EXPECT_FALSE(profile3.has_perf_stat());
402 EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile3.perf_data()));
403 }
404
405 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698