mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
Make uploading files with fetch()fast (#3125)
* Make file uploads fast * Add benchmark * Update README.md * defaults * print * prettier * smaller * fix(path) fix parse behavior (#3134) * Add macro docs (#3139) * Add macro doc * Updates * Tweaks * Update doc * Update macro serialization doc * Update macro doc * `--no-macros` flag, disable macros in node_modules * invert base/filename internally (#3141) * always false * Fix broken test * Add a test sendfile() test with large file --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: Ciro Spaciari <ciro.spaciari@gmail.com> Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
This commit is contained in:
1
bench/stream-file-upload-client/.gitignore
vendored
Normal file
1
bench/stream-file-upload-client/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
hello.txt
|
||||
35
bench/stream-file-upload-client/README.md
Normal file
35
bench/stream-file-upload-client/README.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# HTTP request file upload benchmark
|
||||
|
||||
This is a simple benchmark of uploading a file to a web server in different runtimes.
|
||||
|
||||
## Usage
|
||||
|
||||
Generate a file to upload (default is `hello.txt`):
|
||||
|
||||
```bash
|
||||
bun generate-file.js
|
||||
```
|
||||
|
||||
Run the server:
|
||||
|
||||
```bash
|
||||
node server-node.mjs
|
||||
```
|
||||
|
||||
Run the benchmark in bun:
|
||||
|
||||
```bash
|
||||
bun stream-file-bun.js
|
||||
```
|
||||
|
||||
Run the benchmark in node:
|
||||
|
||||
```bash
|
||||
node stream-file-node.mjs
|
||||
```
|
||||
|
||||
Run the benchmark in deno:
|
||||
|
||||
```bash
|
||||
deno run -A stream-file-deno.js
|
||||
```
|
||||
8
bench/stream-file-upload-client/generate-file.js
Normal file
8
bench/stream-file-upload-client/generate-file.js
Normal file
File diff suppressed because one or more lines are too long
15
bench/stream-file-upload-client/server-node.mjs
Normal file
15
bench/stream-file-upload-client/server-node.mjs
Normal file
@@ -0,0 +1,15 @@
|
||||
import { createServer } from "node:http";
|
||||
const server = createServer((req, res) => {
|
||||
var chunkSize = 0;
|
||||
req.on("data", chunk => {
|
||||
chunkSize += chunk.byteLength;
|
||||
});
|
||||
|
||||
req.on("end", () => {
|
||||
console.log("Received", chunkSize, "bytes");
|
||||
res.end(`${chunkSize}`);
|
||||
});
|
||||
});
|
||||
server.listen(parseInt(process.env.PORT ?? "3000"), (err, port) => {
|
||||
console.log(`http://localhost:${server.address().port}`);
|
||||
});
|
||||
9
bench/stream-file-upload-client/stream-file-bun.js
Normal file
9
bench/stream-file-upload-client/stream-file-bun.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { file } from "bun";
|
||||
console.time("stream-file-bun");
|
||||
const response = await fetch(process.env.URL ?? "http://localhost:3000", {
|
||||
method: "POST",
|
||||
body: file(process.env.FILE ?? "hello.txt"),
|
||||
});
|
||||
console.timeEnd("stream-file-bun");
|
||||
|
||||
console.log("Sent", await response.text(), "bytes");
|
||||
12
bench/stream-file-upload-client/stream-file-deno.js
Normal file
12
bench/stream-file-upload-client/stream-file-deno.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const file = await Deno.open(Deno.env.get("FILE") ?? "hello.txt", {
|
||||
read: true,
|
||||
});
|
||||
|
||||
console.time("stream-file-deno");
|
||||
const response = await fetch(Deno.env.get("URL") ?? "http://localhost:3000", {
|
||||
method: "POST",
|
||||
body: file.readable,
|
||||
});
|
||||
console.timeEnd("stream-file-deno");
|
||||
|
||||
console.log("Sent", await response.text(), "bytes");
|
||||
19
bench/stream-file-upload-client/stream-file-node.mjs
Normal file
19
bench/stream-file-upload-client/stream-file-node.mjs
Normal file
@@ -0,0 +1,19 @@
|
||||
import { createReadStream } from "node:fs";
|
||||
import http from "node:http";
|
||||
|
||||
console.time("stream-file-node");
|
||||
createReadStream(process.env.FILE ?? "hello.txt")
|
||||
.pipe(
|
||||
http
|
||||
.request(process.env.URL ?? "http://localhost:3000", {
|
||||
method: "POST",
|
||||
})
|
||||
.on("response", response => {
|
||||
response.on("data", data => {
|
||||
console.log("Sent", parseInt(data.toString(), 10), "bytes");
|
||||
});
|
||||
}),
|
||||
)
|
||||
.on("close", () => {
|
||||
console.timeEnd("stream-file-node");
|
||||
});
|
||||
Reference in New Issue
Block a user