Skip to content

Instantly share code, notes, and snippets.

@mourner
Created January 17, 2017 18:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mourner/6633d7c52760bad94dd4e917b735c0ff to your computer and use it in GitHub Desktop.
Save mourner/6633d7c52760bad94dd4e917b735c0ff to your computer and use it in GitHub Desktop.
Running a Mapbox GL JS benchmark in headless Electron
mapboxgl.accessToken = 'pk.eyJ1IjoibW91cm5lciIsImEiOiJWWnRiWG1VIn0.j6eccFHpE3Q04XPLI7JxbA';
console.log('Version: ' + mapboxgl.version);
const container = document.createElement('div');
container.style.width = '1200px';
container.style.height = '700px';
document.body.appendChild(container);
const map = new mapboxgl.Map({
container: container,
style: 'mapbox://styles/mapbox/streets-v9',
center: [-77.01866, 38.888],
zoom: 13
});
map.on('load', () => {
map.repaint = true;
setTimeout(() => {
const _render = mapboxgl.Map.prototype._render;
const durations = [];
map._render = () => {
const now = performance.now();
_render.call(map);
durations.push(performance.now() - now);
};
setTimeout(() => {
map.repaint = false;
durations.sort((a, b) => a - b);
console.log('Median frame: ' + Math.round(100 * durations[Math.floor(durations.length / 2)]) / 100);
window.close();
}, 5000);
}, 1000);
});
const electron = require('electron-stream');
const fs = require('fs');
const benchSource = fs.readFileSync('./bench.js', 'utf8');
const gljsUrl = 'https://api.tiles.mapbox.com/mapbox-gl-js/v0.31.0/mapbox-gl.js';
const browser = electron();
browser.pipe(process.stdout);
browser.write(`
const script = document.createElement('script');
script.onload = () => { ${benchSource} };
script.src = '${gljsUrl}';
document.head.appendChild(script);`);
browser.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment