Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add style benchmark #883

Merged
merged 7 commits into from
Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 64 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -47,10 +47,12 @@
},
"devDependencies": {
"@basemaps/sprites": "^6.32.1",
"@mapbox/vector-tile": "^1.3.1",
"@maplibre/maplibre-gl-style-spec": "^17.0.1",
"@types/chai": "^4.3.4",
"@types/color-namer": "^1.3.0",
"@types/mocha": "^10.0.1",
"benchmark": "^2.1.4",
"canvas": "^2.11.0",
"chai": "^4.3.7",
"color-namer": "^1.4.0",
@@ -62,6 +64,7 @@
"mocha": "^10.1.0",
"npm-run-all": "^4.1.5",
"open": "^8.4.2",
"pbf": "^3.2.1",
"prettier": "^2.3.2",
"shx": "^0.3.4",
"svgo": "^2.8.0",
59 changes: 59 additions & 0 deletions scripts/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Benchmark from "benchmark";
import { expression } from "@maplibre/maplibre-gl-style-spec";

import { build } from "../src/layer/index.js";
import { VectorTile } from "@mapbox/vector-tile";
import Pbf from "pbf";

const layers = build(["en"])
.filter((layer) => layer["source-layer"] === "transportation" && layer.filter)
.map((layer) => expression.createExpression(layer.filter).value.expression);

const suite = new Benchmark.Suite();

async function addTest(name, z, x, y) {
const tile = await (
await fetch(
`https://d1zqyi8v6vm8p9.cloudfront.net/planet/${z}/${x}/${y}.mvt`
)
).arrayBuffer();

const transportation = new VectorTile(new Pbf(tile)).layers["transportation"];
const features = [];

for (let i = 0; i < transportation.length; i++) {
const feature = transportation.feature(i);
features.push({
type: feature.type,
properties: feature.properties,
geometry: [],
});
}
suite.add(`evaluate expressions ${name}`, () => {
let num = 0;
const context = {
properties: () => context.feature.properties,
geometryType: () => context.feature.type,
};
for (const layer of layers) {
for (const feature of features) {
context.feature = feature;
if (layer.evaluate(context)) {
num++;
}
}
}
});
}

await addTest("nyc z12", 12, 1207, 1539);
await addTest("boston z12", 12, 1239, 1514);
await addTest("kansas z14", 14, 3707, 6302);

suite
.on("error", (event) => console.log(event.target.error))
.on("cycle", (event) => {
const time = 1_000 / event.target.hz;
console.log(`${time.toPrecision(4)}ms ${event.target}`);
})
.run();