Skip to content

Commit

Permalink
Refactor "set data perf" benchmark utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Sep 27, 2016
1 parent 6d71d52 commit 1ab2e44
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
3 changes: 1 addition & 2 deletions bench/benchmarks/geojson_setdata_large.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ module.exports = function() {

map.on('load', function() {
map = setupGeoJSONMap(map);
var source = map.getSource('geojson');

setDataPerf(source, 50, data, function(err, ms) {
setDataPerf(map.style.sources['geojson'], data, function(err, ms) {
if (err) return evented.fire('error', {error: err});
map.remove();
evented.fire('end', {message: formatNumber(ms) + ' ms', score: ms});
Expand Down
4 changes: 1 addition & 3 deletions bench/benchmarks/geojson_setdata_small.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ module.exports = function() {
map.on('load', function() {
map = setupGeoJSONMap(map);

var source = map.getSource('geojson');

setDataPerf(source, 50, featureCollection, function(err, ms) {
setDataPerf(map.style.sources['geojson'], featureCollection, function(err, ms) {
map.remove();
if (err) return evented.fire('error', {error: err});
evented.fire('end', {message: formatNumber(ms) + ' ms', score: ms});
Expand Down
38 changes: 17 additions & 21 deletions bench/lib/set_data_perf.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
'use strict';

var NUM_TILES = 6;

module.exports = function(source, numCalls, geojson, cb) {
var tileCount = 0;
module.exports = function(sourceCache, data, callback) {
var sampleCount = 50;
var startTime = null;
var times = [];

source.on('data', function tileCounter(event) {
if (event.dataType !== 'tile') return;
var samples = [];

tileCount++;
if (tileCount === NUM_TILES) {
tileCount = 0;
times.push(performance.now() - startTime);

if (times.length < numCalls) {
sourceCache.on('data', function onData() {
if (sourceCache.loaded()) {
samples.push(performance.now() - startTime);
sourceCache.off('data', onData);
if (samples.length < sampleCount) {
startTime = performance.now();
source.setData(geojson);
sourceCache.clearTiles();
sourceCache.on('data', onData);
sourceCache.getSource().setData(data);
} else {
var avgTileTime = times.reduce(function (v, t) {
return v + t;
}, 0) / times.length;
source.off('data', tileCounter);
cb(null, avgTileTime);
callback(null, average(samples));
}
}
});

startTime = performance.now();
source.setData(geojson);
sourceCache.getSource().setData(data);
};

function average(array) {
return array.reduce(function (sum, value) { return sum + value; }, 0) / array.length;
}

0 comments on commit 1ab2e44

Please sign in to comment.