summaryrefslogtreecommitdiffstats
path: root/base/metrics
diff options
context:
space:
mode:
authorbcwhite <bcwhite@chromium.org>2015-11-30 14:31:01 -0800
committerCommit bot <commit-bot@chromium.org>2015-11-30 22:32:03 +0000
commit502b229004bece63acb26aa53888626fddd0fccb (patch)
tree08128174787d4f26c8adf7656106aa98b9574648 /base/metrics
parent8f5297e8a3aa279e278b6f34402830511cddb374 (diff)
downloadchromium_src-502b229004bece63acb26aa53888626fddd0fccb.zip
chromium_src-502b229004bece63acb26aa53888626fddd0fccb.tar.gz
chromium_src-502b229004bece63acb26aa53888626fddd0fccb.tar.bz2
Move HashMetricName to base namespace.
New code that will reside in base will need access to these methods. (cl/1471073007) Depends on https://codereview.chromium.org/1471073010 BUG=546019 Review URL: https://codereview.chromium.org/1476633003 Cr-Commit-Position: refs/heads/master@{#362244}
Diffstat (limited to 'base/metrics')
-rw-r--r--base/metrics/BUILD.gn2
-rw-r--r--base/metrics/metrics_hashes.cc31
-rw-r--r--base/metrics/metrics_hashes.h21
-rw-r--r--base/metrics/metrics_hashes_unittest.cc32
4 files changed, 86 insertions, 0 deletions
diff --git a/base/metrics/BUILD.gn b/base/metrics/BUILD.gn
index 0154cfa..6dd212f 100644
--- a/base/metrics/BUILD.gn
+++ b/base/metrics/BUILD.gn
@@ -20,6 +20,8 @@ source_set("metrics") {
"histogram_samples.h",
"histogram_snapshot_manager.cc",
"histogram_snapshot_manager.h",
+ "metrics_hashes.cc",
+ "metrics_hashes.h",
"sample_map.cc",
"sample_map.h",
"sample_vector.cc",
diff --git a/base/metrics/metrics_hashes.cc b/base/metrics/metrics_hashes.cc
new file mode 100644
index 0000000..b09ea4b
--- /dev/null
+++ b/base/metrics/metrics_hashes.cc
@@ -0,0 +1,31 @@
+// 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 "base/metrics/metrics_hashes.h"
+
+#include "base/logging.h"
+#include "base/md5.h"
+#include "base/sys_byteorder.h"
+
+namespace base {
+
+namespace {
+
+// Converts the 8-byte prefix of an MD5 hash into a uint64 value.
+inline uint64_t DigestToUInt64(const base::MD5Digest& digest) {
+ uint64_t value;
+ DCHECK_GE(sizeof(digest.a), sizeof(value));
+ memcpy(&value, digest.a, sizeof(value));
+ return base::NetToHost64(value);
+}
+
+} // namespace
+
+uint64_t HashMetricName(const std::string& name) {
+ base::MD5Digest digest;
+ base::MD5Sum(name.c_str(), name.size(), &digest);
+ return DigestToUInt64(digest);
+}
+
+} // namespace metrics
diff --git a/base/metrics/metrics_hashes.h b/base/metrics/metrics_hashes.h
new file mode 100644
index 0000000..ff1c800
--- /dev/null
+++ b/base/metrics/metrics_hashes.h
@@ -0,0 +1,21 @@
+// 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.
+
+#ifndef BASE_METRICS_METRICS_HASHES_H_
+#define BASE_METRICS_METRICS_HASHES_H_
+
+#include <stdint.h>
+#include <string>
+
+#include "base/base_export.h"
+
+namespace base {
+
+// Computes a uint64 hash of a given string based on its MD5 hash. Suitable for
+// metric names.
+BASE_EXPORT uint64_t HashMetricName(const std::string& name);
+
+} // namespace metrics
+
+#endif // BASE_METRICS_METRICS_HASHES_H_
diff --git a/base/metrics/metrics_hashes_unittest.cc b/base/metrics/metrics_hashes_unittest.cc
new file mode 100644
index 0000000..cfe3625
--- /dev/null
+++ b/base/metrics/metrics_hashes_unittest.cc
@@ -0,0 +1,32 @@
+// 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 "base/metrics/metrics_hashes.h"
+
+#include "base/format_macros.h"
+#include "base/macros.h"
+#include "base/strings/stringprintf.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+// Make sure our ID hashes are the same as what we see on the server side.
+TEST(MetricsUtilTest, HashMetricName) {
+ static const struct {
+ std::string input;
+ std::string output;
+ } cases[] = {
+ {"Back", "0x0557fa923dcee4d0"},
+ {"Forward", "0x67d2f6740a8eaebf"},
+ {"NewTab", "0x290eb683f96572f1"},
+ };
+
+ for (size_t i = 0; i < arraysize(cases); ++i) {
+ uint64_t hash = HashMetricName(cases[i].input);
+ std::string hash_hex = base::StringPrintf("0x%016" PRIx64, hash);
+ EXPECT_EQ(cases[i].output, hash_hex);
+ }
+}
+
+} // namespace metrics