0

Add Node Depth UKM metric for AIPageContent.

UKM Collection Review Doc is https://docs.google.com/document/d/1wf99leudueP4WJOnuYFd5RDuPSeg64tKDpvGvGXCiPI/edit?usp=sharing&resourcekey=0-buhzwIk_KKrANyLCXvxcPg

Bug: 404239036
Change-Id: Iedb3d8e2eb923dc2e246843d8170d00654aa4573
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6371458
Reviewed-by: Alexei Svitkine <asvitkine@chromium.org>
Commit-Queue: Gary Klassen <gklassen@chromium.org>
Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1439787}
This commit is contained in:
Gary Klassen 2025-03-28 16:54:08 -07:00 committed by Chromium LUCI CQ
parent e2fb2c5ecb
commit 4924ecbd94
7 changed files with 70 additions and 2 deletions
components/optimization_guide/content/browser
third_party/blink/renderer/modules/content_extraction
tools/metrics/ukm

@ -15,6 +15,8 @@ specific_include_rules = {
"page_content_proto_provider_browsertest\.cc": [
"+content/shell",
"+components/network_session_configurator/common/network_switches.h",
"+components/ukm/test_ukm_recorder.h",
"+services/metrics/public/cpp/ukm_builders.h",
],
"page_content_proto_util_unittest\.cc": [
"+third_party/blink/renderer/platform/graphics",

@ -8,6 +8,7 @@
#include "base/test/test_future.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
@ -18,6 +19,7 @@
#include "content/shell/browser/shell.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "ui/display/display_switches.h"
namespace optimization_guide {
@ -733,15 +735,39 @@ int TreeDepth(const optimization_guide::proto::ContentNode& node) {
IN_PROC_BROWSER_TEST_P(PageContentProtoProviderBrowserTestMultiProcess,
MAYBE_DeepTree) {
// Listen for ukm metrics.
base::test::TestFuture<void> future;
ukm::TestAutoSetUkmRecorder ukm_recorder;
ukm_recorder.SetOnAddEntryCallback(
ukm::builders::OptimizationGuide_AIPageContentAgent::kEntryName,
future.GetRepeatingCallback());
LoadPage(https_server()->GetURL("/deep.html"));
// deep.html has a tree depth of 300. Expect mojo encoding to trim to less
// than mojo's kMaxRecursionDepth of 200.
EXPECT_LT(TreeDepth(page_content().root_node()), 200);
// Ensure a ukm metric was recorded.
auto entries = ukm_recorder.GetEntriesByName(
ukm::builders::OptimizationGuide_AIPageContentAgent::kEntryName);
EXPECT_EQ(1u, entries.size());
auto* entry = entries[0].get();
EXPECT_EQ(
1, *ukm_recorder.GetEntryMetric(
entry, ukm::builders::OptimizationGuide_AIPageContentAgent::
kNodeDepthLimitExceededName));
}
IN_PROC_BROWSER_TEST_P(PageContentProtoProviderBrowserTestMultiProcess,
MAYBE_DeepSparseTree) {
DeepSparseTree) {
// Listen for ukm metrics.
base::test::TestFuture<void> future;
ukm::TestAutoSetUkmRecorder ukm_recorder;
ukm_recorder.SetOnAddEntryCallback(
ukm::builders::OptimizationGuide_AIPageContentAgent::kEntryName,
future.GetRepeatingCallback());
LoadPage(https_server()->GetURL("/deep_sparse.html"));
// deep_sparse.html has a dom tree depth of 300. Every other DIV is one that
@ -749,6 +775,11 @@ IN_PROC_BROWSER_TEST_P(PageContentProtoProviderBrowserTestMultiProcess,
// is working properly, the limit should not be reached and the encoded depth
// should be 152 (one for each unskipped div plus root and attributes).
EXPECT_EQ(TreeDepth(page_content().root_node()), 152);
// Ensure that no ukm metric was recorded.
auto entries = ukm_recorder.GetEntriesByName(
ukm::builders::OptimizationGuide_AIPageContentAgent::kEntryName);
EXPECT_EQ(0u, entries.size());
}
} // namespace

@ -20,5 +20,7 @@ blink_modules_sources("content_extraction") {
"inner_text_builder.h",
]
deps = [ "//services/metrics/public/cpp:ukm_builders" ]
public_deps = [ "//third_party/blink/public/mojom:mojom_modules_blink" ]
}

@ -0,0 +1,5 @@
specific_include_rules = {
"ai_page_content_agent.cc": [
"+services/metrics/public/cpp/ukm_builders.h",
]
}

@ -5,6 +5,7 @@
#include "third_party/blink/renderer/modules/content_extraction/ai_page_content_agent.h"
#include "base/time/time.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/blink/public/mojom/content_extraction/ai_page_content.mojom-blink.h"
#include "third_party/blink/renderer/core/accessibility/ax_object_cache.h"
#include "third_party/blink/renderer/core/css/properties/longhands.h"
@ -683,6 +684,13 @@ mojom::blink::AIPageContentPtr AIPageContentAgent::ContentBuilder::Build(
WalkChildren(*layout_view, *root_node, *document_style);
page_content->root_node = std::move(root_node);
if (stack_depth_exceeded_) {
ukm::builders::OptimizationGuide_AIPageContentAgent(
document.UkmSourceID())
.SetNodeDepthLimitExceeded(true)
.Record(document.UkmRecorder());
}
return page_content;
}
@ -740,7 +748,7 @@ bool AIPageContentAgent::ContentBuilder::WalkChildren(
// used in message creation.
static const int kMaxTreeDepth = kMaxRecursionDepth - 8;
if (stack_depth_ > kMaxTreeDepth) {
// TODO(gklassen): Add a metric for this.
stack_depth_exceeded_ = true;
return false;
}

@ -111,6 +111,9 @@ class MODULES_EXPORT AIPageContentAgent final
// The current depth of the tree being walked.
int stack_depth_ = 0;
// Whether the stack depth has exceeded the max tree depth.
bool stack_depth_exceeded_ = false;
};
void Bind(mojo::PendingReceiver<mojom::blink::AIPageContentAgent> receiver);

@ -16468,6 +16468,23 @@ be describing additional metrics about the same event.
</metric>
</event>
<event name="OptimizationGuide.AIPageContentAgent">
<owner>chrome-intelligence-core@google.com</owner>
<owner>khushalsagar@chromium.org</owner>
<owner>abigailklein@chromium.org</owner>
<owner>gklassen@chromium.org</owner>
<summary>
Metrics related to page content extraction. Only recorded when depth limit
is exceeded.
</summary>
<metric name="NodeDepthLimitExceeded">
<summary>
Indicates that the node depth limit was exceeded while building
AnnotatedPageContent.
</summary>
</metric>
</event>
<event name="OptimizationGuide.AnnotatedPageContent">
<owner>chrome-intelligence-core@google.com</owner>
<owner>rajendrant@chromium.org</owner>