Commit a0901c31 authored by Takashi Toyoshima's avatar Takashi Toyoshima Committed by Commit Bot

ResourceLoadScheduler: Have an unit test for console logging

This is a follow-up for my last patch to modify how the class generate
console logs, refs/heads/master@{#637638}.

Bug: 936937
Change-Id: I74db0198180a222ab39ed347f1b9b3a5285540c7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1504659
Commit-Queue: Takashi Toyoshima <toyoshim@chromium.org>
Auto-Submit: Takashi Toyoshima <toyoshim@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638081}
parent d451333c
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "third_party/blink/renderer/platform/runtime_enabled_features.h" #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/public/aggregated_metric_reporter.h" #include "third_party/blink/renderer/platform/scheduler/public/aggregated_metric_reporter.h"
#include "third_party/blink/renderer/platform/scheduler/public/frame_status.h" #include "third_party/blink/renderer/platform/scheduler/public/frame_status.h"
#include "third_party/blink/renderer/platform/wtf/time.h"
namespace blink { namespace blink {
...@@ -421,7 +422,7 @@ void ResourceLoadScheduler::Request(ResourceLoadSchedulerClient* client, ...@@ -421,7 +422,7 @@ void ResourceLoadScheduler::Request(ResourceLoadSchedulerClient* client,
DCHECK(ThrottleOption::kStoppable == option || DCHECK(ThrottleOption::kStoppable == option ||
ThrottleOption::kThrottleable == option); ThrottleOption::kThrottleable == option);
if (pending_requests_[option].empty()) if (pending_requests_[option].empty())
pending_queue_update_times_[option] = base::TimeTicks::Now(); pending_queue_update_times_[option] = CurrentTime();
pending_requests_[option].insert(request_info); pending_requests_[option].insert(request_info);
pending_request_map_.insert( pending_request_map_.insert(
*id, MakeGarbageCollected<ClientInfo>(client, option, priority, *id, MakeGarbageCollected<ClientInfo>(client, option, priority,
...@@ -656,14 +657,12 @@ bool ResourceLoadScheduler::GetNextPendingRequest(ClientId* id) { ...@@ -656,14 +657,12 @@ bool ResourceLoadScheduler::GetNextPendingRequest(ClientId* id) {
if (use_stoppable) { if (use_stoppable) {
*id = stoppable_it->client_id; *id = stoppable_it->client_id;
stoppable_queue.erase(stoppable_it); stoppable_queue.erase(stoppable_it);
pending_queue_update_times_[ThrottleOption::kStoppable] = pending_queue_update_times_[ThrottleOption::kStoppable] = CurrentTime();
base::TimeTicks::Now();
return true; return true;
} }
*id = throttleable_it->client_id; *id = throttleable_it->client_id;
throttleable_queue.erase(throttleable_it); throttleable_queue.erase(throttleable_it);
pending_queue_update_times_[ThrottleOption::kThrottleable] = pending_queue_update_times_[ThrottleOption::kThrottleable] = CurrentTime();
base::TimeTicks::Now();
return true; return true;
} }
...@@ -727,8 +726,7 @@ void ResourceLoadScheduler::ShowConsoleMessageIfNeeded() { ...@@ -727,8 +726,7 @@ void ResourceLoadScheduler::ShowConsoleMessageIfNeeded() {
if (is_console_info_shown_ || pending_request_map_.IsEmpty()) if (is_console_info_shown_ || pending_request_map_.IsEmpty())
return; return;
const base::TimeTicks limit = const double limit = CurrentTime() - 60; // In seconds
base::TimeTicks::Now() - base::TimeDelta::FromMinutes(1);
ThrottleOption target_option; ThrottleOption target_option;
if (pending_queue_update_times_[ThrottleOption::kThrottleable] < limit && if (pending_queue_update_times_[ThrottleOption::kThrottleable] < limit &&
!IsPendingRequestEffectivelyEmpty(ThrottleOption::kThrottleable)) { !IsPendingRequestEffectivelyEmpty(ThrottleOption::kThrottleable)) {
......
...@@ -331,8 +331,9 @@ class PLATFORM_EXPORT ResourceLoadScheduler final ...@@ -331,8 +331,9 @@ class PLATFORM_EXPORT ResourceLoadScheduler final
std::set<ClientIdWithPriority, ClientIdWithPriority::Compare>> std::set<ClientIdWithPriority, ClientIdWithPriority::Compare>>
pending_requests_; pending_requests_;
// Remembers times when the top request in each queue is processed. // Remembers elapsed times in seconds when the top request in each queue is
std::map<ThrottleOption, base::TimeTicks> pending_queue_update_times_; // processed.
std::map<ThrottleOption, double> pending_queue_update_times_;
// Holds an internal class instance to monitor and report traffic. // Holds an internal class instance to monitor and report traffic.
std::unique_ptr<TrafficMonitor> traffic_monitor_; std::unique_ptr<TrafficMonitor> traffic_monitor_;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "third_party/blink/renderer/platform/runtime_enabled_features.h" #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/test/fake_frame_scheduler.h" #include "third_party/blink/renderer/platform/scheduler/test/fake_frame_scheduler.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h" #include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/blink/renderer/platform/testing/wtf/scoped_mock_clock.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator.h"
namespace blink { namespace blink {
...@@ -44,14 +45,17 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>, ...@@ -44,14 +45,17 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>,
void SetDelegate(MockClientDelegate* delegate) { delegate_ = delegate; } void SetDelegate(MockClientDelegate* delegate) { delegate_ = delegate; }
void Run() override { void Run() override {
if (delegate_) { if (delegate_)
delegate_->NotifyRun(this); delegate_->NotifyRun(this);
}
EXPECT_FALSE(was_run_); EXPECT_FALSE(was_run_);
was_run_ = true; was_run_ = true;
} }
ConsoleLogger* GetConsoleLogger() override { return console_logger_; }
bool WasRun() { return was_run_; } bool WasRun() { return was_run_; }
ConsoleLogger* GetConsoleLogger() override {
was_console_logger_obtained_ = true;
return console_logger_;
}
bool WasConsoleLoggerObtained() { return was_console_logger_obtained_; }
void Trace(blink::Visitor* visitor) override { void Trace(blink::Visitor* visitor) override {
ResourceLoadSchedulerClient::Trace(visitor); ResourceLoadSchedulerClient::Trace(visitor);
...@@ -63,6 +67,7 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>, ...@@ -63,6 +67,7 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>,
MakeGarbageCollected<NullConsoleLogger>(); MakeGarbageCollected<NullConsoleLogger>();
MockClientDelegate* delegate_; MockClientDelegate* delegate_;
bool was_run_ = false; bool was_run_ = false;
bool was_console_logger_obtained_ = false;
}; };
class ResourceLoadSchedulerTest : public testing::Test { class ResourceLoadSchedulerTest : public testing::Test {
...@@ -619,5 +624,51 @@ TEST_F(ResourceLoadSchedulerTest, LoosenThrottlingPolicy) { ...@@ -619,5 +624,51 @@ TEST_F(ResourceLoadSchedulerTest, LoosenThrottlingPolicy) {
EXPECT_TRUE(Release(id1)); EXPECT_TRUE(Release(id1));
} }
TEST_F(ResourceLoadSchedulerTest, ConsoleMessage) {
WTF::ScopedMockClock mock_clock;
Scheduler()->SetOutstandingLimitForTesting(0, 0);
Scheduler()->OnLifecycleStateChanged(
scheduler::SchedulingLifecycleState::kThrottled);
// Push two requests into the queue.
MockClient* client1 = MakeGarbageCollected<MockClient>();
ResourceLoadScheduler::ClientId id1 = ResourceLoadScheduler::kInvalidClientId;
Scheduler()->Request(client1, ThrottleOption::kThrottleable,
ResourceLoadPriority::kLowest, 0 /* intra_priority */,
&id1);
EXPECT_NE(ResourceLoadScheduler::kInvalidClientId, id1);
EXPECT_FALSE(client1->WasRun());
MockClient* client2 = MakeGarbageCollected<MockClient>();
ResourceLoadScheduler::ClientId id2 = ResourceLoadScheduler::kInvalidClientId;
Scheduler()->Request(client2, ThrottleOption::kThrottleable,
ResourceLoadPriority::kLowest, 0 /* intra_priority */,
&id2);
EXPECT_NE(ResourceLoadScheduler::kInvalidClientId, id2);
EXPECT_FALSE(client2->WasRun());
// Cancel the first request
EXPECT_TRUE(Release(id1));
// Advance current time a little and triggers an life cycle event, but it
// still won't awake the warning logic.
mock_clock.Advance(WTF::TimeDelta::FromSeconds(50));
Scheduler()->OnLifecycleStateChanged(
scheduler::SchedulingLifecycleState::kNotThrottled);
EXPECT_FALSE(client1->WasConsoleLoggerObtained());
EXPECT_FALSE(client2->WasConsoleLoggerObtained());
Scheduler()->OnLifecycleStateChanged(
scheduler::SchedulingLifecycleState::kThrottled);
// Modify current time to awake the console warning logic, and the second
// client should be used for console logging.
mock_clock.Advance(WTF::TimeDelta::FromSeconds(15));
Scheduler()->OnLifecycleStateChanged(
scheduler::SchedulingLifecycleState::kNotThrottled);
EXPECT_FALSE(client1->WasConsoleLoggerObtained());
EXPECT_TRUE(client2->WasConsoleLoggerObtained());
EXPECT_TRUE(Release(id2));
}
} // namespace } // namespace
} // namespace blink } // namespace blink
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