|
| 1 | +import { expect, test } from "bun:test"; |
| 2 | +import { createHash } from "node:crypto"; |
| 3 | +import { mkdtemp, rm, stat } from "node:fs/promises"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { downloadDriveRevision } from "../../lib/media-transfer"; |
| 7 | + |
| 8 | +test.skipIf(process.env.MEDIA_SERVER_TRANSFER_PERFORMANCE_TESTS !== "1")( |
| 9 | + "streams and verifies a transfer larger than two GiB with bounded memory", |
| 10 | + async () => { |
| 11 | + const directory = await mkdtemp(join(tmpdir(), "cap-large-transfer-")); |
| 12 | + const path = join(directory, "input.bin"); |
| 13 | + const chunk = Buffer.alloc(1024 ** 2, 0x5a); |
| 14 | + const size = 2 * 1024 ** 3 + chunk.length; |
| 15 | + const expected = createHash("sha256"); |
| 16 | + for (let offset = 0; offset < size; offset += chunk.length) |
| 17 | + expected.update(chunk); |
| 18 | + const sha256 = expected.digest("hex"); |
| 19 | + let requests = 0; |
| 20 | + let transferred = 0; |
| 21 | + let peakRss = process.memoryUsage().rss; |
| 22 | + const timer = setInterval(() => { |
| 23 | + peakRss = Math.max(peakRss, process.memoryUsage().rss); |
| 24 | + }, 25); |
| 25 | + const started = performance.now(); |
| 26 | + try { |
| 27 | + const fetcher = Object.assign( |
| 28 | + async () => { |
| 29 | + requests++; |
| 30 | + let sent = 0; |
| 31 | + return new Response( |
| 32 | + new ReadableStream<Uint8Array>({ |
| 33 | + pull(controller) { |
| 34 | + if (sent === size) controller.close(); |
| 35 | + else { |
| 36 | + controller.enqueue(chunk); |
| 37 | + sent += chunk.length; |
| 38 | + } |
| 39 | + }, |
| 40 | + }), |
| 41 | + { headers: { "Content-Length": String(size) } }, |
| 42 | + ); |
| 43 | + }, |
| 44 | + { preconnect: fetch.preconnect }, |
| 45 | + ); |
| 46 | + await downloadDriveRevision( |
| 47 | + { |
| 48 | + version: 1, |
| 49 | + url: "https://www.googleapis.com/drive/v3/files/test/revisions/test?alt=media", |
| 50 | + authorization: "Bearer synthetic-test", |
| 51 | + objectIdentity: '"synthetic-large-revision"', |
| 52 | + size, |
| 53 | + sha256, |
| 54 | + }, |
| 55 | + path, |
| 56 | + { |
| 57 | + fetcher, |
| 58 | + onBytes: (bytes) => { |
| 59 | + transferred += bytes; |
| 60 | + }, |
| 61 | + }, |
| 62 | + ); |
| 63 | + const actual = createHash("sha256"); |
| 64 | + for await (const bytes of Bun.file(path).stream()) actual.update(bytes); |
| 65 | + expect(actual.digest("hex")).toBe(sha256); |
| 66 | + expect((await stat(path)).size).toBe(size); |
| 67 | + expect(transferred).toBe(size); |
| 68 | + expect(requests).toBe(1); |
| 69 | + expect(peakRss).toBeLessThan(512 * 1024 ** 2); |
| 70 | + console.info("[large-transfer-verification]", { |
| 71 | + bytes: size, |
| 72 | + elapsedMs: Math.round(performance.now() - started), |
| 73 | + peakRssMB: Math.round(peakRss / 1024 ** 2), |
| 74 | + }); |
| 75 | + } finally { |
| 76 | + clearInterval(timer); |
| 77 | + await rm(directory, { recursive: true, force: true }); |
| 78 | + } |
| 79 | + }, |
| 80 | + 180_000, |
| 81 | +); |
0 commit comments