-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,58 @@ | ||
import { beforeAll, afterAll, test, expect } from "bun:test"; | ||
import type { Subprocess } from "bun"; | ||
|
||
var cat: Subprocess<"pipe", "pipe", "inherit">; | ||
var r: ReadableStreamDefaultReader<Uint8Array>; | ||
|
||
const BYTES_TO_WRITE = 1_000_000; | ||
beforeAll(() => { | ||
cat = Bun.spawn(["cat"], { | ||
stdin: "pipe", | ||
stdout: "pipe", | ||
}); | ||
r = cat.stdout.getReader() as any; | ||
}); | ||
|
||
afterAll(() => { | ||
r = null!; | ||
cat.stdin.end(); | ||
cat.kill(); | ||
}); | ||
|
||
async function readAndWrite(bytes = BYTES_TO_WRITE) { | ||
const buf = new Uint8Array(bytes); | ||
await cat.stdin.write(buf); | ||
|
||
let i = 0; | ||
for (let chunks = 0; chunks < bytes; chunks++) { | ||
const { value } = await r.read(); | ||
i += value?.length ?? 0; | ||
if (i >= buf.length) { | ||
return; | ||
} | ||
} | ||
throw new Error("infinite loop"); | ||
} | ||
|
||
// https://github.com/oven-sh/bun/issues/12198 | ||
test("PullIntoDescriptors do not leak buffers", async () => { | ||
const rounds = 100; | ||
const warmup = 10; | ||
|
||
for (let i = 0; i < warmup; i++) { | ||
await readAndWrite(10_000); | ||
} | ||
Bun.gc(true); | ||
const { arrayBuffers: arrayBuffersBefore, heapUsed: heapUsedBefore } = process.memoryUsage(); | ||
|
||
for (let i = 0; i < rounds; i++) { | ||
await readAndWrite(); | ||
} | ||
Bun.gc(true); | ||
const { arrayBuffers: arrayBuffersAfter, heapUsed: heapUsedAfter } = process.memoryUsage(); | ||
|
||
const newArrayBuffers = arrayBuffersAfter - arrayBuffersBefore; | ||
const newHeapUsed = heapUsedAfter - heapUsedBefore; | ||
expect(newArrayBuffers).toBeLessThan(rounds / 2); | ||
expect(newHeapUsed).toBeLessThan((BYTES_TO_WRITE * rounds) / 2); | ||
}); |