Commit 12582d42 authored by Sergio Villar Senin's avatar Sergio Villar Senin Committed by Commit Bot

Migrate DOMTimer & WindowPerformance tests to TestMockTimeTaskRunner

This is a follow up of crrev.com/c/1518518. In this CL both
window_performance_test.cc and dom_timer_test.cc are migrated to a mock
task runner. These two migrations involve changes in some other files
basically the ones that make use of performance.now().

Bug: 919383
Change-Id: I95aeae8442850ce20889bcbc7b7fe6309df21eb7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1590101Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarNicolás Peña Moreno <npm@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Sergio Villar <svillar@igalia.com>
Cr-Commit-Position: refs/heads/master@{#664361}
parent 4ebae3bc
......@@ -16,6 +16,7 @@
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/testing/core_unit_test_helper.h"
#include "third_party/blink/renderer/core/timing/dom_window_performance.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support_with_mock_scheduler.h"
using testing::DoubleNear;
......@@ -49,7 +50,21 @@ class DOMTimerTest : public RenderingTest {
// Advance timer manually as RenderingTest expects the time to be non-zero.
platform_->AdvanceClockSeconds(1.);
RenderingTest::SetUp();
auto* window_performance = DOMWindowPerformance::performance(
*GetDocument().GetFrame()->DomWindow());
auto test_task_runner = platform_->test_task_runner();
auto* mock_clock = test_task_runner->GetMockClock();
auto* mock_tick_clock = test_task_runner->GetMockTickClock();
auto now_ticks = test_task_runner->NowTicks();
unified_clock_ = std::make_unique<Performance::UnifiedClock>(
mock_clock, mock_tick_clock);
window_performance->SetClocksForTesting(unified_clock_.get());
window_performance->ResetTimeOriginForTesting(now_ticks);
GetDocument().GetSettings()->SetScriptEnabled(true);
auto* loader = GetDocument().Loader();
loader->GetTiming().SetNavigationStart(now_ticks);
loader->GetTiming().SetClockForTesting(mock_clock);
loader->GetTiming().SetTickClockForTesting(mock_tick_clock);
}
v8::Local<v8::Value> EvalExpression(const char* expr) {
......@@ -78,6 +93,9 @@ class DOMTimerTest : public RenderingTest {
script, KURL(), SanitizeScriptErrors::kSanitize);
platform_->RunUntilIdle();
}
private:
std::unique_ptr<Performance::UnifiedClock> unified_clock_;
};
const char* const kSetTimeout0ScriptText =
......
......@@ -26,6 +26,8 @@
#include "third_party/blink/renderer/core/loader/document_load_timing.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/default_clock.h"
#include "base/time/default_tick_clock.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/inspector/identifiers_factory.h"
#include "third_party/blink/renderer/core/loader/document_loader.h"
......@@ -38,12 +40,23 @@ DocumentLoadTiming::DocumentLoadTiming(DocumentLoader& document_loader)
: redirect_count_(0),
has_cross_origin_redirect_(false),
has_same_origin_as_previous_document_(false),
clock_(base::DefaultClock::GetInstance()),
tick_clock_(base::DefaultTickClock::GetInstance()),
document_loader_(document_loader) {}
void DocumentLoadTiming::Trace(blink::Visitor* visitor) {
visitor->Trace(document_loader_);
}
void DocumentLoadTiming::SetTickClockForTesting(
const base::TickClock* tick_clock) {
tick_clock_ = tick_clock;
}
void DocumentLoadTiming::SetClockForTesting(const base::Clock* clock) {
clock_ = clock;
}
// TODO(csharrison): Remove the null checking logic in a later patch.
LocalFrame* DocumentLoadTiming::GetFrame() const {
return document_loader_ ? document_loader_->GetFrame() : nullptr;
......@@ -56,9 +69,9 @@ void DocumentLoadTiming::NotifyDocumentTimingChanged() {
void DocumentLoadTiming::EnsureReferenceTimesSet() {
if (reference_wall_time_.is_zero())
reference_wall_time_ = TimeDelta::FromSecondsD(CurrentTime());
reference_wall_time_ = TimeDelta::FromSecondsD(clock_->Now().ToDoubleT());
if (reference_monotonic_time_.is_null())
reference_monotonic_time_ = CurrentTimeTicks();
reference_monotonic_time_ = tick_clock_->NowTicks();
}
TimeDelta DocumentLoadTiming::MonotonicTimeToZeroBasedDocumentTime(
......@@ -175,7 +188,7 @@ void DocumentLoadTiming::MarkUnloadEventEnd(TimeTicks end_time) {
}
void DocumentLoadTiming::MarkFetchStart() {
SetFetchStart(CurrentTimeTicks());
SetFetchStart(tick_clock_->NowTicks());
}
void DocumentLoadTiming::SetFetchStart(TimeTicks fetch_start) {
......@@ -195,7 +208,7 @@ void DocumentLoadTiming::SetResponseEnd(TimeTicks response_end) {
}
void DocumentLoadTiming::MarkLoadEventStart() {
load_event_start_ = CurrentTimeTicks();
load_event_start_ = tick_clock_->NowTicks();
TRACE_EVENT_MARK_WITH_TIMESTAMP1("blink.user_timing", "loadEventStart",
load_event_start_, "frame",
ToTraceValue(GetFrame()));
......@@ -203,7 +216,7 @@ void DocumentLoadTiming::MarkLoadEventStart() {
}
void DocumentLoadTiming::MarkLoadEventEnd() {
load_event_end_ = CurrentTimeTicks();
load_event_end_ = tick_clock_->NowTicks();
TRACE_EVENT_MARK_WITH_TIMESTAMP1("blink.user_timing", "loadEventEnd",
load_event_end_, "frame",
ToTraceValue(GetFrame()));
......@@ -211,7 +224,7 @@ void DocumentLoadTiming::MarkLoadEventEnd() {
}
void DocumentLoadTiming::MarkRedirectEnd() {
redirect_end_ = CurrentTimeTicks();
redirect_end_ = tick_clock_->NowTicks();
TRACE_EVENT_MARK_WITH_TIMESTAMP1("blink.user_timing", "redirectEnd",
redirect_end_, "frame",
ToTraceValue(GetFrame()));
......
......@@ -32,6 +32,11 @@
#include "third_party/blink/renderer/platform/instrumentation/tracing/traced_value.h"
#include "third_party/blink/renderer/platform/wtf/time.h"
namespace base {
class Clock;
class TickClock;
} // namespace base
namespace blink {
class DocumentLoader;
......@@ -95,6 +100,9 @@ class CORE_EXPORT DocumentLoadTiming final {
void Trace(blink::Visitor*);
void SetTickClockForTesting(const base::TickClock* tick_clock);
void SetClockForTesting(const base::Clock* clock);
private:
void MarkRedirectEnd();
void NotifyDocumentTimingChanged();
......@@ -118,6 +126,9 @@ class CORE_EXPORT DocumentLoadTiming final {
bool has_cross_origin_redirect_;
bool has_same_origin_as_previous_document_;
const base::Clock* clock_;
const base::TickClock* tick_clock_;
Member<DocumentLoader> document_loader_;
};
......
......@@ -33,6 +33,8 @@
#include <algorithm>
#include "base/metrics/histogram_macros.h"
#include "base/time/default_clock.h"
#include "base/time/default_tick_clock.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/string_or_performance_measure_options.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_object_builder.h"
......@@ -76,18 +78,6 @@ const SecurityOrigin* GetSecurityOrigin(ExecutionContext* context) {
return nullptr;
}
// When a Performance object is first created, use the current system time
// to calculate what the Unix time would be at the time the monotonic clock time
// was zero, assuming no manual changes to the system clock. This can be
// calculated as current_unix_time - current_monotonic_time.
DOMHighResTimeStamp GetUnixAtZeroMonotonic() {
DEFINE_THREAD_SAFE_STATIC_LOCAL(
DOMHighResTimeStamp, unix_at_zero_monotonic,
{ConvertSecondsToDOMHighResTimeStamp(CurrentTime() -
CurrentTimeTicksInSeconds())});
return unix_at_zero_monotonic;
}
Performance::MeasureParameterType StringToNavigationTimingParameterType(
const String& s) {
// The following names come from performance_user_timing.cc.
......@@ -159,6 +149,13 @@ void LogMeasureEndToUma(Performance::MeasureParameterType type) {
UMA_HISTOGRAM_ENUMERATION("Performance.MeasureParameter.EndMark", type);
}
const Performance::UnifiedClock* DefaultUnifiedClock() {
DEFINE_THREAD_SAFE_STATIC_LOCAL(Performance::UnifiedClock, unified_clock,
(base::DefaultClock::GetInstance(),
base::DefaultTickClock::GetInstance()));
return &unified_clock;
}
} // namespace
using PerformanceObserverVector = HeapVector<Member<PerformanceObserver>>;
......@@ -176,6 +173,7 @@ Performance::Performance(
element_timing_buffer_max_size_(kDefaultElementTimingBufferSize),
user_timing_(nullptr),
time_origin_(time_origin),
unified_clock_(DefaultUnifiedClock()),
observer_filter_options_(PerformanceEntry::kInvalid),
task_runner_(std::move(task_runner)),
deliver_observations_timer_(task_runner_,
......@@ -206,7 +204,7 @@ MemoryInfo* Performance::memory() const {
DOMHighResTimeStamp Performance::timeOrigin() const {
DCHECK(!time_origin_.is_null());
return GetUnixAtZeroMonotonic() +
return unified_clock_->GetUnixAtZeroMonotonic() +
ConvertTimeTicksToDOMHighResTimeStamp(time_origin_);
}
......@@ -1021,7 +1019,7 @@ DOMHighResTimeStamp Performance::MonotonicTimeToDOMHighResTimeStamp(
}
DOMHighResTimeStamp Performance::now() const {
return MonotonicTimeToDOMHighResTimeStamp(CurrentTimeTicks());
return MonotonicTimeToDOMHighResTimeStamp(unified_clock_->NowTicks());
}
ScriptValue Performance::toJSONForBinding(ScriptState* script_state) const {
......@@ -1052,4 +1050,29 @@ void Performance::Trace(blink::Visitor* visitor) {
EventTargetWithInlineData::Trace(visitor);
}
DOMHighResTimeStamp Performance::UnifiedClock::GetUnixAtZeroMonotonic() const {
// When a Performance object is first queried, use the current system time
// to calculate what the Unix time would be at the time the monotonic
// clock time was zero, assuming no manual changes to the system clock.
// This can be calculated as current_unix_time - current_monotonic_time.
if (UNLIKELY(!unix_at_zero_monotonic_)) {
unix_at_zero_monotonic_ = ConvertSecondsToDOMHighResTimeStamp(
clock_->Now().ToDoubleT() -
tick_clock_->NowTicks().since_origin().InSecondsF());
}
return unix_at_zero_monotonic_.value();
}
TimeTicks Performance::UnifiedClock::NowTicks() const {
return tick_clock_->NowTicks();
}
void Performance::SetClocksForTesting(const UnifiedClock* clock) {
unified_clock_ = clock;
}
void Performance::ResetTimeOriginForTesting(base::TimeTicks time_origin) {
time_origin_ = time_origin;
}
} // namespace blink
......@@ -51,6 +51,11 @@
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
namespace base {
class Clock;
class TickClock;
} // namespace base
namespace blink {
class PerformanceMarkOptions;
......@@ -278,6 +283,23 @@ class CORE_EXPORT Performance : public EventTargetWithInlineData {
void Trace(blink::Visitor*) override;
class UnifiedClock {
public:
UnifiedClock(const base::Clock* clock, const base::TickClock* tick_clock)
: clock_(clock), tick_clock_(tick_clock) {}
DOMHighResTimeStamp GetUnixAtZeroMonotonic() const;
TimeTicks NowTicks() const;
private:
const base::Clock* clock_;
const base::TickClock* tick_clock_;
mutable base::Optional<DOMHighResTimeStamp> unix_at_zero_monotonic_;
};
// The caller owns the |clock|.
void SetClocksForTesting(const UnifiedClock* clock);
void ResetTimeOriginForTesting(base::TimeTicks time_origin);
private:
void AddPaintTiming(PerformancePaintTiming::PaintType, TimeTicks start_time);
......@@ -339,6 +361,7 @@ class CORE_EXPORT Performance : public EventTargetWithInlineData {
Member<PerformanceEventTiming> first_input_timing_;
TimeTicks time_origin_;
const UnifiedClock* unified_clock_;
PerformanceEntryTypeMask observer_filter_options_;
HeapLinkedHashSet<Member<PerformanceObserver>> observers_;
......
......@@ -4,6 +4,7 @@
#include "third_party/blink/renderer/core/timing/window_performance.h"
#include "base/test/test_mock_time_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/bindings/core/v8/string_or_double.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
......@@ -17,7 +18,6 @@
#include "third_party/blink/renderer/core/timing/dom_window_performance.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/testing/runtime_enabled_features_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/wtf/scoped_mock_clock.h"
namespace blink {
......@@ -32,6 +32,7 @@ TimeTicks GetTimeOrigin() {
class WindowPerformanceTest : public testing::Test {
protected:
void SetUp() override {
test_task_runner_ = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
ResetPerformance();
// Create another dummy page holder and pretend this is the iframe.
......@@ -90,12 +91,18 @@ class WindowPerformanceTest : public testing::Test {
page_holder_->GetDocument().SetURL(KURL("https://example.com"));
performance_ = MakeGarbageCollected<WindowPerformance>(
page_holder_->GetDocument().domWindow());
unified_clock_ = std::make_unique<Performance::UnifiedClock>(
test_task_runner_->GetMockClock(),
test_task_runner_->GetMockTickClock());
performance_->SetClocksForTesting(unified_clock_.get());
performance_->time_origin_ = GetTimeOrigin();
}
Persistent<WindowPerformance> performance_;
std::unique_ptr<DummyPageHolder> page_holder_;
std::unique_ptr<DummyPageHolder> another_page_holder_;
scoped_refptr<base::TestMockTimeTaskRunner> test_task_runner_;
std::unique_ptr<Performance::UnifiedClock> unified_clock_;
};
TEST_F(WindowPerformanceTest, LongTaskObserverInstrumentation) {
......@@ -191,16 +198,17 @@ TEST(PerformanceLifetimeTest, SurviveContextSwitch) {
// order. (http://crbug.com/767560)
TEST_F(WindowPerformanceTest, EnsureEntryListOrder) {
V8TestingScope scope;
WTF::ScopedMockClock clock;
clock.Advance(GetTimeOrigin() - TimeTicks());
auto initial_offset =
test_task_runner_->NowTicks().since_origin().InSecondsF();
test_task_runner_->FastForwardBy(GetTimeOrigin() - TimeTicks());
DummyExceptionStateForTesting exception_state;
clock.Advance(TimeDelta::FromSeconds(2));
test_task_runner_->FastForwardBy(TimeDelta::FromSeconds(2));
for (int i = 0; i < 8; i++) {
performance_->mark(scope.GetScriptState(), AtomicString::Number(i),
exception_state);
}
clock.Advance(TimeDelta::FromSeconds(2));
test_task_runner_->FastForwardBy(TimeDelta::FromSeconds(2));
for (int i = 8; i < 17; i++) {
performance_->mark(scope.GetScriptState(), AtomicString::Number(i),
exception_state);
......@@ -209,11 +217,11 @@ TEST_F(WindowPerformanceTest, EnsureEntryListOrder) {
EXPECT_EQ(17U, entries.size());
for (int i = 0; i < 8; i++) {
EXPECT_EQ(AtomicString::Number(i), entries[i]->name());
EXPECT_NEAR(2000, entries[i]->startTime(), 0.005);
EXPECT_NEAR(2000, entries[i]->startTime() - initial_offset, 0.005);
}
for (int i = 8; i < 17; i++) {
EXPECT_EQ(AtomicString::Number(i), entries[i]->name());
EXPECT_NEAR(4000, entries[i]->startTime(), 0.005);
EXPECT_NEAR(4000, entries[i]->startTime() - initial_offset, 0.005);
}
}
......
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