Chromium Code Reviews| Index: base/metrics/statistics_recorder.cc |
| diff --git a/base/metrics/statistics_recorder.cc b/base/metrics/statistics_recorder.cc |
| index f23c81054cf5cff26915da0ae36ffd34f9e69050..e1de90474b1051be00c429370811ba631c0afbac 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.length()) { |
|
Peter Kasting
2013/11/14 07:04:06
Nit: Use !empty()
grt (UTC plus 2)
2013/11/14 14:44:12
Done.
|
| + output.append("\"query\":"); |
|
Peter Kasting
2013/11/14 07:04:06
Nit: All these append() calls could just be +=
grt (UTC plus 2)
2013/11/14 14:44:12
Done.
|
| + JsonDoubleQuote(query, true, &output); |
| + output.append(","); |
| + } |
| + |
| + Histograms snapshot; |
| + GetSnapshot(query, &snapshot); |
| + output.append("\"histograms\":["); |
| + std::string json; |
| + bool first_time = true; |
|
Peter Kasting
2013/11/14 07:04:06
Nit: |first_histogram|?
grt (UTC plus 2)
2013/11/14 14:44:12
Done.
|
| + for (Histograms::const_iterator it = snapshot.begin(); it != snapshot.end(); |
| + ++it) { |
| + if (first_time) |
| + first_time = false; |
| + else |
| + output.append(","); |
| + json.clear(); |
|
Peter Kasting
2013/11/14 07:04:06
Nit: Instead of trying to micro-optimize with clea
grt (UTC plus 2)
2013/11/14 14:44:12
Leaving this as-is.
Peter Kasting
2013/11/14 21:26:16
OK... is there a big advantage to doing things thi
grt (UTC plus 2)
2013/11/14 21:52:27
I believe that reuse of the underlying buffer will
|
| + (*it)->WriteJSON(&json); |
| + output.append(json); |
| + } |
| + output.append("]}"); |
| + return output; |
| +} |
| + |
| +// static |
| void StatisticsRecorder::GetHistograms(Histograms* output) { |
| if (lock_ == NULL) |
| return; |