Skip to content

Commit

Permalink
Fix benchmark playground
Browse files Browse the repository at this point in the history
- added project token support
- option checkbox to enable compiler
  • Loading branch information
GarboMuffin committed May 13, 2024
1 parent 216b5b6 commit 73d71c0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
42 changes: 38 additions & 4 deletions src/playground/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
};

Expand Down Expand Up @@ -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();
Expand Down
21 changes: 14 additions & 7 deletions src/playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ <h2>Scratch VM Benchmark</h2>
<p class="description">
Welcome to the scratch-vm benchmark. This tool helps you profile a scratch
project. When you load the page, it:
<ol class="description">
<li>loads the default project and enables turbo mode
<li>runs the project for 4 seconds to warm up
<li>profiles for 6 seconds
<li>stops and reports
</ol>
</p>
<ol class="description">
<li>loads the default project and enables turbo mode
<li>runs the project for 4 seconds to warm up
<li>profiles for 6 seconds
<li>stops and reports
</ol>
<p class="description">
The benchmark is not very useful when the compiler is enabled.
</p>
<div class="run-form">
<input type="text" value="119615668">
<input id="project-id" type="text" value="119615668">
<label>
<input type="checkbox" id="enable-compiler" autocomplete="off">
Enable compiler
</label>
<button class="run">run</button>
</div>
<p class="run-push">
Expand Down

0 comments on commit 73d71c0

Please sign in to comment.