-
Notifications
You must be signed in to change notification settings - Fork 40
Add benchmarks and trim signature overhead #140
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
Open
thibmeu
wants to merge
1
commit into
cloudflare:main
Choose a base branch
from
thibmeu:remove-unecessary-slices
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,3 +1,4 @@ | ||
| dist/ | ||
| node_modules/ | ||
| target/ | ||
| target/ | ||
| bench/.trace/ |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # TypeScript benchmarks | ||
|
|
||
| The harness separates signature processing, WebCrypto, and key setup. | ||
|
|
||
| | Command | Purpose | | ||
| | --------------------- | -------------------------------------------- | | ||
| | `npm run bench` | Compare signature and JWK thumbprint latency | | ||
| | `npm run bench:trace` | Export CPU, heap, and WebCrypto profiles | | ||
|
|
||
| Tracing defaults to Web Bot Auth verification. Select another operation and | ||
| iteration count with environment variables: | ||
|
|
||
| ```sh | ||
| OP=coreSign ITERS=10000 npm run bench:trace | ||
| ``` | ||
|
|
||
| Operations are `coreSign`, `coreVerify`, `sign`, `verify`, `signerFromJWK`, and | ||
| `verifierFromJWK`. | ||
|
|
||
| Trace artifacts are written under `bench/.trace/`: | ||
|
|
||
| - `.cpuprofile`: open in speedscope or Chrome DevTools Performance | ||
| - `.heapprofile`: open in Chrome DevTools Memory to find allocation sites | ||
| - `.trace.json`: open in Perfetto to inspect WebCrypto call duration | ||
|
|
||
| CPU, heap, and WebCrypto data are collected in separate passes so tracing does | ||
| not distort the profiles. |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { readFile } from "node:fs/promises"; | ||
| import { createSignatureSync, verifySignature } from "http-message-sig"; | ||
| import { sign, verify } from "web-bot-auth"; | ||
| import { signerFromJWK, verifierFromJWK } from "web-bot-auth/crypto"; | ||
|
|
||
| const key = JSON.parse( | ||
| await readFile( | ||
| new URL("../examples/rfc9421-keys/ed25519.json", import.meta.url), | ||
| "utf8" | ||
| ) | ||
| ); | ||
|
|
||
| const request = { | ||
| kind: "request", | ||
| method: "POST", | ||
| targetUri: "https://example.com/foo?param=Value&Pet=dog", | ||
| requestTarget: "/foo?param=Value&Pet=dog", | ||
| fields: [ | ||
| { name: "Host", value: "example.com" }, | ||
| { name: "Date", value: "Tue, 20 Apr 2021 02:07:55 GMT" }, | ||
| { name: "Content-Type", value: "application/json" }, | ||
| { name: "Content-Digest", value: "sha-256=:YWJjZA==:" }, | ||
| { name: "Content-Length", value: "18" }, | ||
| ], | ||
| }; | ||
| const components = [ | ||
| "@method", | ||
| "@authority", | ||
| "@path", | ||
| "content-digest", | ||
| "content-length", | ||
| "content-type", | ||
| ]; | ||
| const coreSignature = new Uint8Array([1, 2, 3]); | ||
| const coreSigner = { | ||
| algorithm: "test-alg", | ||
| sign() { | ||
| return coreSignature; | ||
| }, | ||
| }; | ||
| const coreVerifier = { | ||
| algorithm: "test-alg", | ||
| verify() { | ||
| return true; | ||
| }, | ||
| }; | ||
| const coreOptions = { | ||
| components, | ||
| parameters: { created: 1_618_884_475, alg: "test-alg", keyid: "key" }, | ||
| signer: coreSigner, | ||
| }; | ||
| const coreFields = createSignatureSync(request, coreOptions); | ||
| const signedDescriptor = { | ||
| ...request, | ||
| fields: [ | ||
| ...request.fields, | ||
| { name: "Signature", value: coreFields.signature }, | ||
| { name: "Signature-Input", value: coreFields.signatureInput }, | ||
| ], | ||
| }; | ||
| const coreVerifyOptions = { | ||
| policy: { | ||
| algorithms: ["test-alg"], | ||
| requiredComponents: components, | ||
| requiredParameters: ["created", "keyid"], | ||
| now: 1_618_884_500, | ||
| }, | ||
| resolveVerifier: () => coreVerifier, | ||
| }; | ||
|
|
||
| const signer = await signerFromJWK(key); | ||
| const verifier = await verifierFromJWK(key); | ||
| const created = new Date("2025-01-01T00:00:00Z"); | ||
| const expires = new Date("2025-01-01T01:00:00Z"); | ||
| const now = new Date("2025-01-01T00:30:00Z"); | ||
| const webRequest = new Request("https://example.com/resource", { | ||
| headers: { | ||
| "content-type": "application/json", | ||
| "signature-agent": 'sig1="https://example.com/agent";type=directory', | ||
| }, | ||
| }); | ||
| const webOptions = { | ||
| signer, | ||
| created, | ||
| expires, | ||
| signatureAgentKey: "sig1", | ||
| additionalComponents: ["content-type"], | ||
| }; | ||
| const webFields = await sign(webRequest, webOptions); | ||
| const webHeaders = new Headers(webRequest.headers); | ||
| webHeaders.set("signature", webFields.signature); | ||
| webHeaders.set("signature-input", webFields.signatureInput); | ||
| const signedWebRequest = new Request(webRequest, { headers: webHeaders }); | ||
| const webVerifyOptions = { now, resolver: () => verifier }; | ||
|
|
||
| export const operations = Object.freeze({ | ||
| coreSign: () => createSignatureSync(request, coreOptions), | ||
| coreVerify: () => verifySignature(signedDescriptor, coreVerifyOptions), | ||
| sign: () => sign(webRequest, webOptions), | ||
| verify: () => verify(signedWebRequest, webVerifyOptions), | ||
| signerFromJWK: () => signerFromJWK(key), | ||
| verifierFromJWK: () => verifierFromJWK(key), | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { bench, describe } from "vitest"; | ||
| import { operations } from "./fixtures.mjs"; | ||
|
|
||
| const options = { | ||
| time: 2_000, | ||
| warmupTime: 1_000, | ||
| }; | ||
|
|
||
| describe("HTTP message signatures", () => { | ||
| bench("core sign without crypto", operations.coreSign, options); | ||
| bench("core verify without crypto", operations.coreVerify, options); | ||
| }); | ||
|
|
||
| describe("Web Bot Auth", () => { | ||
| bench("sign Ed25519", operations.sign, options); | ||
| bench("verify Ed25519", operations.verify, options); | ||
| bench("create Ed25519 signer", operations.signerFromJWK, options); | ||
| bench("create Ed25519 verifier", operations.verifierFromJWK, options); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { mkdir, writeFile } from "node:fs/promises"; | ||
| import { Session } from "node:inspector/promises"; | ||
| import { operations } from "./fixtures.mjs"; | ||
|
|
||
| const operationName = process.env.OP ?? "verify"; | ||
| const iterations = Number(process.env.ITERS ?? "2000"); | ||
| const operation = operations[operationName]; | ||
| if (operation === undefined) { | ||
| throw new Error( | ||
| `unknown OP=${operationName} (one of: ${Object.keys(operations).join(", ")})` | ||
| ); | ||
| } | ||
| if (!Number.isSafeInteger(iterations) || iterations <= 0) { | ||
| throw new Error("ITERS must be a positive integer"); | ||
| } | ||
|
|
||
| const events = []; | ||
| function instrumentWebCrypto() { | ||
| const origin = performance.now(); | ||
| const subtle = crypto.subtle; | ||
| for (const name of Object.getOwnPropertyNames( | ||
| Object.getPrototypeOf(subtle) | ||
| )) { | ||
| if (name === "constructor" || typeof subtle[name] !== "function") continue; | ||
| const original = subtle[name].bind(subtle); | ||
| subtle[name] = (...arguments_) => { | ||
| const start = (performance.now() - origin) * 1_000; | ||
| return Promise.resolve(original(...arguments_)).finally(() => { | ||
| events.push({ | ||
| name, | ||
| ph: "X", | ||
| pid: 1, | ||
| tid: 1, | ||
| ts: start, | ||
| dur: (performance.now() - origin) * 1_000 - start, | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| async function run() { | ||
| for (let index = 0; index < iterations; index++) await operation(); | ||
| } | ||
|
|
||
| for (let index = 0; index < Math.min(iterations, 200); index++) | ||
| await operation(); | ||
|
|
||
| const cpuSession = new Session(); | ||
| cpuSession.connect(); | ||
| await cpuSession.post("Profiler.enable"); | ||
| await cpuSession.post("Profiler.setSamplingInterval", { interval: 50 }); | ||
| await cpuSession.post("Profiler.start"); | ||
| await run(); | ||
| const { profile } = await cpuSession.post("Profiler.stop"); | ||
| cpuSession.disconnect(); | ||
|
|
||
| const heapSession = new Session(); | ||
| heapSession.connect(); | ||
| await heapSession.post("HeapProfiler.enable"); | ||
| await heapSession.post("HeapProfiler.startSampling", { | ||
| samplingInterval: 4_096, | ||
| includeObjectsCollectedByMajorGC: true, | ||
| includeObjectsCollectedByMinorGC: true, | ||
| }); | ||
| await run(); | ||
| const { profile: heap } = await heapSession.post("HeapProfiler.stopSampling"); | ||
| heapSession.disconnect(); | ||
|
|
||
| instrumentWebCrypto(); | ||
| await run(); | ||
|
|
||
| await mkdir("bench/.trace", { recursive: true }); | ||
| const output = `bench/.trace/${operationName}`; | ||
| await Promise.all([ | ||
| writeFile(`${output}.cpuprofile`, JSON.stringify(profile)), | ||
| writeFile(`${output}.heapprofile`, JSON.stringify(heap)), | ||
| writeFile(`${output}.trace.json`, JSON.stringify({ traceEvents: events })), | ||
| ]); | ||
| console.log( | ||
| `wrote ${output}.{cpuprofile,heapprofile,trace.json} (${events.length} WebCrypto calls)` | ||
| ); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no
toLowerCasecheck needed?