Commit b1053f97 authored by Orin Jaworski's avatar Orin Jaworski Committed by Chromium LUCI CQ

[omnibox] Add Pedals memory usage estimation and unit test

Bug: 1157612
Change-Id: Iebd56655a223360f716c0d075c68fa2572fd1508
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2585752Reviewed-by: default avatarAngela Yoeurng <yoangela@chromium.org>
Commit-Queue: Orin Jaworski <orinj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836216}
parent e3817bf3
......@@ -16,7 +16,6 @@ MockAutocompleteProviderClient::MockAutocompleteProviderClient() {
document_suggestions_service_ = std::make_unique<DocumentSuggestionsService>(
/*identity_manager=*/nullptr, GetURLLoaderFactory());
pedal_provider_ = std::make_unique<OmniboxPedalProvider>(*this);
incognito_mode_available_ = true;
}
MockAutocompleteProviderClient::~MockAutocompleteProviderClient() {
......
......@@ -129,14 +129,6 @@ class MockAutocompleteProviderClient
return &test_url_loader_factory_;
}
bool IsIncognitoModeAvailable() const override {
return incognito_mode_available_;
}
void set_incognito_mode_available(bool incognito_mode_available) {
incognito_mode_available_ = incognito_mode_available;
}
private:
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> shared_factory_;
......@@ -145,7 +137,6 @@ class MockAutocompleteProviderClient
std::unique_ptr<DocumentSuggestionsService> document_suggestions_service_;
std::unique_ptr<OmniboxPedalProvider> pedal_provider_;
std::unique_ptr<TemplateURLService> template_url_service_;
bool incognito_mode_available_;
};
#endif // COMPONENTS_OMNIBOX_BROWSER_MOCK_AUTOCOMPLETE_PROVIDER_CLIENT_H_
......@@ -6,6 +6,7 @@
#include <algorithm>
#include <cctype>
#include <numeric>
#include "base/strings/utf_string_conversions.h"
#include "components/omnibox/browser/omnibox_client.h"
......@@ -96,6 +97,15 @@ void OmniboxPedal::SynonymGroup::AddSynonym(OmniboxPedal::Tokens&& synonym) {
synonyms_.push_back(std::move(synonym));
}
size_t OmniboxPedal::SynonymGroup::EstimateMemoryUsage() const {
size_t total = sizeof(*this);
total += std::accumulate(synonyms_.begin(), synonyms_.end(), 0,
[](size_t sum, auto& tokens) {
return sum + tokens.capacity() * sizeof(tokens[0]);
});
return total;
}
// =============================================================================
OmniboxPedal::OmniboxPedal(OmniboxPedalId id, LabelStrings strings, GURL url)
......@@ -140,6 +150,23 @@ void OmniboxPedal::AddSynonymGroup(SynonymGroup&& group) {
synonym_groups_.push_back(std::move(group));
}
size_t OmniboxPedal::EstimateMemoryUsage() const {
size_t total = sizeof(*this);
total += url_.EstimateMemoryUsage();
total += std::accumulate(
synonym_groups_.begin(), synonym_groups_.end(), 0,
[](size_t sum, auto& s) { return sum + s.EstimateMemoryUsage(); });
total += (synonym_groups_.capacity() - synonym_groups_.size()) *
sizeof(synonym_groups_[0]);
total += strings_.hint.size() * sizeof(strings_.hint[0]);
total += strings_.hint_short.size() * sizeof(strings_.hint_short[0]);
total += strings_.suggestion_contents.size() *
sizeof(strings_.suggestion_contents[0]);
total += strings_.accessibility_hint.size() *
sizeof(strings_.accessibility_hint[0]);
return total;
}
bool OmniboxPedal::IsConceptMatch(const Tokens& match_sequence) const {
Tokens remaining(match_sequence);
for (const auto& group : synonym_groups_) {
......
......@@ -79,6 +79,9 @@ class OmniboxPedal {
// Increase acceptable input size range according to this group's content.
void UpdateTokenSequenceSizeRange(size_t* out_min, size_t* out_max) const;
// Estimates RAM usage in bytes for this synonym group.
size_t EstimateMemoryUsage() const;
protected:
// If this is true, a synonym of the group must be present for triggering.
// If false, then presence is simply allowed and does not inhibit triggering
......@@ -156,6 +159,9 @@ class OmniboxPedal {
// Move a synonym group into this Pedal's collection.
void AddSynonymGroup(SynonymGroup&& group);
// Estimates RAM usage in bytes for this Pedal.
size_t EstimateMemoryUsage() const;
OmniboxPedalId id() { return id_; }
protected:
......
......@@ -4,6 +4,8 @@
#include "components/omnibox/browser/omnibox_pedal_provider.h"
#include <numeric>
#include "base/i18n/case_conversion.h"
#include "base/i18n/char_iterator.h"
#include "base/json/json_reader.h"
......@@ -64,6 +66,21 @@ void OmniboxPedalProvider::ResetSession() {
field_trial_triggered_ = false;
}
size_t OmniboxPedalProvider::EstimateMemoryUsage() const {
size_t total = sizeof(*this);
total += std::accumulate(dictionary_.begin(), dictionary_.end(), 0,
[](size_t sum, auto& s) {
return sum + s.first.size() * sizeof(s.first[0]);
});
total += dictionary_.size() * (sizeof(int) + sizeof(base::string16));
total += ignore_group_.EstimateMemoryUsage();
total += std::accumulate(pedals_.begin(), pedals_.end(), 0,
[](size_t sum, auto& s) {
return sum + s.second->EstimateMemoryUsage();
});
return total;
}
OmniboxPedal* OmniboxPedalProvider::FindPedalMatch(
const AutocompleteInput& input,
const base::string16& match_text) {
......
......@@ -47,6 +47,9 @@ class OmniboxPedalProvider {
field_trial_triggered_ = triggered;
}
// Estimates memory usage for this and all contained Pedals.
size_t EstimateMemoryUsage() const;
protected:
FRIEND_TEST_ALL_PREFIXES(OmniboxPedalImplementationsTest,
ProviderFiltersPedalUpdateChrome);
......
......@@ -26,3 +26,16 @@ TEST_F(OmniboxPedalProviderTest, QueriesTriggerPedals) {
EXPECT_NE(provider.FindPedalMatch(input, base::ASCIIToUTF16("clear history")),
nullptr);
}
TEST_F(OmniboxPedalProviderTest, MemoryUsageIsModerate) {
MockAutocompleteProviderClient client;
OmniboxPedalProvider provider(client);
// Note: This allowance is a soft limit that may be tweaked depending on
// how usefulness is weighed against memory cost. The goal of the test is
// just to prove a reasonable bound.
size_t memory_allowance =
static_cast<size_t>(OmniboxPedalId::TOTAL_COUNT) * 2048;
size_t memory_usage = provider.EstimateMemoryUsage();
LOG(INFO) << "Pedals memory usage: " << memory_usage;
EXPECT_LT(memory_usage, memory_allowance);
}
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