2019-01-23 11:25:04 -05:00
|
|
|
// Track loading time with timestamps and if possible the performance api.
|
|
|
|
if (window.performance) {
|
|
|
|
// Mark with the performance API when benchmark.js and its dependecies start
|
|
|
|
// evaluation. This can tell us once measured how long the code spends time
|
|
|
|
// turning into execution code for the first time. Skipping evaluation of
|
|
|
|
// some of the code can help us make it faster.
|
|
|
|
performance.mark('Scratch.EvalStart');
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:42:34 -05:00
|
|
|
const ScratchStorage = require('scratch-storage');
|
|
|
|
const VirtualMachine = require('..');
|
2019-01-23 11:25:04 -05:00
|
|
|
const Runtime = require('../engine/runtime');
|
|
|
|
|
|
|
|
const ScratchRender = require('scratch-render');
|
|
|
|
const AudioEngine = require('scratch-audio');
|
|
|
|
const ScratchSVGRenderer = require('scratch-svg-renderer');
|
2019-01-22 17:42:34 -05:00
|
|
|
|
2017-11-09 18:35:12 -05:00
|
|
|
const Scratch = window.Scratch = window.Scratch || {};
|
|
|
|
|
|
|
|
const ASSET_SERVER = 'https://cdn.assets.scratch.mit.edu/';
|
|
|
|
const PROJECT_SERVER = 'https://cdn.projects.scratch.mit.edu/';
|
|
|
|
|
|
|
|
const SLOW = .1;
|
|
|
|
|
2017-11-16 15:43:51 -05:00
|
|
|
const projectInput = document.querySelector('input');
|
|
|
|
|
|
|
|
document.querySelector('.run')
|
|
|
|
.addEventListener('click', () => {
|
|
|
|
window.location.hash = projectInput.value;
|
|
|
|
location.reload();
|
|
|
|
}, false);
|
|
|
|
|
2018-01-16 18:20:26 -05:00
|
|
|
const setShareLink = function (json) {
|
|
|
|
document.querySelector('.share')
|
|
|
|
.href = `#view/${btoa(JSON.stringify(json))}`;
|
|
|
|
document.querySelectorAll('.share')[1]
|
|
|
|
.href = `suite.html`;
|
|
|
|
};
|
|
|
|
|
2017-11-09 18:35:12 -05:00
|
|
|
const loadProject = function () {
|
2018-01-08 15:34:58 -05:00
|
|
|
let id = location.hash.substring(1).split(',')[0];
|
2017-11-09 18:35:12 -05:00
|
|
|
if (id.length < 1 || !isFinite(id)) {
|
2017-11-16 15:43:51 -05:00
|
|
|
id = projectInput.value;
|
2017-11-09 18:35:12 -05:00
|
|
|
}
|
|
|
|
Scratch.vm.downloadProjectId(id);
|
2018-01-16 18:20:26 -05:00
|
|
|
return id;
|
2017-11-09 18:35:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Asset} asset - calculate a URL for this asset.
|
|
|
|
* @returns {string} a URL to download a project file.
|
|
|
|
*/
|
|
|
|
const getProjectUrl = function (asset) {
|
|
|
|
const assetIdParts = asset.assetId.split('.');
|
|
|
|
const assetUrlParts = [PROJECT_SERVER, 'internalapi/project/', assetIdParts[0], '/get/'];
|
|
|
|
if (assetIdParts[1]) {
|
|
|
|
assetUrlParts.push(assetIdParts[1]);
|
|
|
|
}
|
|
|
|
return assetUrlParts.join('');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Asset} asset - calculate a URL for this asset.
|
|
|
|
* @returns {string} a URL to download a project asset (PNG, WAV, etc.)
|
|
|
|
*/
|
|
|
|
const getAssetUrl = function (asset) {
|
|
|
|
const assetUrlParts = [
|
|
|
|
ASSET_SERVER,
|
|
|
|
'internalapi/asset/',
|
|
|
|
asset.assetId,
|
|
|
|
'.',
|
|
|
|
asset.dataFormat,
|
|
|
|
'/get/'
|
|
|
|
];
|
|
|
|
return assetUrlParts.join('');
|
|
|
|
};
|
|
|
|
|
|
|
|
class LoadingProgress {
|
|
|
|
constructor (callback) {
|
|
|
|
this.total = 0;
|
|
|
|
this.complete = 0;
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
|
|
|
|
2019-01-23 11:25:04 -05:00
|
|
|
on (storage, vm) {
|
2017-11-09 18:35:12 -05:00
|
|
|
const _this = this;
|
|
|
|
const _load = storage.webHelper.load;
|
|
|
|
storage.webHelper.load = function (...args) {
|
2019-01-23 11:25:04 -05:00
|
|
|
if (_this.complete === 0 && window.performance) {
|
|
|
|
// Mark in browser inspectors how long it takes to load the
|
|
|
|
// projects initial data file.
|
|
|
|
performance.mark('Scratch.LoadDataStart');
|
|
|
|
}
|
|
|
|
|
2017-11-09 18:35:12 -05:00
|
|
|
const result = _load.call(this, ...args);
|
|
|
|
_this.total += 1;
|
|
|
|
_this.callback(_this);
|
|
|
|
result.then(() => {
|
2019-01-23 11:25:04 -05:00
|
|
|
if (_this.complete === 0 && window.performance) {
|
|
|
|
// How long did loading the data file take?
|
|
|
|
performance.mark('Scratch.LoadDataEnd');
|
|
|
|
performance.measure('Scratch.LoadData', 'Scratch.LoadDataStart', 'Scratch.LoadDataEnd');
|
|
|
|
}
|
|
|
|
|
2017-11-09 18:35:12 -05:00
|
|
|
_this.complete += 1;
|
|
|
|
_this.callback(_this);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
2019-01-23 11:25:04 -05:00
|
|
|
vm.runtime.on(Runtime.PROJECT_LOADED, () => {
|
|
|
|
// Currently LoadingProgress tracks when the data has been loaded
|
|
|
|
// and not when the data has been decoded. It may be difficult to
|
|
|
|
// track that but it isn't hard to track when its all been decoded.
|
|
|
|
if (window.performance) {
|
|
|
|
// How long did it take to load the html, js, and all the
|
|
|
|
// project assets?
|
|
|
|
performance.mark('Scratch.LoadEnd');
|
|
|
|
performance.measure('Scratch.Load', 'Scratch.LoadStart', 'Scratch.LoadEnd');
|
|
|
|
}
|
|
|
|
|
|
|
|
window.ScratchVMLoadEnd = Date.now();
|
|
|
|
|
|
|
|
// With this event lets update LoadingProgress a final time so its
|
|
|
|
// displayed loading time is accurate.
|
|
|
|
_this.callback(_this);
|
|
|
|
});
|
2017-11-09 18:35:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class StatTable {
|
|
|
|
constructor ({table, keys, viewOf, isSlow}) {
|
|
|
|
this.table = table;
|
|
|
|
if (keys) {
|
|
|
|
this.keys = keys;
|
|
|
|
}
|
|
|
|
if (viewOf) {
|
|
|
|
this.viewOf = viewOf;
|
|
|
|
}
|
|
|
|
if (isSlow) {
|
|
|
|
this.isSlow = isSlow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const table = this.table;
|
|
|
|
Array.from(table.children)
|
|
|
|
.forEach(node => table.removeChild(node));
|
|
|
|
const keys = this.keys();
|
|
|
|
for (const key of keys) {
|
|
|
|
this.viewOf(key).render({
|
|
|
|
table,
|
|
|
|
isSlow: frame => this.isSlow(key, frame)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class StatView {
|
|
|
|
constructor (name) {
|
|
|
|
this.name = name;
|
|
|
|
this.executions = 0;
|
|
|
|
this.selfTime = 0;
|
|
|
|
this.totalTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
update (selfTime, totalTime) {
|
|
|
|
this.executions++;
|
|
|
|
this.selfTime += selfTime;
|
|
|
|
this.totalTime += totalTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
render ({table, isSlow}) {
|
|
|
|
const row = document.createElement('tr');
|
|
|
|
let cell = document.createElement('td');
|
|
|
|
cell.innerText = this.name;
|
|
|
|
row.appendChild(cell);
|
|
|
|
|
|
|
|
if (isSlow(this)) {
|
|
|
|
row.setAttribute('class', 'slow');
|
|
|
|
}
|
|
|
|
|
|
|
|
cell = document.createElement('td');
|
2018-01-16 18:32:19 -05:00
|
|
|
// Truncate selfTime. Value past the microsecond are floating point
|
|
|
|
// noise.
|
|
|
|
this.selfTime = Math.floor(this.selfTime * 1000) / 1000;
|
2017-11-09 18:35:12 -05:00
|
|
|
cell.innerText = (this.selfTime / 1000).toPrecision(3);
|
|
|
|
row.appendChild(cell);
|
|
|
|
|
|
|
|
cell = document.createElement('td');
|
2018-01-16 18:32:19 -05:00
|
|
|
// Truncate totalTime. Value past the microsecond are floating point
|
|
|
|
// noise.
|
|
|
|
this.totalTime = Math.floor(this.totalTime * 1000) / 1000;
|
2017-11-09 18:35:12 -05:00
|
|
|
cell.innerText = (this.totalTime / 1000).toPrecision(3);
|
|
|
|
row.appendChild(cell);
|
|
|
|
|
|
|
|
cell = document.createElement('td');
|
|
|
|
cell.innerText = this.executions;
|
|
|
|
row.appendChild(cell);
|
|
|
|
|
|
|
|
table.appendChild(row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RunningStats {
|
|
|
|
constructor (profiler) {
|
|
|
|
this.stepThreadsInnerId = profiler.idByName('Sequencer.stepThreads#inner');
|
|
|
|
this.blockFunctionId = profiler.idByName('blockFunction');
|
|
|
|
this.stpeThreadsId = profiler.idByName('Sequencer.stepThreads');
|
|
|
|
|
|
|
|
this.recordedTime = 0;
|
|
|
|
this.executed = {
|
|
|
|
steps: 0,
|
|
|
|
blocks: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
update (id, selfTime, totalTime) {
|
|
|
|
if (id === this.stpeThreadsId) {
|
|
|
|
this.recordedTime += totalTime;
|
|
|
|
} else if (id === this.stepThreadsInnerId) {
|
|
|
|
this.executed.steps++;
|
|
|
|
} else if (id === this.blockFunctionId) {
|
|
|
|
this.executed.blocks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const WORK_TIME = 0.75;
|
|
|
|
|
|
|
|
class RunningStatsView {
|
|
|
|
constructor ({runningStats, maxRecordedTime, dom}) {
|
|
|
|
this.recordedTimeDom =
|
|
|
|
dom.getElementsByClassName('profile-count-amount-recorded')[0];
|
|
|
|
this.stepsLoopedDom =
|
|
|
|
dom.getElementsByClassName('profile-count-steps-looped')[0];
|
|
|
|
this.blocksExecutedDom =
|
|
|
|
dom.getElementsByClassName('profile-count-blocks-executed')[0];
|
|
|
|
|
|
|
|
this.maxRecordedTime = maxRecordedTime;
|
|
|
|
this.maxWorkedTime = maxRecordedTime * WORK_TIME;
|
|
|
|
this.runningStats = runningStats;
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
runningStats,
|
|
|
|
recordedTimeDom,
|
|
|
|
stepsLoopedDom,
|
|
|
|
blocksExecutedDom
|
|
|
|
} = this;
|
|
|
|
const {executed} = runningStats;
|
|
|
|
const fractionWorked = runningStats.recordedTime / this.maxWorkedTime;
|
|
|
|
recordedTimeDom.innerText = `${(fractionWorked * 100).toFixed(1)} %`;
|
|
|
|
stepsLoopedDom.innerText = executed.steps;
|
|
|
|
blocksExecutedDom.innerText = executed.blocks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Frames {
|
|
|
|
constructor (profiler) {
|
|
|
|
this.profiler = profiler;
|
|
|
|
|
|
|
|
this.frames = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
update (id, selfTime, totalTime) {
|
|
|
|
if (!this.frames[id]) {
|
|
|
|
this.frames[id] = new StatView(this.profiler.nameById(id));
|
|
|
|
}
|
|
|
|
this.frames[id].update(selfTime, totalTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const frameOrder = [
|
|
|
|
'blockFunction',
|
|
|
|
'execute',
|
|
|
|
'Sequencer.stepThread',
|
|
|
|
'Sequencer.stepThreads#inner',
|
|
|
|
'Sequencer.stepThreads',
|
|
|
|
'RenderWebGL.draw',
|
|
|
|
'Runtime._step'
|
|
|
|
];
|
|
|
|
|
2018-01-16 18:20:26 -05:00
|
|
|
const trackSlowFrames = [
|
|
|
|
'Sequencer.stepThreads',
|
|
|
|
'Sequencer.stepThreads#inner',
|
|
|
|
'Sequencer.stepThread',
|
|
|
|
'execute'
|
|
|
|
];
|
|
|
|
|
2017-11-09 18:35:12 -05:00
|
|
|
class FramesTable extends StatTable {
|
|
|
|
constructor (options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
this.profiler = options.profiler;
|
|
|
|
this.frames = options.frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
keys () {
|
|
|
|
const keys = Object.keys(this.frames.frames)
|
|
|
|
.map(id => this.profiler.nameById(Number(id)));
|
|
|
|
keys.sort((a, b) => frameOrder.indexOf(a) - frameOrder.indexOf(b));
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
viewOf (key) {
|
|
|
|
return this.frames.frames[this.profiler.idByName(key)];
|
|
|
|
}
|
|
|
|
|
|
|
|
isSlow (key, frame) {
|
2018-01-16 18:20:26 -05:00
|
|
|
return (trackSlowFrames.indexOf(key) > 0 &&
|
2017-11-09 18:35:12 -05:00
|
|
|
frame.selfTime / frame.totalTime > SLOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Opcodes {
|
|
|
|
constructor (profiler) {
|
|
|
|
this.blockFunctionId = profiler.idByName('blockFunction');
|
|
|
|
|
|
|
|
this.opcodes = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
update (id, selfTime, totalTime, arg) {
|
|
|
|
if (id === this.blockFunctionId) {
|
|
|
|
if (!this.opcodes[arg]) {
|
|
|
|
this.opcodes[arg] = new StatView(arg);
|
|
|
|
}
|
|
|
|
this.opcodes[arg].update(selfTime, totalTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class OpcodeTable extends StatTable {
|
|
|
|
constructor (options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
this.profiler = options.profiler;
|
|
|
|
this.opcodes = options.opcodes;
|
|
|
|
this.frames = options.frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
keys () {
|
|
|
|
const keys = Object.keys(this.opcodes.opcodes);
|
|
|
|
keys.sort();
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
viewOf (key) {
|
|
|
|
return this.opcodes.opcodes[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
isSlow (key) {
|
|
|
|
const blockFunctionTotalTime = this.frames.frames[this.profiler.idByName('blockFunction')].totalTime;
|
|
|
|
const rowTotalTime = this.opcodes.opcodes[key].totalTime;
|
|
|
|
const percentOfRun = rowTotalTime / blockFunctionTotalTime;
|
|
|
|
return percentOfRun > SLOW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProfilerRun {
|
|
|
|
constructor ({vm, maxRecordedTime, warmUpTime}) {
|
|
|
|
this.vm = vm;
|
|
|
|
this.maxRecordedTime = maxRecordedTime;
|
|
|
|
this.warmUpTime = warmUpTime;
|
|
|
|
|
|
|
|
vm.runtime.enableProfiling();
|
|
|
|
const profiler = this.profiler = vm.runtime.profiler;
|
|
|
|
vm.runtime.profiler = null;
|
|
|
|
|
|
|
|
const runningStats = this.runningStats = new RunningStats(profiler);
|
|
|
|
const runningStatsView = this.runningStatsView = new RunningStatsView({
|
|
|
|
dom: document.getElementsByClassName('profile-count-group')[0],
|
|
|
|
|
|
|
|
runningStats,
|
2018-01-08 15:34:58 -05:00
|
|
|
maxRecordedTime
|
2017-11-09 18:35:12 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const frames = this.frames = new Frames(profiler);
|
|
|
|
this.frameTable = new FramesTable({
|
|
|
|
table: document
|
|
|
|
.getElementsByClassName('profile-count-frame-table')[0]
|
|
|
|
.getElementsByTagName('tbody')[0],
|
|
|
|
|
|
|
|
profiler,
|
|
|
|
frames
|
|
|
|
});
|
|
|
|
|
|
|
|
const opcodes = this.opcodes = new Opcodes(profiler);
|
|
|
|
this.opcodeTable = new OpcodeTable({
|
|
|
|
table: document
|
|
|
|
.getElementsByClassName('profile-count-opcode-table')[0]
|
|
|
|
.getElementsByTagName('tbody')[0],
|
|
|
|
|
|
|
|
profiler,
|
|
|
|
opcodes,
|
|
|
|
frames
|
|
|
|
});
|
|
|
|
|
|
|
|
const stepId = profiler.idByName('Runtime._step');
|
|
|
|
profiler.onFrame = ({id, selfTime, totalTime, arg}) => {
|
|
|
|
if (id === stepId) {
|
|
|
|
runningStatsView.render();
|
|
|
|
}
|
|
|
|
runningStats.update(id, selfTime, totalTime, arg);
|
|
|
|
opcodes.update(id, selfTime, totalTime, arg);
|
|
|
|
frames.update(id, selfTime, totalTime, arg);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
run () {
|
2018-01-16 18:20:26 -05:00
|
|
|
this.projectId = loadProject();
|
2017-11-09 18:35:12 -05:00
|
|
|
|
2018-01-08 15:35:43 -05:00
|
|
|
window.parent.postMessage({
|
|
|
|
type: 'BENCH_MESSAGE_LOADING'
|
|
|
|
}, '*');
|
|
|
|
|
2017-11-09 18:35:12 -05:00
|
|
|
this.vm.on('workspaceUpdate', () => {
|
|
|
|
setTimeout(() => {
|
2018-01-08 15:35:43 -05:00
|
|
|
window.parent.postMessage({
|
|
|
|
type: 'BENCH_MESSAGE_WARMING_UP'
|
|
|
|
}, '*');
|
2017-11-09 18:35:12 -05:00
|
|
|
this.vm.greenFlag();
|
|
|
|
}, 100);
|
|
|
|
setTimeout(() => {
|
2018-01-08 15:35:43 -05:00
|
|
|
window.parent.postMessage({
|
|
|
|
type: 'BENCH_MESSAGE_ACTIVE'
|
|
|
|
}, '*');
|
2017-11-09 18:35:12 -05:00
|
|
|
this.vm.runtime.profiler = this.profiler;
|
|
|
|
}, 100 + this.warmUpTime);
|
|
|
|
setTimeout(() => {
|
|
|
|
this.vm.stopAll();
|
|
|
|
clearTimeout(this.vm.runtime._steppingInterval);
|
|
|
|
this.vm.runtime.profiler = null;
|
|
|
|
|
|
|
|
this.frameTable.render();
|
|
|
|
this.opcodeTable.render();
|
2018-01-08 15:35:43 -05:00
|
|
|
|
|
|
|
window.parent.postMessage({
|
|
|
|
type: 'BENCH_MESSAGE_COMPLETE',
|
|
|
|
frames: this.frames.frames,
|
|
|
|
opcodes: this.opcodes.opcodes
|
|
|
|
}, '*');
|
2018-01-16 18:20:26 -05:00
|
|
|
|
|
|
|
setShareLink({
|
|
|
|
fixture: {
|
|
|
|
projectId: this.projectId,
|
|
|
|
warmUpTime: this.warmUpTime,
|
|
|
|
recordingTime: this.maxRecordedTime
|
|
|
|
},
|
|
|
|
frames: this.frames.frames,
|
|
|
|
opcodes: this.opcodes.opcodes
|
|
|
|
});
|
2017-11-09 18:35:12 -05:00
|
|
|
}, 100 + this.warmUpTime + this.maxRecordedTime);
|
|
|
|
});
|
|
|
|
}
|
2018-01-16 18:20:26 -05:00
|
|
|
|
|
|
|
render (json) {
|
|
|
|
const {fixture} = json;
|
|
|
|
document.querySelector('[type=text]').value = [
|
|
|
|
fixture.projectId,
|
|
|
|
fixture.warmUpTime,
|
|
|
|
fixture.recordingTime
|
|
|
|
].join(',');
|
|
|
|
|
|
|
|
this.frames.frames = json.frames.map(
|
|
|
|
frame => Object.assign(new StatView(), frame, {
|
|
|
|
name: this.profiler.nameById(this.profiler.idByName(frame.name))
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.opcodes.opcodes = {};
|
|
|
|
Object.entries(json.opcodes).forEach(([opcode, data]) => {
|
|
|
|
this.opcodes.opcodes[opcode] = Object.assign(new StatView(), data);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.frameTable.render();
|
|
|
|
this.opcodeTable.render();
|
|
|
|
}
|
2017-11-09 18:35:12 -05:00
|
|
|
}
|
|
|
|
|
2018-01-16 18:20:26 -05:00
|
|
|
/**
|
|
|
|
* Run the benchmark with given parameters in the location's hash field or
|
|
|
|
* using defaults.
|
|
|
|
*/
|
|
|
|
const runBenchmark = function () {
|
2017-11-09 18:35:12 -05:00
|
|
|
// Lots of global variables to make debugging easier
|
|
|
|
// Instantiate the VM.
|
2019-01-22 17:42:34 -05:00
|
|
|
const vm = new VirtualMachine();
|
2017-11-09 18:35:12 -05:00
|
|
|
Scratch.vm = vm;
|
|
|
|
|
|
|
|
vm.setTurboMode(true);
|
|
|
|
|
2019-01-22 17:42:34 -05:00
|
|
|
const storage = new ScratchStorage();
|
2017-11-09 18:35:12 -05:00
|
|
|
const AssetType = storage.AssetType;
|
|
|
|
storage.addWebSource([AssetType.Project], getProjectUrl);
|
|
|
|
storage.addWebSource([AssetType.ImageVector, AssetType.ImageBitmap, AssetType.Sound], getAssetUrl);
|
|
|
|
vm.attachStorage(storage);
|
|
|
|
|
|
|
|
new LoadingProgress(progress => {
|
|
|
|
document.getElementsByClassName('loading-total')[0]
|
|
|
|
.innerText = progress.total;
|
|
|
|
document.getElementsByClassName('loading-complete')[0]
|
|
|
|
.innerText = progress.complete;
|
2019-01-23 11:25:04 -05:00
|
|
|
document.getElementsByClassName('loading-time')[0]
|
|
|
|
.innerText = `(${(window.ScratchVMLoadEnd || Date.now()) - window.ScratchVMLoadStart}ms)`;
|
|
|
|
}).on(storage, vm);
|
2017-11-09 18:35:12 -05:00
|
|
|
|
2018-01-08 15:34:58 -05:00
|
|
|
let warmUpTime = 4000;
|
|
|
|
let maxRecordedTime = 6000;
|
|
|
|
|
|
|
|
if (location.hash) {
|
|
|
|
const split = location.hash.substring(1).split(',');
|
|
|
|
if (split[1] && split[1].length > 0) {
|
|
|
|
warmUpTime = Number(split[1]);
|
|
|
|
}
|
|
|
|
maxRecordedTime = Number(split[2] || '0') || 6000;
|
|
|
|
}
|
|
|
|
|
2017-11-09 18:35:12 -05:00
|
|
|
new ProfilerRun({
|
|
|
|
vm,
|
2018-01-08 15:34:58 -05:00
|
|
|
warmUpTime,
|
|
|
|
maxRecordedTime
|
2017-11-09 18:35:12 -05:00
|
|
|
}).run();
|
|
|
|
|
|
|
|
// Instantiate the renderer and connect it to the VM.
|
|
|
|
const canvas = document.getElementById('scratch-stage');
|
2019-01-23 11:25:04 -05:00
|
|
|
const renderer = new ScratchRender(canvas);
|
2017-11-09 18:35:12 -05:00
|
|
|
Scratch.renderer = renderer;
|
|
|
|
vm.attachRenderer(renderer);
|
2019-01-23 11:25:04 -05:00
|
|
|
const audioEngine = new AudioEngine();
|
2017-11-09 18:35:12 -05:00
|
|
|
vm.attachAudioEngine(audioEngine);
|
2018-08-08 14:36:38 -04:00
|
|
|
vm.attachV2SVGAdapter(new ScratchSVGRenderer.SVGRenderer());
|
|
|
|
vm.attachV2BitmapAdapter(new ScratchSVGRenderer.BitmapAdapter());
|
2017-11-09 18:35:12 -05:00
|
|
|
|
|
|
|
// Feed mouse events as VM I/O events.
|
|
|
|
document.addEventListener('mousemove', e => {
|
|
|
|
const rect = canvas.getBoundingClientRect();
|
|
|
|
const coordinates = {
|
|
|
|
x: e.clientX - rect.left,
|
|
|
|
y: e.clientY - rect.top,
|
|
|
|
canvasWidth: rect.width,
|
|
|
|
canvasHeight: rect.height
|
|
|
|
};
|
|
|
|
Scratch.vm.postIOData('mouse', coordinates);
|
|
|
|
});
|
|
|
|
canvas.addEventListener('mousedown', e => {
|
|
|
|
const rect = canvas.getBoundingClientRect();
|
|
|
|
const data = {
|
|
|
|
isDown: true,
|
|
|
|
x: e.clientX - rect.left,
|
|
|
|
y: e.clientY - rect.top,
|
|
|
|
canvasWidth: rect.width,
|
|
|
|
canvasHeight: rect.height
|
|
|
|
};
|
|
|
|
Scratch.vm.postIOData('mouse', data);
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
canvas.addEventListener('mouseup', e => {
|
|
|
|
const rect = canvas.getBoundingClientRect();
|
|
|
|
const data = {
|
|
|
|
isDown: false,
|
|
|
|
x: e.clientX - rect.left,
|
|
|
|
y: e.clientY - rect.top,
|
|
|
|
canvasWidth: rect.width,
|
|
|
|
canvasHeight: rect.height
|
|
|
|
};
|
|
|
|
Scratch.vm.postIOData('mouse', data);
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Feed keyboard events as VM I/O events.
|
|
|
|
document.addEventListener('keydown', e => {
|
|
|
|
// Don't capture keys intended for Blockly inputs.
|
|
|
|
if (e.target !== document && e.target !== document.body) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Scratch.vm.postIOData('keyboard', {
|
|
|
|
keyCode: e.keyCode,
|
|
|
|
isDown: true
|
|
|
|
});
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
document.addEventListener('keyup', e => {
|
|
|
|
// Always capture up events,
|
|
|
|
// even those that have switched to other targets.
|
|
|
|
Scratch.vm.postIOData('keyboard', {
|
|
|
|
keyCode: e.keyCode,
|
|
|
|
isDown: false
|
|
|
|
});
|
|
|
|
// E.g., prevent scroll.
|
|
|
|
if (e.target !== document && e.target !== document.body) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Run threads
|
|
|
|
vm.start();
|
|
|
|
};
|
2018-01-16 18:20:26 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render previously run benchmark data.
|
|
|
|
* @param {object} json data from a previous benchmark run.
|
|
|
|
*/
|
|
|
|
const renderBenchmarkData = function (json) {
|
2019-01-22 17:42:34 -05:00
|
|
|
const vm = new VirtualMachine();
|
2018-01-16 18:20:26 -05:00
|
|
|
new ProfilerRun({vm}).render(json);
|
|
|
|
setShareLink(json);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.onload = function () {
|
|
|
|
if (location.hash.substring(1).startsWith('view')) {
|
|
|
|
document.body.className = 'render';
|
|
|
|
const data = location.hash.substring(6);
|
|
|
|
const frozen = atob(data);
|
|
|
|
const json = JSON.parse(frozen);
|
|
|
|
renderBenchmarkData(json);
|
|
|
|
} else {
|
|
|
|
runBenchmark();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.onhashchange = function () {
|
|
|
|
location.reload();
|
|
|
|
};
|
2019-01-23 11:25:04 -05:00
|
|
|
|
|
|
|
if (window.performance) {
|
|
|
|
performance.mark('Scratch.EvalEnd');
|
|
|
|
performance.measure('Scratch.Eval', 'Scratch.EvalStart', 'Scratch.EvalEnd');
|
|
|
|
}
|
|
|
|
|
|
|
|
window.ScratchVMEvalEnd = Date.now();
|