forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix memory leak in
new Request(request)
(oven-sh#12387)
- Loading branch information
1 parent
cd97c21
commit 41a5ebe
Showing
2 changed files
with
122 additions
and
25 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
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,103 @@ | ||
import { test, expect } from "bun:test"; | ||
|
||
const constructorArgs = [ | ||
[ | ||
new Request("http://foo/", { | ||
body: "ahoyhoy", | ||
method: "POST", | ||
}), | ||
], | ||
[ | ||
"http://foo/", | ||
{ | ||
body: "ahoyhoy", | ||
method: "POST", | ||
}, | ||
], | ||
[ | ||
new URL("http://foo/"), | ||
{ | ||
body: "ahoyhoy", | ||
method: "POST", | ||
}, | ||
], | ||
[ | ||
new Request("http://foo/", { | ||
body: "ahoyhoy", | ||
method: "POST", | ||
headers: { | ||
"test-header": "value", | ||
}, | ||
}), | ||
], | ||
[ | ||
"http://foo/", | ||
{ | ||
body: "ahoyhoy", | ||
method: "POST", | ||
headers: { | ||
"test-header": "value", | ||
}, | ||
}, | ||
], | ||
[ | ||
new URL("http://foo/"), | ||
{ | ||
body: "ahoyhoy", | ||
method: "POST", | ||
headers: { | ||
"test-header": "value", | ||
}, | ||
}, | ||
], | ||
]; | ||
for (let i = 0; i < constructorArgs.length; i++) { | ||
const args = constructorArgs[i]; | ||
test("new Request(test #" + i + ")", () => { | ||
Bun.gc(true); | ||
|
||
for (let i = 0; i < 1000; i++) { | ||
new Request(...args); | ||
} | ||
|
||
Bun.gc(true); | ||
const baseline = (process.memoryUsage.rss() / 1024 / 1024) | 0; | ||
for (let i = 0; i < 2000; i++) { | ||
for (let j = 0; j < 500; j++) { | ||
new Request(...args); | ||
} | ||
Bun.gc(); | ||
} | ||
Bun.gc(true); | ||
|
||
const memory = (process.memoryUsage.rss() / 1024 / 1024) | 0; | ||
const delta = Math.max(memory, baseline) - Math.min(baseline, memory); | ||
console.log("RSS delta: ", delta, "MB"); | ||
expect(delta).toBeLessThan(30); | ||
}); | ||
|
||
test("request.clone(test #" + i + ")", () => { | ||
Bun.gc(true); | ||
|
||
for (let i = 0; i < 1000; i++) { | ||
const request = new Request(...args); | ||
request.clone(); | ||
} | ||
|
||
Bun.gc(true); | ||
const baseline = (process.memoryUsage.rss() / 1024 / 1024) | 0; | ||
for (let i = 0; i < 2000; i++) { | ||
for (let j = 0; j < 500; j++) { | ||
const request = new Request(...args); | ||
request.clone(); | ||
} | ||
Bun.gc(); | ||
} | ||
Bun.gc(true); | ||
|
||
const memory = (process.memoryUsage.rss() / 1024 / 1024) | 0; | ||
const delta = Math.max(memory, baseline) - Math.min(baseline, memory); | ||
console.log("RSS delta: ", delta, "MB"); | ||
expect(delta).toBeLessThan(30); | ||
}); | ||
} |