diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml index adc221580..cb6bba423 100644 --- a/.github/workflows/codspeed.yml +++ b/.github/workflows/codspeed.yml @@ -6,6 +6,7 @@ on: - "package-lock.json" - "src/**" - "test/benchmarks/**" + - "web/**" - ".github/workflows/codspeed.yml" branches: - master @@ -15,6 +16,7 @@ on: - "package-lock.json" - "src/**" - "test/benchmarks/**" + - "web/**" - ".github/workflows/codspeed.yml" branches: - master diff --git a/test/benchmarks/autolinker.bench.js b/test/benchmarks/autolinker.bench.js new file mode 100644 index 000000000..dac3bae9f --- /dev/null +++ b/test/benchmarks/autolinker.bench.js @@ -0,0 +1,42 @@ +/* Copyright 2026 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { bench, describe } from "vitest"; +import { Autolinker } from "../../web/autolinker.js"; +import { createPageView } from "./autolinker_utils.js"; + +const PAGES = [ + { + kind: "2 links in each of 5000 text items", + pageView: createPageView( + Array.from( + { length: 5000 }, + (_, i) => `Contact user${i}@example.org or see https://example.com/${i}` + ) + ), + }, + { + kind: "10 links in each of 1000 text items", + pageView: createPageView(Array(1000).fill("a@b.c[".repeat(10))), + }, +]; + +describe("autolinker", () => { + for (const { kind, pageView } of PAGES) { + bench(`Autolinker.processLinks: ${kind}`, () => + Autolinker.processLinks(pageView) + ); + } +}); diff --git a/test/benchmarks/autolinker_utils.js b/test/benchmarks/autolinker_utils.js new file mode 100644 index 000000000..e76584ea7 --- /dev/null +++ b/test/benchmarks/autolinker_utils.js @@ -0,0 +1,85 @@ +/* Copyright 2026 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { PageViewport } from "../../src/display/page_viewport.js"; +import { TextHighlighter } from "../../web/text_highlighter.js"; + +const LINE_HEIGHT = 12; +const CHAR_WIDTH = 6; + +// Fixed-width geometry for matches within one text item. +class RangeStub { + #start = null; + + #end = null; + + setStart(node, offset) { + this.#start = { node, offset }; + } + + setEnd(node, offset) { + this.#end = { node, offset }; + } + + getClientRects() { + const { node, offset } = this.#start; + return [ + { + left: offset * CHAR_WIDTH, + top: node.parentNode.line * LINE_HEIGHT, + width: (this.#end.offset - offset) * CHAR_WIDTH, + height: LINE_HEIGHT, + }, + ]; + } +} + +globalThis.Node = { TEXT_NODE: 3 }; +globalThis.Range = RangeStub; + +function createTextDiv(text, line) { + const div = { nodeType: 1, line, parentNode: null, nextSibling: null }; + div.firstChild = { + nodeType: 3, + textContent: text, + parentNode: div, + nextSibling: null, + }; + return div; +} + +function createPageView(texts) { + const highlighter = new TextHighlighter({ + findController: null, + eventBus: null, + pageIndex: 0, + }); + highlighter.setTextMapping(texts.map(createTextDiv), texts); + const viewport = new PageViewport({ + viewBox: [0, 0, 612, 792], + userUnit: 1, + scale: 1, + rotation: 0, + }); + return { + _textHighlighter: highlighter, + textLayer: { + div: { getBoundingClientRect: () => ({ left: 0, top: 0 }) }, + }, + getPagePoint: (x, y) => viewport.convertToPdfPoint(x, y), + }; +} + +export { createPageView }; diff --git a/test/benchmarks/vitest.config.mjs b/test/benchmarks/vitest.config.mjs index 49df50b3d..985f6f563 100644 --- a/test/benchmarks/vitest.config.mjs +++ b/test/benchmarks/vitest.config.mjs @@ -21,6 +21,7 @@ import { transformSync } from "@babel/core"; const rootDir = fileURLToPath(new URL("../../", import.meta.url)); const srcDir = `${rootDir}src/`; +const webDir = `${rootDir}web/`; // Use the same defines as the `gulp lib-legacy` target, such that the // benchmarked code paths match the ones of the library build in Node.js. @@ -52,7 +53,10 @@ function pdfjsPreprocessorPlugin() { name: "pdfjs-preprocessor", enforce: "pre", transform(code, id) { - if (!id.startsWith(srcDir) || !id.endsWith(".js")) { + if ( + !(id.startsWith(srcDir) || id.startsWith(webDir)) || + !id.endsWith(".js") + ) { return null; } const result = transformSync(code, { @@ -94,6 +98,7 @@ export default defineConfig({ "display-binary_data_factory": `${rootDir}src/display/binary_data_factory.js`, "display-network_stream": `${rootDir}src/display/network_stream.js`, "display-node_utils": `${rootDir}src/display/node_utils.js`, + "pdfjs-lib": `${rootDir}src/pdf.js`, }, }, test: {