Commit 1898534a authored by Karan Bhatia's avatar Karan Bhatia Committed by Commit Bot

UrlMatcher: Add perf test for SubstringSetMatcher.

Add a simple perf test for SubstringSetMatcher. I plan to change a bit
of the internal implementation, so this will help to measure any perf
difference as well as catch future regressions.

BUG=974391

Change-Id: I03ad4d2ae531c524256db095285761ea4529b270
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2038070Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738709}
parent 2af9b000
......@@ -652,6 +652,7 @@ if (!is_ios && !is_fuchsia) {
"omnibox/browser/history_quick_provider_performance_unittest.cc",
"subresource_filter/core/common/perftests/indexed_ruleset_perftest.cc",
"test/run_all_perftests.cc",
"url_matcher/substring_set_matcher_perftest.cc",
"visitedlink/test/visitedlink_perftest.cc",
]
......@@ -666,6 +667,7 @@ if (!is_ios && !is_fuchsia) {
"//components/subresource_filter/core/common",
"//components/subresource_filter/tools:tools_lib",
"//components/test:test_support",
"//components/url_matcher",
"//components/visitedlink/browser",
"//testing/perf",
"//url",
......
......@@ -12,6 +12,7 @@
#include "base/containers/queue.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/trace_event/memory_usage_estimator.h"
namespace url_matcher {
......@@ -139,6 +140,11 @@ bool SubstringSetMatcher::IsEmpty() const {
return patterns_.empty() && tree_.size() == 1u;
}
size_t SubstringSetMatcher::EstimateMemoryUsage() const {
return base::trace_event::EstimateMemoryUsage(tree_) +
base::trace_event::EstimateMemoryUsage(patterns_);
}
void SubstringSetMatcher::RebuildAhoCorasickTree(
const SubstringPatternVector& sorted_patterns) {
tree_.clear();
......@@ -266,4 +272,9 @@ void SubstringSetMatcher::AhoCorasickNode::AddMatches(
matches_.insert(matches.begin(), matches.end());
}
size_t SubstringSetMatcher::AhoCorasickNode::EstimateMemoryUsage() const {
return base::trace_event::EstimateMemoryUsage(edges_) + sizeof(failure_) +
base::trace_event::EstimateMemoryUsage(matches_);
}
} // namespace url_matcher
......@@ -50,6 +50,9 @@ class URL_MATCHER_EXPORT SubstringSetMatcher {
// Returns true if this object retains no allocated data. Only for debugging.
bool IsEmpty() const;
// Returns the estimated memory usage in bytes.
size_t EstimateMemoryUsage() const;
private:
// A node of an Aho Corasick Tree. This is implemented according to
// http://www.cs.uku.fi/~kilpelai/BSA05/lectures/slides04.pdf
......@@ -105,6 +108,8 @@ class URL_MATCHER_EXPORT SubstringSetMatcher {
void AddMatches(const Matches& matches);
const Matches& matches() const { return matches_; }
size_t EstimateMemoryUsage() const;
private:
// Outgoing edges of current node.
Edges edges_;
......
// Copyright 2020 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/url_matcher/substring_set_matcher.h"
#include <limits>
#include <string>
#include <vector>
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_result_reporter.h"
namespace url_matcher {
namespace {
// Returns a character, cycling from 0 to the maximum signed char value.
char GetCurrentChar() {
static char current = 0;
if (current == std::numeric_limits<char>::max())
current = 0;
else
current++;
return current;
}
// Returns a string of the given length.
std::string GetString(size_t len) {
std::vector<char> pattern;
pattern.reserve(len);
for (size_t j = 0; j < len; j++)
pattern.push_back(GetCurrentChar());
return std::string(pattern.begin(), pattern.end());
}
std::vector<const StringPattern*> GetVectorOfPointers(
const std::vector<StringPattern>& patterns) {
std::vector<const StringPattern*> pointers;
for (const StringPattern& pattern : patterns)
pointers.push_back(&pattern);
return pointers;
}
// Tests performance of SubstringSetMatcher for hundred thousand keys each of
// 100 characters.
TEST(SubstringSetMatcherPerfTest, HundredThousandKeys) {
std::vector<StringPattern> patterns;
// Create patterns.
const size_t kNumPatterns = 100000;
const size_t kPatternLen = 100;
for (size_t i = 0; i < kNumPatterns; i++)
patterns.emplace_back(GetString(kPatternLen), i);
base::ElapsedTimer init_timer;
SubstringSetMatcher matcher;
matcher.RegisterPatterns(GetVectorOfPointers(patterns));
base::TimeDelta init_time = init_timer.Elapsed();
// Match patterns against a string of 5000 characters.
const size_t kTextLen = 5000;
base::ElapsedTimer match_timer;
std::set<StringPattern::ID> matches;
matcher.Match(GetString(kTextLen), &matches);
base::TimeDelta match_time = match_timer.Elapsed();
const char* kInitializationTime = ".init_time";
const char* kMatchTime = ".match_time";
const char* kMemoryUsage = ".memory_usage";
auto reporter = perf_test::PerfResultReporter("SubstringSetMatcher",
"HundredThousandKeys");
reporter.RegisterImportantMetric(kInitializationTime, "us");
reporter.RegisterImportantMetric(kMatchTime, "us");
reporter.RegisterImportantMetric(kMemoryUsage, "Mb");
reporter.AddResult(kInitializationTime, init_time);
reporter.AddResult(kMatchTime, match_time);
reporter.AddResult(kMemoryUsage,
(matcher.EstimateMemoryUsage() * 1.0 / (1 << 20)));
}
} // namespace
} // namespace url_matcher
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