Commit 69c21290 authored by John Delaney's avatar John Delaney Committed by Commit Bot

Add browsertest for PLM ResourceDataUpdates with chunked response

Add a browsertest to verify that we are not counting overhead
for responses with "Transfer-Encoding: chunked" towards network body
bytes. Rename PageLoadMetricsTestWaiter methods to use "network bytes"
instead of "resource bytes".

Bug: 894168
Change-Id: Id83af484f210063761dcbf4552406f08cdd5a171
Reviewed-on: https://chromium-review.googlesource.com/c/1351383Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Reviewed-by: default avatarCharlie Harrison <csharrison@chromium.org>
Commit-Queue: John Delaney <johnidel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612322}
parent 95164f37
......@@ -377,7 +377,7 @@ IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverResourceBrowserTest,
vanilla_script_response->WaitForRequest();
vanilla_script_response->Send(kHttpResponseHeader);
vanilla_script_response->Send(std::string(1024, ' '));
waiter->AddMinimumResourceBytesExpectation(4000);
waiter->AddMinimumNetworkBytesExpectation(4000);
waiter->Wait();
// Verify correct numbers of resources are recorded.
......
......@@ -1795,7 +1795,7 @@ IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest,
browser(), embedded_test_server()->GetURL(
"foo.com", "/cross_site_iframe_factory.html?foo"));
waiter->Wait();
int64_t one_frame_page_size = waiter->current_resource_bytes();
int64_t one_frame_page_size = waiter->current_network_bytes();
waiter = CreatePageLoadMetricsTestWaiter();
waiter->AddPageExpectation(TimingField::kLoadEvent);
......@@ -1805,10 +1805,47 @@ IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest,
"a.com", "/cross_site_iframe_factory.html?a(b,c,d(e,f,g))"));
// Verify that 7 iframes are fetched, with some amount of tolerance since
// favicon is fetched only once.
waiter->AddMinimumResourceBytesExpectation(7 * (one_frame_page_size - 100));
waiter->AddMinimumNetworkBytesExpectation(7 * (one_frame_page_size - 100));
waiter->Wait();
}
IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest,
ChunkedResponse_OverheadDoesNotCountForBodyBytes) {
const char kHttpResponseHeader[] =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n";
const int kChunkSize = 5;
const int kNumChunks = 5;
auto main_response =
std::make_unique<net::test_server::ControllableHttpResponse>(
embedded_test_server(), "/mock_page.html",
true /*relative_url_is_prefix*/);
ASSERT_TRUE(embedded_test_server()->Start());
auto waiter = CreatePageLoadMetricsTestWaiter();
browser()->OpenURL(content::OpenURLParams(
embedded_test_server()->GetURL("/mock_page.html"), content::Referrer(),
WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
main_response->WaitForRequest();
main_response->Send(kHttpResponseHeader);
for (int i = 0; i < kNumChunks; i++) {
main_response->Send(std::to_string(kChunkSize));
main_response->Send("\r\n");
main_response->Send(std::string(kChunkSize, '*'));
main_response->Send("\r\n");
}
main_response->Done();
waiter->AddMinimumCompleteResourcesExpectation(1);
waiter->Wait();
// Verify that overheads for each chunk are not reported as body bytes.
EXPECT_EQ(waiter->current_network_body_bytes(), kChunkSize * kNumChunks);
}
IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest, ReceivedCompleteResources) {
const char kHttpResponseHeader[] =
"HTTP/1.1 200 OK\r\n"
......@@ -1841,7 +1878,7 @@ IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest, ReceivedCompleteResources) {
main_html_response->Send(std::string(1000, ' '));
main_html_response->Done();
waiter->AddMinimumCompleteResourcesExpectation(1);
waiter->AddMinimumResourceBytesExpectation(1000);
waiter->AddMinimumNetworkBytesExpectation(1000);
waiter->Wait();
script_response->WaitForRequest();
......@@ -1853,7 +1890,7 @@ IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest, ReceivedCompleteResources) {
script_response->Send(std::string(1000, ' '));
// Data received but resource not complete
waiter->AddMinimumCompleteResourcesExpectation(1);
waiter->AddMinimumResourceBytesExpectation(2000);
waiter->AddMinimumNetworkBytesExpectation(2000);
waiter->Wait();
script_response->Done();
waiter->AddMinimumCompleteResourcesExpectation(2);
......@@ -1865,7 +1902,7 @@ IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest, ReceivedCompleteResources) {
iframe_response->Send(std::string(2000, ' '));
iframe_response->Done();
waiter->AddMinimumCompleteResourcesExpectation(3);
waiter->AddMinimumResourceBytesExpectation(4000);
waiter->AddMinimumNetworkBytesExpectation(4000);
waiter->Wait();
}
......
......@@ -43,9 +43,9 @@ void PageLoadMetricsTestWaiter::AddMinimumCompleteResourcesExpectation(
expected_minimum_complete_resources_ = expected_minimum_complete_resources;
}
void PageLoadMetricsTestWaiter::AddMinimumResourceBytesExpectation(
int expected_minimum_resource_bytes) {
expected_minimum_resource_bytes_ = expected_minimum_resource_bytes;
void PageLoadMetricsTestWaiter::AddMinimumNetworkBytesExpectation(
int expected_minimum_network_bytes) {
expected_minimum_network_bytes_ = expected_minimum_network_bytes;
}
bool PageLoadMetricsTestWaiter::DidObserveInPage(TimingField field) const {
......@@ -117,9 +117,12 @@ void PageLoadMetricsTestWaiter::OnResourceDataUseObserved(
std::forward_as_tuple(resource->request_id),
std::forward_as_tuple(resource->Clone()));
}
if (resource->is_complete)
if (resource->is_complete) {
current_complete_resources_++;
current_resource_bytes_ += resource->delta_bytes;
if (!resource->was_fetched_via_cache)
current_network_body_bytes_ += resource->encoded_body_length;
}
current_network_bytes_ += resource->delta_bytes;
}
if (ExpectationsSatisfied() && run_loop_)
run_loop_->Quit();
......@@ -186,8 +189,8 @@ bool PageLoadMetricsTestWaiter::ResourceUseExpectationsSatisfied() const {
return (expected_minimum_complete_resources_ == 0 ||
current_complete_resources_ >=
expected_minimum_complete_resources_) &&
(expected_minimum_resource_bytes_ == 0 ||
current_resource_bytes_ >= expected_minimum_resource_bytes_);
(expected_minimum_network_bytes_ == 0 ||
current_network_bytes_ >= expected_minimum_network_bytes_);
}
bool PageLoadMetricsTestWaiter::ExpectationsSatisfied() const {
......
......@@ -45,7 +45,7 @@ class PageLoadMetricsTestWaiter
int expected_minimum_complete_resources);
// Add aggregate received resource bytes expectation.
void AddMinimumResourceBytesExpectation(int expected_minimum_resource_bytes);
void AddMinimumNetworkBytesExpectation(int expected_minimum_network_bytes);
// Whether the given TimingField was observed in the page.
bool DidObserveInPage(TimingField field) const;
......@@ -54,7 +54,11 @@ class PageLoadMetricsTestWaiter
// expectation methods. All matching fields must be set to end this wait.
void Wait();
int64_t current_resource_bytes() const { return current_resource_bytes_; }
int64_t current_network_bytes() const { return current_network_bytes_; }
int64_t current_network_body_bytes() const {
return current_network_body_bytes_;
}
protected:
virtual bool ExpectationsSatisfied() const;
......@@ -163,9 +167,12 @@ class PageLoadMetricsTestWaiter
TimingFieldBitSet observed_page_fields_;
int current_complete_resources_ = 0;
int64_t current_resource_bytes_ = 0;
int64_t current_network_bytes_ = 0;
// Network body bytes are only counted for complete resources.
int64_t current_network_body_bytes_ = 0;
int expected_minimum_complete_resources_ = 0;
int expected_minimum_resource_bytes_ = 0;
int expected_minimum_network_bytes_ = 0;
bool attach_on_tracker_creation_ = false;
bool did_add_observer_ = false;
......
......@@ -180,7 +180,7 @@ struct ResourceDataUpdate {
int64 received_data_length = 0;
// The length of the response body for the resource before removing any
// content encodings.
// content encodings. Only set for complete resources.
int64 encoded_body_length = 0;
// Whether this resource load has completed.
......
......@@ -38,8 +38,6 @@ void PageResourceDataUse::DidStartResponse(
data_reduction_proxy::EstimateCompressionRatioFromHeaders(&response_head);
proxy_used_ = !response_head.proxy_server.is_direct();
mime_type_ = response_head.mime_type;
total_received_bytes_ = 0;
last_update_bytes_ = 0;
was_fetched_via_cache_ = response_head.was_fetched_via_cache;
is_secure_scheme_ = response_url.SchemeIsCryptographic();
}
......@@ -82,7 +80,7 @@ void PageResourceDataUse::SetIsMainFrameResource(bool is_main_frame_resource) {
int PageResourceDataUse::CalculateNewlyReceivedBytes() {
int newly_received_bytes = total_received_bytes_ - last_update_bytes_;
last_update_bytes_ = total_received_bytes_;
DCHECK(newly_received_bytes >= 0);
DCHECK_GE(newly_received_bytes, 0);
return newly_received_bytes;
}
......
......@@ -63,8 +63,8 @@ class PageResourceDataUse {
// used.
double data_reduction_proxy_compression_ratio_estimate_;
uint64_t total_received_bytes_;
uint64_t last_update_bytes_;
uint64_t total_received_bytes_ = 0;
uint64_t last_update_bytes_ = 0;
uint64_t encoded_body_length_ = 0;
bool is_complete_;
......
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