blob: 0b40a1f24944227ee02a4c6c81187f929e612246 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(async function() {
TestRunner.addResult(`Tests V8 code cache for javascript resources\n`);
await TestRunner.loadModule('performance_test_runner');
await TestRunner.showPanel('timeline');
// Clear browser cache to avoid any existing entries for the fetched
// scripts in the cache.
SDK.multitargetNetworkManager.clearBrowserCache();
// There are two scripts:
// [A] http://127.0.0.1:8000/devtools/resources/v8-cache-script.cgi
// [B] http://localhost:8000/devtools/resources/v8-cache-script.cgi
// An iframe that loads [A].
// The script is executed as a parser-inserted script,
// to keep the ScriptResource on the MemoryCache.
// ScriptResources for dynamically-inserted <script>s can be
// garbage-collected and thus removed from MemoryCache after its execution.
const scope = 'resources/same-origin-script.html';
// An iframe that loads [B].
const scopeCrossOrigin = 'resources/cross-origin-script.html';
TestRunner.addResult('--- Trace events related to code caches ------');
await PerformanceTestRunner.startTimeline();
async function stopAndPrintTimeline() {
await PerformanceTestRunner.stopTimeline();
await PerformanceTestRunner.printTimelineRecordsWithDetails(
TimelineModel.TimelineModel.RecordType.CompileScript);
}
async function expectationComment(msg) {
await stopAndPrintTimeline();
TestRunner.addResult(msg);
await PerformanceTestRunner.startTimeline();
}
// Load [A] thrice. With the current V8 heuristics (defined in
// third_party/blink/renderer/bindings/core/v8/v8_code_cache.cc) we produce
// cache on second fetch and consume it in the third fetch. This tests these
// heuristics.
// Note that addIframe() waits for iframe's load event, which waits for the
// <script> loading.
await expectationComment('Load [A] 1st time. Produce timestamp. -->');
await TestRunner.addIframe(scope);
await expectationComment('Load [A] 2nd time. Produce code cache. -->');
await TestRunner.addIframe(scope);
await expectationComment('Load [A] 3rd time. Consume code cache. -->');
await TestRunner.addIframe(scope);
await expectationComment('Load [B]. Should not use the cached code. -->');
await TestRunner.addIframe(scopeCrossOrigin);
await expectationComment('Load [A] again from MemoryCache. ' +
'Should use the cached code. -->');
await TestRunner.addIframe(scope);
await expectationComment('Clear [A] from MemoryCache. -->');
// Blink evicts previous Resource when a new request to the same URL but with
// different resource type is started. We fetch() to the URL of [A], and thus
// evicts the previous ScriptResource of [A].
await TestRunner.evaluateInPageAsync(
`fetch('/devtools/resources/v8-cache-script.cgi')`);
await expectationComment('Load [A] from Disk Cache. -->');
// As we cleared [A] from MemoryCache, this doesn't hit MemoryCache, but still
// hits Disk Cache.
await TestRunner.addIframe(scope);
await stopAndPrintTimeline();
TestRunner.addResult('-----------------------------------------------');
TestRunner.completeTest();
})();