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

Unified 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 remaining comments Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/metrics/perf_provider_chromeos_unittest.cc
diff --git a/chrome/browser/metrics/perf_provider_chromeos_unittest.cc b/chrome/browser/metrics/perf_provider_chromeos_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3e077a08125b1d1299817c38d1d50daf40cf2af4
--- /dev/null
+++ b/chrome/browser/metrics/perf_provider_chromeos_unittest.cc
@@ -0,0 +1,432 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/metrics/perf_provider_chromeos.h"
+
+#include <string>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
+#include "chrome/browser/metrics/windowed_incognito_observer.h"
+#include "chromeos/dbus/dbus_thread_manager.h"
+#include "chromeos/login/login_state.h"
+#include "components/metrics/proto/sampled_profile.pb.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace metrics {
+
+namespace {
+
+// Return values for perf.
+const int kPerfSuccess = 0;
+const int kPerfFailure = 1;
+
+// Converts a protobuf to serialized format as a byte vector.
+std::vector<uint8_t> SerializeMessageToVector(
+ const google::protobuf::MessageLite& message) {
+ std::vector<uint8_t> result(message.ByteSize());
+ message.SerializeToArray(result.data(), result.size());
+ return result;
+}
+
+// Returns an example PerfDataProto. The contents don't have to make sense. They
+// just need to constitute a semantically valid protobuf.
+// |proto| is an output parameter that will contain the created protobuf.
+// Also returns the serialized form of |*proto| as a byte vector.
+std::vector<uint8_t> GetExamplePerfDataProto(PerfDataProto* proto) {
+ proto->set_timestamp_sec(1435604013); // Time since epoch in seconds->
+
+ PerfDataProto_PerfFileAttr* file_attr = proto->add_file_attrs();
+ file_attr->add_ids(61);
+ file_attr->add_ids(62);
+ file_attr->add_ids(63);
+
+ PerfDataProto_PerfEventAttr* attr = file_attr->mutable_attr();
+ attr->set_type(1);
+ attr->set_size(2);
+ attr->set_config(3);
+ attr->set_sample_period(4);
+ attr->set_sample_freq(5);
+
+ PerfDataProto_PerfEventStats* stats = proto->mutable_stats();
+ stats->set_num_events_read(100);
+ stats->set_num_sample_events(200);
+ stats->set_num_mmap_events(300);
+ stats->set_num_fork_events(400);
+ stats->set_num_exit_events(500);
+
+ return SerializeMessageToVector(*proto);
+}
+
+// Returns an example PerfStatProto. The contents don't have to make sense. They
+// just need to constitute a semantically valid protobuf.
+// |result| is an output parameter that will contain the created protobuf.
+// |result_raw| is |result| in serialized format.
+std::vector<uint8_t> GetExamplePerfStatProto(PerfStatProto* proto) {
+ proto->set_command_line(
+ "perf stat -a -e cycles -e instructions -e branches -- sleep 2");
+
+ PerfStatProto_PerfStatLine* line1 = proto->add_line();
+ line1->set_time_ms(1000);
+ line1->set_count(2000);
+ line1->set_event("cycles");
+
+ PerfStatProto_PerfStatLine* line2 = proto->add_line();
+ line2->set_time_ms(2000);
+ line2->set_count(5678);
+ line2->set_event("instructions");
+
+ PerfStatProto_PerfStatLine* line3 = proto->add_line();
+ line3->set_time_ms(3000);
+ line3->set_count(9999);
+ line3->set_event("branches");
+
+ return SerializeMessageToVector(*proto);
+}
+
+// Allows testing of PerfProvider behavior when an incognito window is opened.
+class TestIncognitoObserver : public WindowedIncognitoObserver {
+ public:
+ void set_incognito_launched(bool value) {
+ incognito_launched_ = value;
+ }
+};
Ilya Sherman 2015/07/02 00:40:16 nit: DISALLOW_COPY_AND_ASSIGN
Simon Que 2015/07/02 18:53:02 Done.
+
+// Allows access to PerfProvider::ParseOutputProtoIfValid() for testing.
+class PerfProviderForTesting : public PerfProvider {
Ilya Sherman 2015/07/02 00:40:16 Optional nit: I'd name this "TestPerfProvider", fo
Simon Que 2015/07/02 18:53:02 Done.
+ public:
+ void ParseOutputProtoIfValidForTesting(
+ const scoped_ptr<TestIncognitoObserver>& incognito_observer,
Ilya Sherman 2015/07/02 00:40:16 I'd expect that you could replace this entire meth
Simon Que 2015/07/06 00:50:34 Done.
+ const SampledProfile& sampled_profile,
+ int result,
+ const std::vector<uint8>& perf_data,
+ const std::vector<uint8>& perf_stat) {
+ // Create copies of |incognito_observer| and |sampled_profile| to pass to
+ // PerfProvider::ParseOutputProtoIfValid.
+ scoped_ptr<WindowedIncognitoObserver> incognito_observer_copy(
+ new TestIncognitoObserver(*incognito_observer));
Ilya Sherman 2015/07/02 00:40:16 Why do you make a copy, rather than passing in the
Simon Que 2015/07/02 18:53:02 The object will get destroyed by ParseOutputProtoI
+ scoped_ptr<SampledProfile> sampled_profile_copy(
+ new SampledProfile(sampled_profile));
Ilya Sherman 2015/07/02 00:40:16 Ditto.
+
+ ParseOutputProtoIfValid(incognito_observer_copy.Pass(),
+ sampled_profile_copy.Pass(),
+ result,
+ perf_data,
+ perf_stat);
+
+ // PerfProvider does not provide a direct accessor for the SampledProfiles
+ // stored by ParseOutputProtoIfValid. Instead, use the public method
+ // GetSampledProfiles() to append them to |stored_profiles_|.
+ std::vector<SampledProfile> sampled_profiles;
+ if (GetSampledProfiles(&sampled_profiles)) {
+ for (SampledProfile& profile : sampled_profiles) {
+ stored_profiles_.push_back(SampledProfile());
+ stored_profiles_.back().Swap(&profile);
+ }
+ }
Ilya Sherman 2015/07/02 00:40:16 Please call GetSampledProfiles() directly from the
Simon Que 2015/07/02 18:53:01 Done.
+ }
+
+ // Used to accumulate SampledProfiles containing perf data passed into
+ // ParseOutputProtoIfValidForTesting().
+ const std::vector<SampledProfile> stored_profiles() const {
Ilya Sherman 2015/07/02 00:40:16 nit: I'd recommend omitting this method entirely;
Simon Que 2015/07/02 18:53:01 Done.
+ return stored_profiles_;
+ }
+
+ private:
+ std::vector<SampledProfile> stored_profiles_;
+};
Ilya Sherman 2015/07/02 00:40:16 nit: DISALLOW_COPY_AND_ASSIGN
Simon Que 2015/07/02 18:53:02 Done.
+
+} // namespace
+
+class PerfProviderTest : public testing::Test {
+ public:
+ PerfProviderTest() {
+ perf_data_raw_ = GetExamplePerfDataProto(&perf_data_proto_);
+ perf_stat_raw_ = GetExamplePerfStatProto(&perf_stat_proto_);
+ }
+
+ void SetUp() override {
+ // PerfProvider requires chromeos::LoginState and
+ // chromeos::DBusThreadManagerto be initialized.
+ chromeos::LoginState::Initialize();
+ chromeos::DBusThreadManager::Initialize();
+
+ perf_provider_.reset(new PerfProviderForTesting);
+ incognito_observer_.reset(new TestIncognitoObserver);
+ }
+
+ void TearDown() override {
+ perf_provider_.reset();
+ chromeos::DBusThreadManager::Shutdown();
+ chromeos::LoginState::Shutdown();
+ }
+
+ protected:
+ scoped_ptr<PerfProviderForTesting> perf_provider_;
+
+ // For simulating whether an incognito window is open.
+ scoped_ptr<TestIncognitoObserver> incognito_observer_;
+
+ // These store example perf data/stat protobufs for testing.
+ PerfDataProto perf_data_proto_;
+ PerfStatProto perf_stat_proto_;
+ // These are the equivalents of the above protobufs as serialized raw data.
+ std::vector<uint8_t> perf_data_raw_;
+ std::vector<uint8_t> perf_stat_raw_;
Ilya Sherman 2015/07/02 00:40:16 What I meant was: Why do you need to store the pro
Simon Que 2015/07/02 18:53:02 Done.
+
+ DISALLOW_COPY_AND_ASSIGN(PerfProviderTest);
+};
+
+TEST_F(PerfProviderTest, CheckSetup) {
+ EXPECT_GT(perf_data_proto_.ByteSize(), 0);
+ EXPECT_GT(perf_stat_proto_.ByteSize(), 0);
+ EXPECT_FALSE(perf_data_raw_.empty());
+ EXPECT_FALSE(perf_stat_raw_.empty());
+
+ EXPECT_TRUE(perf_provider_->stored_profiles().empty());
+ EXPECT_FALSE(incognito_observer_->incognito_launched());
+}
+
+TEST_F(PerfProviderTest, PerfDataProtoOnly) {
+ SampledProfile sampled_profile;
+ sampled_profile.set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
+
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ sampled_profile,
+ kPerfSuccess,
+ perf_data_raw_,
+ std::vector<uint8_t>());
+ ASSERT_EQ(1U, perf_provider_->stored_profiles().size());
+
+ const SampledProfile& profile = perf_provider_->stored_profiles()[0];
+ EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
+ EXPECT_GT(profile.ms_after_login(), 0);
+
+ ASSERT_TRUE(profile.has_perf_data());
+ EXPECT_FALSE(profile.has_perf_stat());
+ EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile.perf_data()));
Simon Que 2015/07/01 22:16:07 Calling ByteSize() in SerializeMessageToVector() f
Ilya Sherman 2015/07/02 00:40:16 I'm also not sure. You could try gdb/lldb.
Simon Que 2015/07/02 18:53:01 Don't see this anymore.
+}
+
+TEST_F(PerfProviderTest, PerfStatProtoOnly) {
+ SampledProfile sampled_profile;
+ sampled_profile.set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
+
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ sampled_profile,
+ kPerfSuccess,
+ std::vector<uint8_t>(),
+ perf_stat_raw_);
+ ASSERT_EQ(1U, perf_provider_->stored_profiles().size());
+
+ const SampledProfile& profile = perf_provider_->stored_profiles()[0];
+ EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
+ EXPECT_GT(profile.ms_after_login(), 0);
+
+ EXPECT_FALSE(profile.has_perf_data());
+ ASSERT_TRUE(profile.has_perf_stat());
+ EXPECT_EQ(perf_stat_raw_, SerializeMessageToVector(profile.perf_stat()));
+}
+
+TEST_F(PerfProviderTest, BothPerfDataProtoAndPerfStatProto) {
+ SampledProfile sampled_profile;
+ sampled_profile.set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
+
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ sampled_profile,
+ kPerfSuccess,
+ perf_data_raw_,
+ perf_stat_raw_);
+ ASSERT_TRUE(perf_provider_->stored_profiles().empty());
+}
+
+TEST_F(PerfProviderTest, InvalidPerfOutputResult) {
+ SampledProfile sampled_profile;
+ sampled_profile.set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
+
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ sampled_profile,
+ kPerfFailure,
+ perf_data_raw_,
+ std::vector<uint8_t>());
+
+ // Should not have been stored.
+ EXPECT_TRUE(perf_provider_->stored_profiles().empty());
+}
+
+// Change |sampled_profile| between calls to ParseOutputProtoIfValid().
+TEST_F(PerfProviderTest, MultipleCalls) {
+ scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
+ sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
+
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ *sampled_profile,
+ kPerfSuccess,
+ perf_data_raw_,
+ std::vector<uint8_t>());
+ EXPECT_EQ(1U, perf_provider_->stored_profiles().size());
+
+ sampled_profile.reset(new SampledProfile);
+ sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION);
+ sampled_profile->set_ms_after_login(23456);
+ sampled_profile->set_ms_after_restore(3000);
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ *sampled_profile,
+ kPerfSuccess,
+ std::vector<uint8_t>(),
+ perf_stat_raw_);
+ EXPECT_EQ(2U, perf_provider_->stored_profiles().size());
+
+ sampled_profile.reset(new SampledProfile);
+ sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
+ sampled_profile->set_ms_after_login(34567);
+ sampled_profile->set_suspend_duration_ms(60000);
+ sampled_profile->set_ms_after_resume(1500);
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ *sampled_profile,
+ kPerfSuccess,
+ perf_data_raw_,
+ std::vector<uint8_t>());
+ EXPECT_EQ(3U, perf_provider_->stored_profiles().size());
+
+ sampled_profile.reset(new SampledProfile);
+ sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ *sampled_profile,
+ kPerfSuccess,
+ std::vector<uint8_t>(),
+ perf_stat_raw_);
+ ASSERT_EQ(4U, perf_provider_->stored_profiles().size());
+
+ const SampledProfile& profile1 = perf_provider_->stored_profiles()[0];
+ EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
+ EXPECT_GT(profile1.ms_after_login(), 0);
+ ASSERT_TRUE(profile1.has_perf_data());
+ EXPECT_FALSE(profile1.has_perf_stat());
+ EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile1.perf_data()));
+
+ const SampledProfile& profile2 = perf_provider_->stored_profiles()[1];
+ EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
+ EXPECT_GT(profile2.ms_after_login(), 0);
+ EXPECT_EQ(3000, profile2.ms_after_restore());
+ EXPECT_FALSE(profile2.has_perf_data());
+ ASSERT_TRUE(profile2.has_perf_stat());
+ EXPECT_EQ(perf_stat_raw_, SerializeMessageToVector(profile2.perf_stat()));
+
+ const SampledProfile& profile3 = perf_provider_->stored_profiles()[2];
+ EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
+ EXPECT_GT(profile3.ms_after_login(), 0);
+ EXPECT_EQ(60000, profile3.suspend_duration_ms());
+ EXPECT_EQ(1500, profile3.ms_after_resume());
+ ASSERT_TRUE(profile3.has_perf_data());
+ EXPECT_FALSE(profile3.has_perf_stat());
+ EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile3.perf_data()));
+
+ const SampledProfile& profile4 = perf_provider_->stored_profiles()[3];
+ EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile4.trigger_event());
+ EXPECT_GT(profile4.ms_after_login(), 0);
+ EXPECT_FALSE(profile4.has_perf_data());
+ ASSERT_TRUE(profile4.has_perf_stat());
+ EXPECT_EQ(perf_stat_raw_, SerializeMessageToVector(profile4.perf_stat()));
+}
+
+// Simulate opening and closing of incognito window in between calls to
+// ParseOutputProtoIfValid().
+TEST_F(PerfProviderTest, IncognitoWindowOpened) {
+ scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
+ sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
+
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ *sampled_profile,
+ kPerfSuccess,
+ perf_data_raw_,
+ std::vector<uint8_t>());
+ EXPECT_EQ(1U, perf_provider_->stored_profiles().size());
+
+ sampled_profile.reset(new SampledProfile);
+ sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION);
+ sampled_profile->set_ms_after_login(23456);
+ sampled_profile->set_ms_after_restore(3000);
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ *sampled_profile,
+ kPerfSuccess,
+ std::vector<uint8_t>(),
+ perf_stat_raw_);
+ EXPECT_EQ(2U, perf_provider_->stored_profiles().size());
+
+ // An incognito window opens.
+ incognito_observer_->set_incognito_launched(true);
+
+ sampled_profile.reset(new SampledProfile);
+ sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ *sampled_profile,
+ kPerfSuccess,
+ perf_data_raw_,
+ std::vector<uint8_t>());
+ EXPECT_EQ(2U, perf_provider_->stored_profiles().size());
+
+ sampled_profile.reset(new SampledProfile);
+ sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ *sampled_profile,
+ kPerfSuccess,
+ std::vector<uint8_t>(),
+ perf_stat_raw_);
+ ASSERT_EQ(2U, perf_provider_->stored_profiles().size());
+
+ // Incognito window closes, should be good to go again.
+ incognito_observer_->set_incognito_launched(false);
+
+ sampled_profile.reset(new SampledProfile);
+ sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
+ sampled_profile->set_ms_after_login(34567);
+ sampled_profile->set_suspend_duration_ms(60000);
+ sampled_profile->set_ms_after_resume(1500);
+ perf_provider_->ParseOutputProtoIfValidForTesting(
+ incognito_observer_,
+ *sampled_profile,
+ kPerfSuccess,
+ perf_data_raw_,
+ std::vector<uint8_t>());
+ EXPECT_EQ(3U, perf_provider_->stored_profiles().size());
+
+ const SampledProfile& profile1 = perf_provider_->stored_profiles()[0];
+ EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
+ EXPECT_GT(profile1.ms_after_login(), 0);
+ ASSERT_TRUE(profile1.has_perf_data());
+ EXPECT_FALSE(profile1.has_perf_stat());
+ EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile1.perf_data()));
+
+ const SampledProfile& profile2 = perf_provider_->stored_profiles()[1];
+ EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
+ EXPECT_GT(profile2.ms_after_login(), 0);
+ EXPECT_EQ(3000, profile2.ms_after_restore());
+ EXPECT_FALSE(profile2.has_perf_data());
+ ASSERT_TRUE(profile2.has_perf_stat());
+ EXPECT_EQ(perf_stat_raw_, SerializeMessageToVector(profile2.perf_stat()));
+
+ const SampledProfile& profile3 = perf_provider_->stored_profiles()[2];
+ EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
+ EXPECT_GT(profile3.ms_after_login(), 0);
+ EXPECT_EQ(60000, profile3.suspend_duration_ms());
+ EXPECT_EQ(1500, profile3.ms_after_resume());
+ ASSERT_TRUE(profile3.has_perf_data());
+ EXPECT_FALSE(profile3.has_perf_stat());
+ EXPECT_EQ(perf_data_raw_, SerializeMessageToVector(profile3.perf_data()));
+}
+
+} // namespace metrics

Powered by Google App Engine
This is Rietveld 408576698