-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor "set data perf" benchmark utility
- Loading branch information
Lucas Wojciechowski
committed
Sep 27, 2016
1 parent
6d71d52
commit 1ab2e44
Showing
3 changed files
with
19 additions
and
26 deletions.
There are no files selected for viewing
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
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
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
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; | ||
} |