oakserver / oak

A middleware framework for handling HTTP with Deno, Node, Bun and Cloudflare Workers 🐿️ 🦕
https://oakserver.org
MIT License
5.2k stars 233 forks source link

Form Data (with file) parsing slow #246

Open kaleidawave opened 4 years ago

kaleidawave commented 4 years ago

I am finding that parsing form data with files is very slow. Some crude testing but oak form data parsing seems to be ~75x times longer than express's multer

With oak parsing form data with a single short string field and a 1.6mb jpeg image takes ~7500ms. On the other hand multer parses the ~100ms

Oak Deno Test:

import { Application } from "../application.ts";
import { join } from "https://deno.land/std@0.65.0/path/posix.ts";
import { Status } from "https://deno.land/std@0.67.0/http/http_status.ts";

const app = new Application();

app.addEventListener("listen", console.log);

app.use(async ctx => {
    console.time("Read file");
    const formDataReader = ctx.request.body({type: "form-data"});
    console.timeLog("Read file", "Get reader");
    const formData = await formDataReader.value.read({ outPath: join(Deno.cwd(), "out") });
    console.timeEnd("Read file");
    ctx.response.status = Status.Created;
});

await app.listen({ port: 8080 });

NodeJS Express Multer Test:

const express = require("express");
const multer = require("multer");

const upload = multer({ dest: 'uploads/' });

const app = express();

app.post("/", (req, res, next) => {
    console.time("Read file");
    next();
}, upload.single("File1"), (req, res) => {
    console.timeEnd("Read file");
    res.sendStatus(201);
});

app.listen(8080, console.log);
itohatweb commented 3 years ago

I'm not so familiar with oaks code but I think it is because of the many loops which are used to parse. Also for await can get really slow when it iterates over something big so that might be an issue too

itohatweb commented 3 years ago

Bump ^^ Currently it takes about 2 seconds to read a 7.7mb file. Would be nice if it could be fixed soon .-.

kitsonk commented 3 years ago

The biggest challenge is https://github.com/denoland/deno/issues/10157. Deno is adding a benchmark to try to focus on improving the performance (see: https://github.com/denoland/deno/pull/11862). I will try to take a look at some point if there are additional challenges in oak, but the big bottleneck at the moment is Deno.

e-osuna-g commented 3 months ago

Hi, it seems that some improvements have been done from 2021 to today, see commit: https://github.com/denoland/deno/issues/10157#issuecomment-2074403091 , do we have a benchmark on uploading big files?