VeryGoodOpenSource / dart_frog

A fast, minimalistic backend framework for Dart 🎯
https://dartfrog.vgv.dev
MIT License
1.87k stars 150 forks source link

perf: await context.request.body() taking 600ms to process #1583

Open Schamppu opened 2 days ago

Schamppu commented 2 days ago

Description

I have an endpoint that only uses root middleware to setup CORS for Shelf, like so:

Handler middleware(Handler handler) {
  return handler.use(requestLogger()).use(
        fromShelfMiddleware(
          shelf.corsHeaders(
            headers: {
              shelf.ACCESS_CONTROL_ALLOW_ORIGIN: '${Platform.environment['CORS_ACCEPTED_ORIGIN']!}',
              shelf.ACCESS_CONTROL_ALLOW_METHODS: 'GET, POST, PUT, DELETE, OPTIONS',
              shelf.ACCESS_CONTROL_ALLOW_HEADERS: 'Content-Type, Authorization',
              shelf.ACCESS_CONTROL_ALLOW_CREDENTIALS: 'true',
              shelf.ACCESS_CONTROL_MAX_AGE: '86400',
            },
          ),
        ),
      );
}

I run the following code in my endpoint:

final stopWatch = Stopwatch()..start();
final body = await context.request.body();
print('Request body took ${stopWatch.elapsedMilliseconds}ms');
print('Body was ${utf8.encode(body).lengthInBytes} bytes.');
stopWatch.stop();

Which gives me the following result, when using a very short request body (218 bytes): Result 1

With a longer request, the time it takes to await is practically the same: Result 2

600ms when calling await context.request.body() is way too long, and causes a performance bottleneck.

Requirements

Additional Context

Additional information:

wolfenrain commented 2 days ago

Can you provide the shelf version that is being used? You should be able to find that in your pubspec.lock

Schamppu commented 2 days ago

Shelf version from pubspec.lock is 1.4.2