From 9259e2d63304689de7d000d2c27b1fc9927a23c0 Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Mon, 19 Jun 2023 21:25:52 -0400 Subject: [PATCH 1/3] add style benchmark --- package-lock.json | 69 ++++++++++++++++++++++++++++++++++++++++---- package.json | 3 ++ scripts/benchmark.js | 52 +++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 scripts/benchmark.js diff --git a/package-lock.json b/package-lock.json index c8b4ec366..1a7620100 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,10 +23,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", @@ -38,6 +40,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", @@ -184,7 +187,8 @@ }, "node_modules/@mapbox/vector-tile": { "version": "1.3.1", - "license": "BSD-3-Clause", + "resolved": "https://registry.npmjs.org/@mapbox/vector-tile/-/vector-tile-1.3.1.tgz", + "integrity": "sha512-MCEddb8u44/xfQ3oD+Srl/tNcQoqTw3goGk2oLsrFxOTc3dUp+kAnby3PvAeeBYSMSjSPD1nd1AJA6W49WnoUw==", "dependencies": { "@mapbox/point-geometry": "~0.1.0" } @@ -304,7 +308,8 @@ }, "node_modules/@types/mapbox__vector-tile": { "version": "1.3.0", - "license": "MIT", + "resolved": "https://registry.npmjs.org/@types/mapbox__vector-tile/-/mapbox__vector-tile-1.3.0.tgz", + "integrity": "sha512-kDwVreQO5V4c8yAxzZVQLE5tyWF+IPToAanloQaSnwfXmIcJ7cyOrv8z4Ft4y7PsLYmhWXmON8MBV8RX0Rgr8g==", "dependencies": { "@types/geojson": "*", "@types/mapbox__point-geometry": "*", @@ -326,7 +331,8 @@ }, "node_modules/@types/pbf": { "version": "3.0.2", - "license": "MIT" + "resolved": "https://registry.npmjs.org/@types/pbf/-/pbf-3.0.2.tgz", + "integrity": "sha512-EDrLIPaPXOZqDjrkzxxbX7UlJSeQVgah3i0aA4pOSzmK9zq3BIh7/MZIQxED7slJByvKM4Gc6Hypyu2lJzh3SQ==" }, "node_modules/@xmldom/xmldom": { "version": "0.8.6", @@ -491,6 +497,16 @@ ], "license": "MIT" }, + "node_modules/benchmark": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/benchmark/-/benchmark-2.1.4.tgz", + "integrity": "sha512-l9MlfN4M1K/H2fbhfMy3B7vJd6AGKJVQn2h6Sg/Yx+KckoUA7ewS5Vv6TjSq18ooE1kS9hhAlQRH3AkXIh/aOQ==", + "dev": true, + "dependencies": { + "lodash": "^4.17.4", + "platform": "^1.3.3" + } + }, "node_modules/binary-extensions": { "version": "2.2.0", "dev": true, @@ -2074,6 +2090,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "node_modules/log-symbols": { "version": "4.1.0", "dev": true, @@ -2885,7 +2907,8 @@ }, "node_modules/pbf": { "version": "3.2.1", - "license": "BSD-3-Clause", + "resolved": "https://registry.npmjs.org/pbf/-/pbf-3.2.1.tgz", + "integrity": "sha512-ClrV7pNOn7rtmoQVF4TS1vyU0WhYRnP92fzbfF75jAIwpnzdJXf8iTd4CMEqO4yUenH6NDqLiwjqlh6QgZzgLQ==", "dependencies": { "ieee754": "^1.1.12", "resolve-protobuf-schema": "^2.1.0" @@ -2929,6 +2952,12 @@ "node": ">=4" } }, + "node_modules/platform": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/platform/-/platform-1.3.6.tgz", + "integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==", + "dev": true + }, "node_modules/potpack": { "version": "1.0.2", "license": "ISC" @@ -4199,6 +4228,8 @@ }, "@mapbox/vector-tile": { "version": "1.3.1", + "resolved": "https://registry.npmjs.org/@mapbox/vector-tile/-/vector-tile-1.3.1.tgz", + "integrity": "sha512-MCEddb8u44/xfQ3oD+Srl/tNcQoqTw3goGk2oLsrFxOTc3dUp+kAnby3PvAeeBYSMSjSPD1nd1AJA6W49WnoUw==", "requires": { "@mapbox/point-geometry": "~0.1.0" } @@ -4300,6 +4331,8 @@ }, "@types/mapbox__vector-tile": { "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@types/mapbox__vector-tile/-/mapbox__vector-tile-1.3.0.tgz", + "integrity": "sha512-kDwVreQO5V4c8yAxzZVQLE5tyWF+IPToAanloQaSnwfXmIcJ7cyOrv8z4Ft4y7PsLYmhWXmON8MBV8RX0Rgr8g==", "requires": { "@types/geojson": "*", "@types/mapbox__point-geometry": "*", @@ -4320,7 +4353,9 @@ "peer": true }, "@types/pbf": { - "version": "3.0.2" + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/pbf/-/pbf-3.0.2.tgz", + "integrity": "sha512-EDrLIPaPXOZqDjrkzxxbX7UlJSeQVgah3i0aA4pOSzmK9zq3BIh7/MZIQxED7slJByvKM4Gc6Hypyu2lJzh3SQ==" }, "@xmldom/xmldom": { "version": "0.8.6" @@ -4418,6 +4453,16 @@ "version": "1.5.1", "dev": true }, + "benchmark": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/benchmark/-/benchmark-2.1.4.tgz", + "integrity": "sha512-l9MlfN4M1K/H2fbhfMy3B7vJd6AGKJVQn2h6Sg/Yx+KckoUA7ewS5Vv6TjSq18ooE1kS9hhAlQRH3AkXIh/aOQ==", + "dev": true, + "requires": { + "lodash": "^4.17.4", + "platform": "^1.3.3" + } + }, "binary-extensions": { "version": "2.2.0", "dev": true @@ -5439,6 +5484,12 @@ "p-locate": "^5.0.0" } }, + "lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "log-symbols": { "version": "4.1.0", "dev": true, @@ -5979,6 +6030,8 @@ }, "pbf": { "version": "3.2.1", + "resolved": "https://registry.npmjs.org/pbf/-/pbf-3.2.1.tgz", + "integrity": "sha512-ClrV7pNOn7rtmoQVF4TS1vyU0WhYRnP92fzbfF75jAIwpnzdJXf8iTd4CMEqO4yUenH6NDqLiwjqlh6QgZzgLQ==", "requires": { "ieee754": "^1.1.12", "resolve-protobuf-schema": "^2.1.0" @@ -6000,6 +6053,12 @@ "version": "3.0.0", "dev": true }, + "platform": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/platform/-/platform-1.3.6.tgz", + "integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==", + "dev": true + }, "potpack": { "version": "1.0.2" }, diff --git a/package.json b/package.json index 616704c17..ccf35de2e 100644 --- a/package.json +++ b/package.json @@ -50,10 +50,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", @@ -65,6 +67,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", diff --git a/scripts/benchmark.js b/scripts/benchmark.js new file mode 100644 index 000000000..71c535457 --- /dev/null +++ b/scripts/benchmark.js @@ -0,0 +1,52 @@ +"use strict"; + +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 tile = await ( + await fetch(`https://d1zqyi8v6vm8p9.cloudfront.net/planet/12/1207/1539.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: [], + }); +} + +let num = 0; +new Benchmark.Suite() + .add("evaluate expressions", () => { + 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++; + } + } + } + }) + .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}`); + }) + .on("complete", () => console.log(`total ${num}`)) + .run(); From 461a8b49729c6ae009565313176cdb451b71533c Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Mon, 19 Jun 2023 21:35:31 -0400 Subject: [PATCH 2/3] test a few areas --- scripts/benchmark.js | 49 ++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/scripts/benchmark.js b/scripts/benchmark.js index 71c535457..1bbe59dc2 100644 --- a/scripts/benchmark.js +++ b/scripts/benchmark.js @@ -10,25 +10,28 @@ 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 tile = await ( - await fetch(`https://d1zqyi8v6vm8p9.cloudfront.net/planet/12/1207/1539.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: [], - }); -} -let num = 0; -new Benchmark.Suite() - .add("evaluate expressions", () => { +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, @@ -42,11 +45,17 @@ new Benchmark.Suite() } } } - }) + }); +} + +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}`); }) - .on("complete", () => console.log(`total ${num}`)) .run(); From ea0fd5b2d696805764bb04129ea88e0b3fc5d7a6 Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Mon, 19 Jun 2023 21:45:19 -0400 Subject: [PATCH 3/3] no strict --- scripts/benchmark.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/scripts/benchmark.js b/scripts/benchmark.js index 1bbe59dc2..abe4a0e96 100644 --- a/scripts/benchmark.js +++ b/scripts/benchmark.js @@ -1,5 +1,3 @@ -"use strict"; - import Benchmark from "benchmark"; import { expression } from "@maplibre/maplibre-gl-style-spec";