From 73d71c0720b40cb5ab6ce2151c6efd1b198c529b Mon Sep 17 00:00:00 2001 From: Muffin Date: Mon, 13 May 2024 14:30:42 -0500 Subject: [PATCH] Fix benchmark playground - added project token support - option checkbox to enable compiler --- src/playground/benchmark.js | 42 +++++++++++++++++++++++++++++++++---- src/playground/index.html | 21 ++++++++++++------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/playground/benchmark.js b/src/playground/benchmark.js index 846a57b03d2..85da6b49257 100644 --- a/src/playground/benchmark.js +++ b/src/playground/benchmark.js @@ -58,12 +58,20 @@ const PROJECT_SERVER = 'https://cdn.projects.scratch.mit.edu/'; const SLOW = .1; -const projectInput = document.querySelector('input'); +const projectInput = document.querySelector('#project-id'); +if (location.hash) { + projectInput.value = location.hash.substring(1); +} + +const enableCompiler = new URLSearchParams(location.search).get('compiler') === 'true'; +const compilerInput = document.querySelector('#enable-compiler'); +compilerInput.checked = enableCompiler; document.querySelector('.run') .addEventListener('click', () => { - window.location.hash = projectInput.value; - location.reload(); + const params = new URLSearchParams(location.search); + params.set('compiler', compilerInput.checked); + location.href = `${location.pathname}?${params}#${projectInput.value}`; }, false); const setShareLink = function (json) { @@ -73,12 +81,35 @@ const setShareLink = function (json) { .href = `suite.html`; }; +const getProjectMetadata = async projectId => { + const response = await fetch(`https://trampoline.turbowarp.org/api/projects/${projectId}`); + if (response.status === 404) { + throw new Error('The project is unshared or does not exist'); + } + if (!response.ok) { + throw new Error(`HTTP error ${response.status} fetching project metadata`); + } + const json = await response.json(); + return json; +}; + +const getProjectData = async projectId => { + const metadata = await getProjectMetadata(projectId); + const token = metadata.project_token; + const response = await fetch(`https://projects.scratch.mit.edu/${projectId}?token=${token}`); + if (!response.ok) { + throw new Error(`HTTP error ${response.status} fetching project data`); + } + const data = await response.arrayBuffer(); + return data; +}; + const loadProject = function () { let id = location.hash.substring(1).split(',')[0]; if (id.length < 1 || !isFinite(id)) { id = projectInput.value; } - Scratch.vm.downloadProjectId(id); + getProjectData(id).then(data => Scratch.vm.loadProject(data)); return id; }; @@ -588,6 +619,9 @@ const runBenchmark = function () { const vm = new VirtualMachine(); Scratch.vm = vm; + vm.setCompilerOptions({ + enabled: enableCompiler + }); vm.setTurboMode(true); const storage = new ScratchStorage(); diff --git a/src/playground/index.html b/src/playground/index.html index 8459fcdda4b..43b3d423ce4 100644 --- a/src/playground/index.html +++ b/src/playground/index.html @@ -23,15 +23,22 @@

Scratch VM Benchmark

Welcome to the scratch-vm benchmark. This tool helps you profile a scratch project. When you load the page, it: -

    -
  1. loads the default project and enables turbo mode -
  2. runs the project for 4 seconds to warm up -
  3. profiles for 6 seconds -
  4. stops and reports -
+

+
    +
  1. loads the default project and enables turbo mode +
  2. runs the project for 4 seconds to warm up +
  3. profiles for 6 seconds +
  4. stops and reports +
+

+ The benchmark is not very useful when the compiler is enabled.

- + +