Adds a method to the Performance Monitor Database to fetch the most recent metric.

BUG=130212
TEST=database_unittest gets the most recent metric


Review URL: https://chromiumcodereview.appspot.com/10843010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149668 0039d316-1c4b-4281-b951-d872f2087c98
parent f6250339
......@@ -11,6 +11,7 @@
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/stl_util.h"
#include "base/string_number_conversions.h"
#include "base/string_split.h"
#include "base/stringprintf.h"
......@@ -379,6 +380,23 @@ std::vector<std::string> Database::GetActiveActivities(
return results;
}
bool Database::GetRecentStatsForActivityAndMetric(
const std::string& activity,
MetricType metric,
MetricInfo* info) {
CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
std::string recent_map_key = CreateRecentMapKey(metric, activity);
if (!ContainsKey(recent_map_, recent_map_key))
return false;
std::string recent_key = recent_map_[recent_map_key];
std::string result;
leveldb::Status status = recent_db_->Get(read_options_, recent_key, &result);
if (status.ok())
*info = MetricInfo(SplitRecentKey(recent_key).time, result);
return status.ok();
}
Database::MetricInfoVector Database::GetStatsForActivityAndMetric(
const std::string& activity, MetricType metric_type,
const base::Time& start, const base::Time& end) {
......
......@@ -158,6 +158,18 @@ class Database {
std::vector<std::string> GetActiveActivities(MetricType metric_type,
const base::Time& start);
// Populate info with the most recent activity. Return false if populate
// was unsuccessful.
bool GetRecentStatsForActivityAndMetric(const std::string& activity,
MetricType metric,
MetricInfo* info);
bool GetRecentStatsForActivityAndMetric(MetricType metric, MetricInfo* info) {
return GetRecentStatsForActivityAndMetric(kProcessChromeAggregate,
metric,
info);
}
// Query given |metric_type| and |activity|.
MetricInfoVector GetStatsForActivityAndMetric(const std::string& activity,
MetricType metric_type,
......
......@@ -285,6 +285,23 @@ TEST_F(PerformanceMonitorDatabaseMetricTest, GetActiveMetrics) {
EXPECT_EQ(expected_metrics, active_metrics);
}
TEST_F(PerformanceMonitorDatabaseMetricTest, GetRecentMetric) {
MetricInfo stat;
ASSERT_TRUE(db_->GetRecentStatsForActivityAndMetric(activity_,
METRIC_PRIVATE_MEMORY_USAGE, &stat));
EXPECT_EQ(3000000, stat.value);
ASSERT_TRUE(db_->GetRecentStatsForActivityAndMetric(METRIC_CPU_USAGE, &stat));
EXPECT_EQ(50.5, stat.value);
ScopedTempDir dir;
CHECK(dir.CreateUniqueTempDir());
scoped_ptr<Database> db = Database::Create(dir.path());
CHECK(db.get());
db->set_clock(scoped_ptr<Database::Clock>(new TestingClock()));
EXPECT_FALSE(db->GetRecentStatsForActivityAndMetric(METRIC_CPU_USAGE, &stat));
}
TEST_F(PerformanceMonitorDatabaseMetricTest, GetState) {
std::string key("version");
std::string value("1.0.0.0.1");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment