Last active
December 2, 2019 02:29
-
-
Save mourner/c0813d1a2e847cd13f47b33b432a891b to your computer and use it in GitHub Desktop.
Mapbox GL JS Puppeteer benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<meta charset="utf-8"> | |
<title>Benchmark</title> | |
<body></body> | |
<style>html, body, #map { height: 100%; margin: 0; } </style> | |
<div id="map"></div> | |
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.0/mapbox-gl.js'></script> | |
<!-- <script src="mapbox-gl.js"></script> --> | |
<script> | |
console.log(mapboxgl.version); | |
mapboxgl.accessToken = 'pk.eyJ1IjoibW91cm5lciIsImEiOiJWWnRiWG1VIn0.j6eccFHpE3Q04XPLI7JxbA'; | |
var map = new mapboxgl.Map({ | |
container: document.getElementById('map'), | |
style: 'mapbox://styles/mapbox/streets-v9' | |
}); | |
var from = { | |
center: [-77.01866, 38.888], | |
zoom: 4 | |
}; | |
var to = { | |
center: [-77.01866, 38.888], | |
zoom: 18, | |
easing: (t) => t, | |
duration: 20000 | |
}; | |
map.jumpTo(from); | |
map.on('load', function () { | |
console.log('warmup'); | |
map.flyTo(to).once('moveend', function () { | |
console.log('start'); | |
setTimeout(function () { | |
map.jumpTo(from); | |
map.flyTo(to).once('moveend', function () { console.log('end'); }); | |
}, 1000); | |
}); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const fs = require('fs'); | |
(async () => { | |
const browser = await puppeteer.launch({ | |
headless: false, | |
args: [ | |
'--headless' | |
] | |
}); | |
const page = await browser.newPage(); | |
page.setViewport({width: 1280, height: 720, deviceScaleFactor: 2}); | |
page.on('console', async (msg) => { | |
console.log(msg); | |
if (msg === 'start') { | |
page.tracing.start({path: 'trace.json'}); | |
} | |
if (msg === 'end') { | |
await page.tracing.stop(); | |
await browser.close(); | |
const traces = JSON.parse(fs.readFileSync('./trace.json')) | |
.traceEvents.filter(t => t.name === 'FireAnimationFrame'); | |
const durations = []; | |
for (let i = 0; i < traces.length; i++) { | |
let d = traces[i].dur / 1000; | |
if (d > 16) durations.push(d); | |
} | |
console.log('num: ' + durations.length); | |
console.log('percent: ' + (100 * durations.length / traces.length).toFixed(2) + '%'); | |
console.log('total: ' + durations.reduce((sum, a) => sum + a, 0)); | |
} | |
}); | |
await page.goto('file://' + __dirname + '/index.html', {waitUntil: 'networkidle'}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment