From 8f2a6c657124f18d687b2423ede96040a584b667 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Thu, 22 Feb 2024 10:56:54 +1100 Subject: [PATCH 1/2] chore(crypto): move test scripts to own files --- crypto/crypto_test.ts | 65 ++------------------------ crypto/testdata/digest_large_inputs.ts | 27 +++++++++++ crypto/testdata/digest_many_calls.ts | 25 ++++++++++ 3 files changed, 56 insertions(+), 61 deletions(-) create mode 100644 crypto/testdata/digest_large_inputs.ts create mode 100644 crypto/testdata/digest_many_calls.ts diff --git a/crypto/crypto_test.ts b/crypto/crypto_test.ts index fadc95d5ec84..2df53b7cb5ef 100644 --- a/crypto/crypto_test.ts +++ b/crypto/crypto_test.ts @@ -2,12 +2,9 @@ import { assert, assertEquals, assertInstanceOf, fail } from "../assert/mod.ts"; import { crypto as stdCrypto } from "./mod.ts"; import { repeat } from "../bytes/repeat.ts"; -import { dirname, fromFileUrl } from "../path/mod.ts"; import { DigestAlgorithm, digestAlgorithms } from "./_wasm/mod.ts"; import { encodeHex } from "../encoding/hex.ts"; -const moduleDir = dirname(fromFileUrl(import.meta.url)); - const webCrypto = globalThis.crypto; Deno.test( @@ -167,35 +164,9 @@ Deno.test("digest() handles length option", async () => { }); Deno.test("digest() keeps memory usage reasonable with large inputs", async () => { - const code = ` - import { crypto as stdCrypto } from "./mod.ts"; - import { instantiateWithInstance } from "./_wasm/lib/deno_std_wasm_crypto.generated.mjs"; - import { encodeHex } from "../encoding/hex.ts"; - - const { memory } = instantiateWithInstance().instance.exports; - - const heapBytesInitial = memory.buffer.byteLength; - - const smallData = new Uint8Array(64); - const smallDigest = encodeHex(stdCrypto.subtle.digestSync("BLAKE3", smallData.buffer)); - const heapBytesAfterSmall = memory.buffer.byteLength; - - const largeData = new Uint8Array(64_000_000); - const largeDigest = encodeHex(stdCrypto.subtle.digestSync("BLAKE3", largeData.buffer)); - const heapBytesAfterLarge = memory.buffer.byteLength; - - console.log(JSON.stringify({ - heapBytesInitial, - smallDigest, - heapBytesAfterSmall, - largeDigest, - heapBytesAfterLarge, - })); - `; - const command = new Deno.Command(Deno.execPath(), { - args: ["eval", "--no-lock", code], - cwd: moduleDir, + args: ["run", "--no-lock", "crypto/testdata/digest_large_inputs.ts"], + stderr: "inherit", }); const { success, stdout } = await command.output(); @@ -249,37 +220,9 @@ Deno.test("digest() keeps memory usage reasonable with large inputs", async () = }); Deno.test("digest() keeps memory usage reasonable with many calls", async () => { - const code = ` - import { crypto as stdCrypto } from "./mod.ts"; - import { instantiateWithInstance } from "./_wasm/lib/deno_std_wasm_crypto.generated.mjs"; - import { encodeHex } from "../encoding/hex.ts"; - - const { memory } = instantiateWithInstance().instance.exports; - - const heapBytesInitial = memory.buffer.byteLength; - - let state = new ArrayBuffer(0); - - for (let i = 0; i < 1_000_000; i++) { - state = stdCrypto.subtle.digestSync({ - name: "BLAKE3" - }, state); - } - - const heapBytesFinal = memory.buffer.byteLength; - - const stateFinal = encodeHex(state); - - console.log(JSON.stringify({ - heapBytesInitial, - heapBytesFinal, - stateFinal, - })); - `; - const command = new Deno.Command(Deno.execPath(), { - args: ["eval", "--no-lock", code], - cwd: moduleDir, + args: ["run", "--no-lock", "crypto/testdata/digest_many_calls.ts"], + stderr: "inherit", }); const { stdout, success } = await command.output(); const output = new TextDecoder().decode(stdout); diff --git a/crypto/testdata/digest_large_inputs.ts b/crypto/testdata/digest_large_inputs.ts new file mode 100644 index 000000000000..a08851c108f7 --- /dev/null +++ b/crypto/testdata/digest_large_inputs.ts @@ -0,0 +1,27 @@ +import { crypto as stdCrypto } from "../crypto.ts"; +import { instantiateWithInstance } from "../_wasm/lib/deno_std_wasm_crypto.generated.mjs"; +import { encodeHex } from "../../encoding/hex.ts"; + +const { memory } = instantiateWithInstance().instance.exports; + +const heapBytesInitial = memory.buffer.byteLength; + +const smallData = new Uint8Array(64); +const smallDigest = encodeHex( + stdCrypto.subtle.digestSync("BLAKE3", smallData.buffer), +); +const heapBytesAfterSmall = memory.buffer.byteLength; + +const largeData = new Uint8Array(64_000_000); +const largeDigest = encodeHex( + stdCrypto.subtle.digestSync("BLAKE3", largeData.buffer), +); +const heapBytesAfterLarge = memory.buffer.byteLength; + +console.log(JSON.stringify({ + heapBytesInitial, + smallDigest, + heapBytesAfterSmall, + largeDigest, + heapBytesAfterLarge, +})); diff --git a/crypto/testdata/digest_many_calls.ts b/crypto/testdata/digest_many_calls.ts new file mode 100644 index 000000000000..050f57e8912a --- /dev/null +++ b/crypto/testdata/digest_many_calls.ts @@ -0,0 +1,25 @@ +import { crypto as stdCrypto } from "../crypto.ts"; +import { instantiateWithInstance } from "../_wasm/lib/deno_std_wasm_crypto.generated.mjs"; +import { encodeHex } from "../../encoding/hex.ts"; + +const { memory } = instantiateWithInstance().instance.exports; + +const heapBytesInitial = memory.buffer.byteLength; + +let state = new ArrayBuffer(0); + +for (let i = 0; i < 1_000_000; i++) { + state = stdCrypto.subtle.digestSync({ + name: "BLAKE3", + }, state); +} + +const heapBytesFinal = memory.buffer.byteLength; + +const stateFinal = encodeHex(state); + +console.log(JSON.stringify({ + heapBytesInitial, + heapBytesFinal, + stateFinal, +})); From e2ed8edba61921d5ba60f20697f02811d02d5aff Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Thu, 22 Feb 2024 11:01:00 +1100 Subject: [PATCH 2/2] copyright --- crypto/testdata/digest_large_inputs.ts | 1 + crypto/testdata/digest_many_calls.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/crypto/testdata/digest_large_inputs.ts b/crypto/testdata/digest_large_inputs.ts index a08851c108f7..321c46eeb6a0 100644 --- a/crypto/testdata/digest_large_inputs.ts +++ b/crypto/testdata/digest_large_inputs.ts @@ -1,3 +1,4 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { crypto as stdCrypto } from "../crypto.ts"; import { instantiateWithInstance } from "../_wasm/lib/deno_std_wasm_crypto.generated.mjs"; import { encodeHex } from "../../encoding/hex.ts"; diff --git a/crypto/testdata/digest_many_calls.ts b/crypto/testdata/digest_many_calls.ts index 050f57e8912a..ba07f761b696 100644 --- a/crypto/testdata/digest_many_calls.ts +++ b/crypto/testdata/digest_many_calls.ts @@ -1,3 +1,4 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { crypto as stdCrypto } from "../crypto.ts"; import { instantiateWithInstance } from "../_wasm/lib/deno_std_wasm_crypto.generated.mjs"; import { encodeHex } from "../../encoding/hex.ts";