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

Unified Diff: components/metrics/export/histogram_sample.cc

Issue 227873002: Create a histogram serialization mechanism in components/metrics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Running tests only for chromeos. Created 6 years, 8 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: components/metrics/export/histogram_sample.cc
diff --git a/components/metrics/export/histogram_sample.cc b/components/metrics/export/histogram_sample.cc
new file mode 100644
index 0000000000000000000000000000000000000000..72bc4c304659b88511f405c3f70fbebf497f299f
--- /dev/null
+++ b/components/metrics/export/histogram_sample.cc
@@ -0,0 +1,64 @@
+// Copyright 2014 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 "components/metrics/export/histogram_sample.h"
+
+#include <string>
+#include <vector>
+
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_split.h"
+#include "base/strings/stringprintf.h"
+#include "components/metrics/export/metric_sample.h"
+
+using base::SplitString;
+using base::StringPrintf;
+using base::StringToInt;
+using std::string;
+using std::vector;
+
+namespace metrics {
+
+HistogramSample::HistogramSample(const std::string& name,
+ int sample,
+ int min,
+ int max,
+ int nbucket)
+ : MetricSample(MetricSample::HISTOGRAM, name),
+ sample_(sample),
+ min_(min),
+ max_(max),
+ nbucket_(nbucket) {}
+
+HistogramSample::~HistogramSample() {}
+
+string HistogramSample::ToString() const {
+ return StringPrintf("histogram%c%s %d %d %d %d%c",
+ '\0',
+ name().c_str(),
+ sample_,
+ min_,
+ max_,
+ nbucket_,
+ '\0');
+}
+
+// static
+// Read a histogram from a string listing |name|, |sample|, |min|, |max|,
+// |nbucket| separated by a space.
+HistogramSample* HistogramSample::ReadHistogram(
+ const std::string& histogram_serialized) {
+ vector<string> parts;
+ SplitString(histogram_serialized, ' ', &parts);
+ if (parts.size() != 5) return NULL;
+ int sample, min, max, nbucket;
+ if (parts[0].length() == 0 || !StringToInt(parts[1], &sample) ||
+ !StringToInt(parts[2], &min) || !StringToInt(parts[3], &max) ||
+ !StringToInt(parts[4], &nbucket))
+ return NULL;
+
+ return new HistogramSample(parts[0], sample, min, max, nbucket);
+}
+
+} // namespace metrics

Powered by Google App Engine
This is Rietveld 408576698