diff options
author | grt@chromium.org <grt@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-14 18:33:53 +0000 |
---|---|---|
committer | grt@chromium.org <grt@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-14 18:33:53 +0000 |
commit | 38076f10f1dcb85a52e5c992909c035716437e4e (patch) | |
tree | b4bd0018bf131d29303b56c45e7ce34a4e5b6b90 /base/metrics | |
parent | 99105db3c5d409aefc4f3d8f534e366d7402de3b (diff) | |
download | chromium_src-38076f10f1dcb85a52e5c992909c035716437e4e.zip chromium_src-38076f10f1dcb85a52e5c992909c035716437e4e.tar.gz chromium_src-38076f10f1dcb85a52e5c992909c035716437e4e.tar.bz2 |
Add a switch to emit browser histograms.
BUG=None
Review URL: https://codereview.chromium.org/61983003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@235191 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/metrics')
-rw-r--r-- | base/metrics/statistics_recorder.cc | 33 | ||||
-rw-r--r-- | base/metrics/statistics_recorder.h | 10 | ||||
-rw-r--r-- | base/metrics/statistics_recorder_unittest.cc | 62 |
3 files changed, 102 insertions, 3 deletions
diff --git a/base/metrics/statistics_recorder.cc b/base/metrics/statistics_recorder.cc index f23c810..1e28fbe 100644 --- a/base/metrics/statistics_recorder.cc +++ b/base/metrics/statistics_recorder.cc @@ -6,11 +6,13 @@ #include "base/at_exit.h" #include "base/debug/leak_annotations.h" +#include "base/json/string_escape.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/metrics/histogram.h" #include "base/strings/stringprintf.h" #include "base/synchronization/lock.h" +#include "base/values.h" using std::list; using std::string; @@ -163,6 +165,37 @@ void StatisticsRecorder::WriteGraph(const std::string& query, } // static +std::string StatisticsRecorder::ToJSON(const std::string& query) { + if (!IsActive()) + return std::string(); + + std::string output("{"); + if (!query.empty()) { + output += "\"query\":"; + JsonDoubleQuote(query, true, &output); + output += ","; + } + + Histograms snapshot; + GetSnapshot(query, &snapshot); + output += "\"histograms\":["; + std::string json; + bool first_histogram = true; + for (Histograms::const_iterator it = snapshot.begin(); it != snapshot.end(); + ++it) { + if (first_histogram) + first_histogram = false; + else + output += ","; + json.clear(); + (*it)->WriteJSON(&json); + output += json; + } + output += "]}"; + return output; +} + +// static void StatisticsRecorder::GetHistograms(Histograms* output) { if (lock_ == NULL) return; diff --git a/base/metrics/statistics_recorder.h b/base/metrics/statistics_recorder.h index 0fdfb76..0716e80 100644 --- a/base/metrics/statistics_recorder.h +++ b/base/metrics/statistics_recorder.h @@ -49,12 +49,16 @@ class BASE_EXPORT StatisticsRecorder { static const BucketRanges* RegisterOrDeleteDuplicateRanges( const BucketRanges* ranges); - // Methods for printing histograms. Only histograms which have query as - // a substring are written to output (an empty string will process all - // registered histograms). + // Methods for appending histogram data to a string. Only histograms which + // have |query| as a substring are written to |output| (an empty string will + // process all registered histograms). static void WriteHTMLGraph(const std::string& query, std::string* output); static void WriteGraph(const std::string& query, std::string* output); + // Returns the histograms with |query| as a substring as JSON text (an empty + // |query| will process all registered histograms). + static std::string ToJSON(const std::string& query); + // Method for extracting histograms which were marked for use by UMA. static void GetHistograms(Histograms* output); diff --git a/base/metrics/statistics_recorder_unittest.cc b/base/metrics/statistics_recorder_unittest.cc index 22504dd..906e642 100644 --- a/base/metrics/statistics_recorder_unittest.cc +++ b/base/metrics/statistics_recorder_unittest.cc @@ -4,9 +4,11 @@ #include <vector> +#include "base/json/json_reader.h" #include "base/memory/scoped_ptr.h" #include "base/metrics/histogram.h" #include "base/metrics/statistics_recorder.h" +#include "base/values.h" #include "testing/gtest/include/gtest/gtest.h" namespace base { @@ -263,4 +265,64 @@ TEST_F(StatisticsRecorderTest, BucketRangesSharing) { EXPECT_EQ(2u, ranges.size()); } +TEST_F(StatisticsRecorderTest, ToJSON) { + HISTOGRAM_COUNTS("TestHistogram1", 30); + HISTOGRAM_COUNTS("TestHistogram1", 40); + HISTOGRAM_COUNTS("TestHistogram2", 30); + HISTOGRAM_COUNTS("TestHistogram2", 40); + + std::string json(StatisticsRecorder::ToJSON(std::string())); + + // Check for valid JSON. + scoped_ptr<Value> root; + root.reset(JSONReader::Read(json)); + ASSERT_TRUE(root.get()); + + DictionaryValue* root_dict = NULL; + ASSERT_TRUE(root->GetAsDictionary(&root_dict)); + + // No query should be set. + ASSERT_FALSE(root_dict->HasKey("query")); + + ListValue* histogram_list = NULL; + ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list)); + ASSERT_EQ(2u, histogram_list->GetSize()); + + // Examine the first histogram. + DictionaryValue* histogram_dict = NULL; + ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict)); + + int sample_count; + ASSERT_TRUE(histogram_dict->GetInteger("count", &sample_count)); + EXPECT_EQ(2, sample_count); + + // Test the query filter. + std::string query("TestHistogram2"); + json = StatisticsRecorder::ToJSON(query); + + root.reset(JSONReader::Read(json)); + ASSERT_TRUE(root.get()); + ASSERT_TRUE(root->GetAsDictionary(&root_dict)); + + std::string query_value; + ASSERT_TRUE(root_dict->GetString("query", &query_value)); + EXPECT_EQ(query, query_value); + + ASSERT_TRUE(root_dict->GetList("histograms", &histogram_list)); + ASSERT_EQ(1u, histogram_list->GetSize()); + + ASSERT_TRUE(histogram_list->GetDictionary(0, &histogram_dict)); + + std::string histogram_name; + ASSERT_TRUE(histogram_dict->GetString("name", &histogram_name)); + EXPECT_EQ("TestHistogram2", histogram_name); + + json.clear(); + UninitializeStatisticsRecorder(); + + // No data should be returned. + json = StatisticsRecorder::ToJSON(query); + EXPECT_TRUE(json.empty()); +} + } // namespace base |