Skip to content

Commit

Permalink
refactor benchmarks further
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Aug 17, 2018
1 parent e7785c5 commit 8a9710b
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 209 deletions.
67 changes: 19 additions & 48 deletions bench/benchmarks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

import Benchmark from '../lib/benchmark';
import fetchStyle from '../lib/fetch_style';
import createStyle from '../lib/create_style';
import fetchTiles from '../lib/fetch_tiles';
import parseTiles from '../lib/parse_tiles';
import StyleLayerIndex from '../../src/style/style_layer_index';
import deref from '../../src/style-spec/deref';
import TileParser from '../lib/tile_parser';
import { OverscaledTileID } from '../../src/source/tile_id';

export default class Layout extends Benchmark {
glyphs: Object;
icons: Object;
layerIndex: StyleLayerIndex;
tiles: Array<{tileID: OverscaledTileID, buffer: ArrayBuffer}>;
parser: TileParser;

setup(): Promise<void> {
const tileIDs = [
Expand All @@ -22,52 +16,29 @@ export default class Layout extends Benchmark {
new OverscaledTileID(4, 0, 4, 3, 6),
new OverscaledTileID(0, 0, 0, 0, 0)
];

return fetchStyle(`mapbox://styles/mapbox/streets-v9`)
.then((styleJSON) => {
this.layerIndex = new StyleLayerIndex(deref(styleJSON.layers));
return Promise.all([
createStyle(styleJSON),
fetchTiles((styleJSON.sources.composite: any).url, tileIDs)
]);
this.parser = new TileParser(styleJSON, 'composite');
return this.parser.setup();
})
.then(() => {
return Promise.all(tileIDs.map(tileID => this.parser.fetchTile(tileID)));
})
.then(([style, tiles]) => {
.then((tiles) => {
this.tiles = tiles;
this.glyphs = {};
this.icons = {};

const preloadImages = (params, callback) => {
style.getImages('', params, (err, icons) => {
this.icons[JSON.stringify(params)] = icons;
callback(err, icons);
});
};

const preloadGlyphs = (params, callback) => {
style.getGlyphs('', params, (err, glyphs) => {
this.glyphs[JSON.stringify(params)] = glyphs;
callback(err, glyphs);
});
};

return parseTiles('composite',
this.tiles,
this.layerIndex,
preloadImages,
preloadGlyphs)
.then(() => {});
});
// parse tiles once to populate glyph/icon cache
return Promise.all(tiles.map(tile => this.parser.parseTile(tile)));
})
.then(() => {});
}

bench() {
const loadGlyphs = (params, callback) => callback(null, this.glyphs[JSON.stringify(params)]);
const loadImages = (params, callback) => callback(null, this.icons[JSON.stringify(params)]);

return parseTiles('composite',
this.tiles,
this.layerIndex,
loadImages,
loadGlyphs)
.then(() => {});
let promise = Promise.resolve();
for (const tile of this.tiles) {
promise = promise.then(() => {
return this.parser.parseTile(tile).then(() => {});
});
}
return promise;
}
}
38 changes: 18 additions & 20 deletions bench/benchmarks/layout_dds.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// @flow

import assert from 'assert';
import Benchmark from '../lib/benchmark';
import fetchTiles from '../lib/fetch_tiles';
import parseTiles from '../lib/parse_tiles';
import StyleLayerIndex from '../../src/style/style_layer_index';
import TileParser from '../lib/tile_parser';
import { OverscaledTileID } from '../../src/source/tile_id';


const LAYER_COUNT = 2;

export default class LayoutDDS extends Benchmark {
layerIndex: StyleLayerIndex;
tiles: Array<{tileID: OverscaledTileID, buffer: ArrayBuffer}>;
parser: TileParser;

setup(): Promise<void> {
const tileIDs = [
Expand Down Expand Up @@ -91,26 +89,26 @@ export default class LayoutDDS extends Benchmark {
}
}

this.layerIndex = new StyleLayerIndex(styleJSON.layers);

return fetchTiles(styleJSON.sources.mapbox.url, tileIDs)
.then(tiles => {
this.parser = new TileParser(styleJSON, 'mapbox');
return this.parser.setup()
.then(() => {
return Promise.all(tileIDs.map(tileID => this.parser.fetchTile(tileID)));
})
.then((tiles) => {
this.tiles = tiles;
return parseTiles('mapbox',
this.tiles,
this.layerIndex,
() => assert(false), // The style above doesn't use any glyphs or icons.
() => assert(false));
// parse tiles once to populate glyph/icon cache
return Promise.all(tiles.map(tile => this.parser.parseTile(tile)));
})
.then(() => {});
}

bench() {
return parseTiles('mapbox',
this.tiles,
this.layerIndex,
() => assert(false),
() => assert(false))
.then(() => {});
let promise = Promise.resolve();
for (const tile of this.tiles) {
promise = promise.then(() => {
return this.parser.parseTile(tile).then(() => {});
});
}
return promise;
}
}
55 changes: 18 additions & 37 deletions bench/benchmarks/worker_transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import Benchmark from '../lib/benchmark';
import fetchStyle from '../lib/fetch_style';
import createStyle from '../lib/create_style';
import fetchTiles from '../lib/fetch_tiles';
import parseTiles from '../lib/parse_tiles';

import TileParser from '../lib/tile_parser';
import { OverscaledTileID } from '../../src/source/tile_id';
import { serialize } from '../../src/util/web_worker_transfer';
import StyleLayerIndex from '../../src/style/style_layer_index';
import {OverscaledTileID} from '../../src/source/tile_id';
import deref from '../../src/style-spec/deref';
import { values } from '../../src/util/util';

export default class WorkerTransfer extends Benchmark {
layerIndex: StyleLayerIndex;
parser: TileParser;
payload: Array<any>;
worker: Worker;

Expand All @@ -36,35 +32,18 @@ export default class WorkerTransfer extends Benchmark {

return fetchStyle(`mapbox://styles/mapbox/streets-v9`)
.then((styleJSON) => {
this.layerIndex = new StyleLayerIndex(deref(styleJSON.layers));
return Promise.all([
createStyle(styleJSON),
fetchTiles((styleJSON.sources.composite: any).url, tileIDs)
]);
this.parser = new TileParser(styleJSON, 'composite');
return this.parser.setup();
})
.then(([style, tiles]) => {
const preloadImages = (params, callback) => {
style.getImages('', params, (err, icons) => {
this.payload.push(barePayload(icons));
callback(err, icons);
});
};

const preloadGlyphs = (params, callback) => {
style.getGlyphs('', params, (err, glyphs) => {
this.payload.push(barePayload(glyphs));
callback(err, glyphs);
});
};

return parseTiles('composite',
tiles,
this.layerIndex,
preloadImages,
preloadGlyphs);
.then(() => {
return Promise.all(tileIDs.map(tileID => this.parser.fetchTile(tileID)));
})
.then((tiles) => {
return Promise.all(tiles.map(tile => this.parser.parseTile(tile)));
}).then((tileResults) => {
for (const data of tileResults) this.payload.push(barePayload(data));
// console.log(this.payload.map(p => JSON.stringify(p).length));
this.payload = tileResults.map(barePayload)
.concat(values(this.parser.icons).map(barePayload))
.concat(values(this.parser.glyphs).map(barePayload));
});
}

Expand All @@ -89,6 +68,8 @@ export default class WorkerTransfer extends Benchmark {
}

function barePayload(obj) {
// strip all transferables from a worker payload
return JSON.parse(JSON.stringify(serialize(obj, []), (key, value) => ArrayBuffer.isView(value) ? {} : value));
// strip all transferables from a worker payload, because we can't transfer them repeatedly in the bench:
// as soon as it's transfered once, it's no longer available on the main thread
const str = JSON.stringify(serialize(obj, []), (key, value) => ArrayBuffer.isView(value) ? {} : value);
return JSON.parse(str);
}
24 changes: 0 additions & 24 deletions bench/lib/create_style.js

This file was deleted.

17 changes: 0 additions & 17 deletions bench/lib/fetch_tiles.js

This file was deleted.

63 changes: 0 additions & 63 deletions bench/lib/parse_tiles.js

This file was deleted.

Loading

0 comments on commit 8a9710b

Please sign in to comment.