Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks
Schamppu opened this issue Nov 13, 2024 · 2 comments
Open
2 tasks

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

Schamppu opened this issue Nov 13, 2024 · 2 comments
Assignees
Labels
p2 Important issues not at the top of the work list performance Changes that improve performance

Comments

@Schamppu
Copy link

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

  • All CI/CD checks are passing.
  • There is no drop in the test coverage percentage.

Additional Context

Additional information:

  • Test was ran locally using dart_frog dev
  • Running on Dart Frog 1.1.0, according to pubspec.lock
  • Using shelf_cors_headers 0.1.5
  • Request data is compressed and encrypted when sent to Dart Frog server, but there was no difference that I found when not using compression/encryption.
  • I tried running without the CORS headers middleware, and the result was the same.
  • Very rarely the time to load the body was 0ms. This makes me think it's a caching issue.
@wolfenrain
Copy link
Member

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

@Schamppu
Copy link
Author

Shelf version from pubspec.lock is 1.4.2

@tomarra tomarra moved this from Needs Triage to In Progress in VGV Open Source 🦄 🧙🌟 Nov 19, 2024
@tomarra tomarra added the p2 Important issues not at the top of the work list label Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2 Important issues not at the top of the work list performance Changes that improve performance
Projects
Status: Todo
Development

No branches or pull requests

3 participants