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 @@
#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/frame_status.h"
#include "third_party/blink/renderer/platform/wtf/time.h"
namespace blink {
......@@ -421,7 +422,7 @@ void ResourceLoadScheduler::Request(ResourceLoadSchedulerClient* client,
DCHECK(ThrottleOption::kStoppable == option ||
ThrottleOption::kThrottleable == option);
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_request_map_.insert(
*id, MakeGarbageCollected<ClientInfo>(client, option, priority,
......@@ -656,14 +657,12 @@ bool ResourceLoadScheduler::GetNextPendingRequest(ClientId* id) {
if (use_stoppable) {
*id = stoppable_it->client_id;
stoppable_queue.erase(stoppable_it);
pending_queue_update_times_[ThrottleOption::kStoppable] =
base::TimeTicks::Now();
pending_queue_update_times_[ThrottleOption::kStoppable] = CurrentTime();
return true;
}
*id = throttleable_it->client_id;
throttleable_queue.erase(throttleable_it);
pending_queue_update_times_[ThrottleOption::kThrottleable] =
base::TimeTicks::Now();
pending_queue_update_times_[ThrottleOption::kThrottleable] = CurrentTime();
return true;
}
......@@ -727,8 +726,7 @@ void ResourceLoadScheduler::ShowConsoleMessageIfNeeded() {
if (is_console_info_shown_ || pending_request_map_.IsEmpty())
return;
const base::TimeTicks limit =
base::TimeTicks::Now() - base::TimeDelta::FromMinutes(1);
const double limit = CurrentTime() - 60; // In seconds
ThrottleOption target_option;
if (pending_queue_update_times_[ThrottleOption::kThrottleable] < limit &&
!IsPendingRequestEffectivelyEmpty(ThrottleOption::kThrottleable)) {
......
......@@ -331,8 +331,9 @@ class PLATFORM_EXPORT ResourceLoadScheduler final
std::set<ClientIdWithPriority, ClientIdWithPriority::Compare>>
pending_requests_;
// Remembers times when the top request in each queue is processed.
std::map<ThrottleOption, base::TimeTicks> pending_queue_update_times_;
// Remembers elapsed times in seconds when the top request in each queue is
// processed.
std::map<ThrottleOption, double> pending_queue_update_times_;
// Holds an internal class instance to monitor and report traffic.
std::unique_ptr<TrafficMonitor> traffic_monitor_;
......
......@@ -12,6 +12,7 @@
#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/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"
namespace blink {
......@@ -44,14 +45,17 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>,
void SetDelegate(MockClientDelegate* delegate) { delegate_ = delegate; }
void Run() override {
if (delegate_) {
if (delegate_)
delegate_->NotifyRun(this);
}
EXPECT_FALSE(was_run_);
was_run_ = true;
}
ConsoleLogger* GetConsoleLogger() override { return console_logger_; }
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 {
ResourceLoadSchedulerClient::Trace(visitor);
......@@ -63,6 +67,7 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>,
MakeGarbageCollected<NullConsoleLogger>();
MockClientDelegate* delegate_;
bool was_run_ = false;
bool was_console_logger_obtained_ = false;
};
class ResourceLoadSchedulerTest : public testing::Test {
......@@ -619,5 +624,51 @@ TEST_F(ResourceLoadSchedulerTest, LoosenThrottlingPolicy) {
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 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