| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/scoped_temp_dir.h" | |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "chrome/browser/performance_monitor/database.h" | 14 #include "chrome/browser/performance_monitor/database.h" |
| 15 #include "chrome/browser/performance_monitor/key_builder.h" | 15 #include "chrome/browser/performance_monitor/key_builder.h" |
| 16 #include "chrome/browser/performance_monitor/metric.h" | 16 #include "chrome/browser/performance_monitor/metric.h" |
| 17 #include "chrome/browser/performance_monitor/performance_monitor_util.h" | 17 #include "chrome/browser/performance_monitor/performance_monitor_util.h" |
| 18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 20 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 21 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 21 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| 22 | 22 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 void PopulateDB() { | 113 void PopulateDB() { |
| 114 InitEvents(); | 114 InitEvents(); |
| 115 db_->AddEvent(*install_event_1_.get()); | 115 db_->AddEvent(*install_event_1_.get()); |
| 116 db_->AddEvent(*install_event_2_.get()); | 116 db_->AddEvent(*install_event_2_.get()); |
| 117 db_->AddEvent(*uninstall_event_1_.get()); | 117 db_->AddEvent(*uninstall_event_1_.get()); |
| 118 db_->AddEvent(*uninstall_event_2_.get()); | 118 db_->AddEvent(*uninstall_event_2_.get()); |
| 119 } | 119 } |
| 120 | 120 |
| 121 scoped_ptr<Database> db_; | 121 scoped_ptr<Database> db_; |
| 122 Database::Clock* clock_; | 122 Database::Clock* clock_; |
| 123 ScopedTempDir temp_dir_; | 123 base::ScopedTempDir temp_dir_; |
| 124 scoped_ptr<Event> install_event_1_; | 124 scoped_ptr<Event> install_event_1_; |
| 125 scoped_ptr<Event> install_event_2_; | 125 scoped_ptr<Event> install_event_2_; |
| 126 scoped_ptr<Event> uninstall_event_1_; | 126 scoped_ptr<Event> uninstall_event_1_; |
| 127 scoped_ptr<Event> uninstall_event_2_; | 127 scoped_ptr<Event> uninstall_event_2_; |
| 128 | 128 |
| 129 private: | 129 private: |
| 130 void InitEvents() { | 130 void InitEvents() { |
| 131 install_event_1_ = util::CreateExtensionEvent( | 131 install_event_1_ = util::CreateExtensionEvent( |
| 132 EVENT_EXTENSION_INSTALL, clock_->GetTime(), "a", "extension 1", | 132 EVENT_EXTENSION_INSTALL, clock_->GetTime(), "a", "extension 1", |
| 133 "http://foo.com", static_cast<int>(Extension::LOAD), "0.1", | 133 "http://foo.com", static_cast<int>(Extension::LOAD), "0.1", |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 clock_->GetTime(), | 173 clock_->GetTime(), |
| 174 1000000.0)); | 174 1000000.0)); |
| 175 db_->AddMetric(activity_, | 175 db_->AddMetric(activity_, |
| 176 Metric(METRIC_PRIVATE_MEMORY_USAGE, | 176 Metric(METRIC_PRIVATE_MEMORY_USAGE, |
| 177 clock_->GetTime(), | 177 clock_->GetTime(), |
| 178 3000000.0)); | 178 3000000.0)); |
| 179 } | 179 } |
| 180 | 180 |
| 181 scoped_ptr<Database> db_; | 181 scoped_ptr<Database> db_; |
| 182 Database::Clock* clock_; | 182 Database::Clock* clock_; |
| 183 ScopedTempDir temp_dir_; | 183 base::ScopedTempDir temp_dir_; |
| 184 std::string activity_; | 184 std::string activity_; |
| 185 }; | 185 }; |
| 186 | 186 |
| 187 ////// PerformanceMonitorDatabaseSetupTests //////////////////////////////////// | 187 ////// PerformanceMonitorDatabaseSetupTests //////////////////////////////////// |
| 188 TEST(PerformanceMonitorDatabaseSetupTest, OpenClose) { | 188 TEST(PerformanceMonitorDatabaseSetupTest, OpenClose) { |
| 189 ScopedTempDir temp_dir; | 189 base::ScopedTempDir temp_dir; |
| 190 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 190 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 191 scoped_ptr<Database> db = Database::Create(temp_dir.path()); | 191 scoped_ptr<Database> db = Database::Create(temp_dir.path()); |
| 192 ASSERT_TRUE(db.get()); | 192 ASSERT_TRUE(db.get()); |
| 193 DatabaseTestHelper helper(db.get()); | 193 DatabaseTestHelper helper(db.get()); |
| 194 ASSERT_TRUE(helper.Close()); | 194 ASSERT_TRUE(helper.Close()); |
| 195 } | 195 } |
| 196 | 196 |
| 197 TEST(PerformanceMonitorDatabaseSetupTest, ActiveInterval) { | 197 TEST(PerformanceMonitorDatabaseSetupTest, ActiveInterval) { |
| 198 ScopedTempDir temp_dir; | 198 base::ScopedTempDir temp_dir; |
| 199 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 199 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 200 | 200 |
| 201 TestingClock* clock_1 = new TestingClock(); | 201 TestingClock* clock_1 = new TestingClock(); |
| 202 base::Time start_time = clock_1->GetTime(); | 202 base::Time start_time = clock_1->GetTime(); |
| 203 | 203 |
| 204 scoped_ptr<Database> db_1 = Database::Create(temp_dir.path()); | 204 scoped_ptr<Database> db_1 = Database::Create(temp_dir.path()); |
| 205 db_1->set_clock(scoped_ptr<Database::Clock>(clock_1)); | 205 db_1->set_clock(scoped_ptr<Database::Clock>(clock_1)); |
| 206 DatabaseTestHelper helper1(db_1.get()); | 206 DatabaseTestHelper helper1(db_1.get()); |
| 207 db_1->AddStateValue("test", "test"); | 207 db_1->AddStateValue("test", "test"); |
| 208 ASSERT_TRUE(db_1.get()); | 208 ASSERT_TRUE(db_1.get()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 228 end_time); | 228 end_time); |
| 229 ASSERT_EQ(active_interval.size(), 2u); | 229 ASSERT_EQ(active_interval.size(), 2u); |
| 230 ASSERT_TRUE(active_interval[0].start > start_time && | 230 ASSERT_TRUE(active_interval[0].start > start_time && |
| 231 active_interval[0].end < mid_time); | 231 active_interval[0].end < mid_time); |
| 232 ASSERT_TRUE(active_interval[1].start > mid_time && | 232 ASSERT_TRUE(active_interval[1].start > mid_time && |
| 233 active_interval[1].end < end_time); | 233 active_interval[1].end < end_time); |
| 234 } | 234 } |
| 235 | 235 |
| 236 TEST(PerformanceMonitorDatabaseSetupTest, | 236 TEST(PerformanceMonitorDatabaseSetupTest, |
| 237 ActiveIntervalRetrievalDuringActiveInterval) { | 237 ActiveIntervalRetrievalDuringActiveInterval) { |
| 238 ScopedTempDir temp_dir; | 238 base::ScopedTempDir temp_dir; |
| 239 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 239 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 240 | 240 |
| 241 TestingClock* clock = new TestingClock(); | 241 TestingClock* clock = new TestingClock(); |
| 242 scoped_ptr<Database> db = Database::Create(temp_dir.path()); | 242 scoped_ptr<Database> db = Database::Create(temp_dir.path()); |
| 243 db->set_clock(scoped_ptr<Database::Clock>(clock)); | 243 db->set_clock(scoped_ptr<Database::Clock>(clock)); |
| 244 db->AddStateValue("test", "test"); | 244 db->AddStateValue("test", "test"); |
| 245 base::Time start_time = clock->GetTime(); | 245 base::Time start_time = clock->GetTime(); |
| 246 db->AddStateValue("test", "test"); | 246 db->AddStateValue("test", "test"); |
| 247 base::Time end_time = clock->GetTime(); | 247 base::Time end_time = clock->GetTime(); |
| 248 db->AddStateValue("test", "test"); | 248 db->AddStateValue("test", "test"); |
| 249 | 249 |
| 250 std::vector<TimeRange> active_interval = db->GetActiveIntervals(start_time, | 250 std::vector<TimeRange> active_interval = db->GetActiveIntervals(start_time, |
| 251 end_time); | 251 end_time); |
| 252 ASSERT_EQ(1u, active_interval.size()); | 252 ASSERT_EQ(1u, active_interval.size()); |
| 253 EXPECT_LT(active_interval[0].start, start_time); | 253 EXPECT_LT(active_interval[0].start, start_time); |
| 254 EXPECT_GT(active_interval[0].end, start_time); | 254 EXPECT_GT(active_interval[0].end, start_time); |
| 255 } | 255 } |
| 256 | 256 |
| 257 TEST(PerformanceMonitorDatabaseSetupTest, | 257 TEST(PerformanceMonitorDatabaseSetupTest, |
| 258 ActiveIntervalRetrievalAfterActiveInterval) { | 258 ActiveIntervalRetrievalAfterActiveInterval) { |
| 259 ScopedTempDir temp_dir; | 259 base::ScopedTempDir temp_dir; |
| 260 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 260 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 261 | 261 |
| 262 TestingClock* clock = new TestingClock(); | 262 TestingClock* clock = new TestingClock(); |
| 263 scoped_ptr<Database> db = Database::Create(temp_dir.path()); | 263 scoped_ptr<Database> db = Database::Create(temp_dir.path()); |
| 264 db->set_clock(scoped_ptr<Database::Clock>(clock)); | 264 db->set_clock(scoped_ptr<Database::Clock>(clock)); |
| 265 db->AddStateValue("test", "test"); | 265 db->AddStateValue("test", "test"); |
| 266 | 266 |
| 267 base::Time start_time = clock->GetTime(); | 267 base::Time start_time = clock->GetTime(); |
| 268 base::Time end_time = clock->GetTime(); | 268 base::Time end_time = clock->GetTime(); |
| 269 std::vector<TimeRange> active_interval = db->GetActiveIntervals(start_time, | 269 std::vector<TimeRange> active_interval = db->GetActiveIntervals(start_time, |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 | 389 |
| 390 TEST_F(PerformanceMonitorDatabaseMetricTest, GetRecentMetric) { | 390 TEST_F(PerformanceMonitorDatabaseMetricTest, GetRecentMetric) { |
| 391 Metric stat; | 391 Metric stat; |
| 392 ASSERT_TRUE(db_->GetRecentStatsForActivityAndMetric(activity_, | 392 ASSERT_TRUE(db_->GetRecentStatsForActivityAndMetric(activity_, |
| 393 METRIC_PRIVATE_MEMORY_USAGE, &stat)); | 393 METRIC_PRIVATE_MEMORY_USAGE, &stat)); |
| 394 EXPECT_EQ(3000000, stat.value); | 394 EXPECT_EQ(3000000, stat.value); |
| 395 | 395 |
| 396 ASSERT_TRUE(db_->GetRecentStatsForActivityAndMetric(METRIC_CPU_USAGE, &stat)); | 396 ASSERT_TRUE(db_->GetRecentStatsForActivityAndMetric(METRIC_CPU_USAGE, &stat)); |
| 397 EXPECT_EQ(50.5, stat.value); | 397 EXPECT_EQ(50.5, stat.value); |
| 398 | 398 |
| 399 ScopedTempDir dir; | 399 base::ScopedTempDir dir; |
| 400 CHECK(dir.CreateUniqueTempDir()); | 400 CHECK(dir.CreateUniqueTempDir()); |
| 401 scoped_ptr<Database> db = Database::Create(dir.path()); | 401 scoped_ptr<Database> db = Database::Create(dir.path()); |
| 402 CHECK(db.get()); | 402 CHECK(db.get()); |
| 403 db->set_clock(scoped_ptr<Database::Clock>(new TestingClock())); | 403 db->set_clock(scoped_ptr<Database::Clock>(new TestingClock())); |
| 404 EXPECT_FALSE(db->GetRecentStatsForActivityAndMetric(METRIC_CPU_USAGE, &stat)); | 404 EXPECT_FALSE(db->GetRecentStatsForActivityAndMetric(METRIC_CPU_USAGE, &stat)); |
| 405 } | 405 } |
| 406 | 406 |
| 407 TEST_F(PerformanceMonitorDatabaseMetricTest, GetState) { | 407 TEST_F(PerformanceMonitorDatabaseMetricTest, GetState) { |
| 408 std::string key("version"); | 408 std::string key("version"); |
| 409 std::string value("1.0.0.0.1"); | 409 std::string value("1.0.0.0.1"); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 db_->AddMetric(kProcessChromeAggregate, | 524 db_->AddMetric(kProcessChromeAggregate, |
| 525 Metric(METRIC_CPU_USAGE, clock_->GetTime(), 21.0)); | 525 Metric(METRIC_CPU_USAGE, clock_->GetTime(), 21.0)); |
| 526 Database::MetricVector stats = | 526 Database::MetricVector stats = |
| 527 *db_->GetStatsForActivityAndMetric(METRIC_CPU_USAGE, start, end); | 527 *db_->GetStatsForActivityAndMetric(METRIC_CPU_USAGE, start, end); |
| 528 ASSERT_EQ(2u, stats.size()); | 528 ASSERT_EQ(2u, stats.size()); |
| 529 ASSERT_EQ(3, stats[0].value); | 529 ASSERT_EQ(3, stats[0].value); |
| 530 ASSERT_EQ(9, stats[1].value); | 530 ASSERT_EQ(9, stats[1].value); |
| 531 } | 531 } |
| 532 | 532 |
| 533 } // namespace performance_monitor | 533 } // namespace performance_monitor |
| OLD | NEW |