Commit 8ec2c4d7 authored by Kouhei Ueno's avatar Kouhei Ueno Committed by Commit Bot

PageLoad UKM: Add WasCached metric

Bug: 909576
Change-Id: Ic202cda4e44410d75bd774edae721a8a2cb6ae10
Reviewed-on: https://chromium-review.googlesource.com/c/1350420Reviewed-by: default avatarBryan McQuade <bmcquade@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Kouhei Ueno <kouhei@chromium.org>
Cr-Commit-Position: refs/heads/master@{#613354}
parent c373d94b
......@@ -72,6 +72,7 @@ UkmPageLoadMetricsObserver::ObservePolicy UkmPageLoadMetricsObserver::OnCommit(
http_response_code_ = response_headers->response_code();
// The PageTransition for the navigation may be updated on commit.
page_transition_ = navigation_handle->GetPageTransition();
was_cached_ = navigation_handle->WasResponseCached();
return CONTINUE_OBSERVING;
}
......@@ -296,6 +297,9 @@ void UkmPageLoadMetricsObserver::RecordPageLoadExtraInfoMetrics(
// info.page_end_reason fits in a uint32_t, so we can safely cast to int64_t.
builder.SetNavigation_PageEndReason(
static_cast<int64_t>(info.page_end_reason));
if (info.did_commit && was_cached_) {
builder.SetWasCached(1);
}
builder.Record(ukm::UkmRecorder::Get());
}
......
......@@ -105,6 +105,9 @@ class UkmPageLoadMetricsObserver
// True if the page started hidden, or ever became hidden.
bool was_hidden_ = false;
// True if the page main resource was served from disk cache.
bool was_cached_ = false;
DISALLOW_COPY_AND_ASSIGN(UkmPageLoadMetricsObserver);
};
......
......@@ -88,6 +88,33 @@ using TimingField = page_load_metrics::PageLoadMetricsTestWaiter::TimingField;
using WebFeature = blink::mojom::WebFeature;
using testing::UnorderedElementsAre;
namespace {
constexpr char kCacheablePathPrefix[] = "/cacheable";
std::unique_ptr<net::test_server::HttpResponse> HandleCachableRequestHandler(
const net::test_server::HttpRequest& request) {
if (!base::StartsWith(request.relative_url, kCacheablePathPrefix,
base::CompareCase::SENSITIVE)) {
return nullptr;
}
if (request.headers.find("If-None-Match") != request.headers.end()) {
return std::make_unique<net::test_server::RawHttpResponse>(
"HTTP/1.1 304 Not Modified", "");
}
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_code(net::HTTP_OK);
response->set_content_type("text/html");
response->AddCustomHeader("cache-control", "max-age=60");
response->AddCustomHeader("etag", "foobar");
response->set_content("hi");
return std::move(response);
}
} // namespace
class PageLoadMetricsBrowserTest : public InProcessBrowserTest {
public:
PageLoadMetricsBrowserTest() {
......@@ -100,6 +127,8 @@ class PageLoadMetricsBrowserTest : public InProcessBrowserTest {
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
embedded_test_server()->RegisterRequestHandler(
base::BindRepeating(&HandleCachableRequestHandler));
}
void PreRunTestOnMainThread() override {
......@@ -228,6 +257,48 @@ IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest, NewPage) {
EXPECT_FALSE(NoPageLoadMetricsRecorded());
}
IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest, CachedPage) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url = embedded_test_server()->GetURL(kCacheablePathPrefix);
// Navigate to the |url| to cache the main resource.
ui_test_utils::NavigateToURL(browser(), url);
NavigateToUntrackedUrl();
using PageLoad = ukm::builders::PageLoad;
auto entries =
test_ukm_recorder_->GetMergedEntriesByName(PageLoad::kEntryName);
EXPECT_EQ(1u, entries.size());
for (const auto& kv : entries) {
auto* const uncached_load_entry = kv.second.get();
test_ukm_recorder_->ExpectEntrySourceHasUrl(uncached_load_entry, url);
EXPECT_FALSE(test_ukm_recorder_->EntryHasMetric(uncached_load_entry,
PageLoad::kWasCachedName));
}
// Reset the UKM recorder so it would only contain the cached pageload.
test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
// Second navigation to the |url| should hit cache.
ui_test_utils::NavigateToURL(browser(), url);
// Force navigation to another page, which should force logging of histograms
// persisted at the end of the page load lifetime.
NavigateToUntrackedUrl();
entries = test_ukm_recorder_->GetMergedEntriesByName(PageLoad::kEntryName);
EXPECT_EQ(1u, entries.size());
for (const auto& kv : entries) {
auto* const cached_load_entry = kv.second.get();
test_ukm_recorder_->ExpectEntrySourceHasUrl(cached_load_entry, url);
EXPECT_TRUE(test_ukm_recorder_->EntryHasMetric(cached_load_entry,
PageLoad::kWasCachedName));
}
}
IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest, NewPageInNewForegroundTab) {
ASSERT_TRUE(embedded_test_server()->Start());
......
......@@ -3450,6 +3450,11 @@ be describing additional metrics about the same event.
start to the time the parser started, for main frame documents.
</summary>
</metric>
<metric name="WasCached">
<summary>
Set to 1 if the main resource was served from cache.
</summary>
</metric>
</event>
<event name="PageLoadCapping" singular="True">
......
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