Skip to content

perf: Skip session work that is only needed when a session is logged - #6005

Open
abdelaziz-mahdy wants to merge 1 commit into
serverpod:mainfrom
abdelaziz-mahdy:perf/lazy-session-work
Open

abdelaziz-mahdy wants to merge 1 commit into
serverpod:mainfrom
abdelaziz-mahdy:perf/lazy-session-work

Conversation

@abdelaziz-mahdy

Copy link
Copy Markdown
Contributor

Every method call did this, even though nothing used the result:

  • generated a secure-random session id (CryptoRNG);
  • built a session-log open event (label string, sessionId.hashCode);
  • when a session was emitted, built VM-service events (maps, ISO timestamps,
    stack trace strings), which postEvent drops when there's no VM service,
    as in production.

The last one matters under overload. Once latency passes the 1 s
slow-session threshold, every session and query is logged. In an overload
profile, building these events took ~16% of CPU, making the overload worse.

Changes:

  • Session.sessionId is generated on first access;
  • SessionLogManager builds its open event on first use;
  • VmServiceSessionLogWriter returns early while
    developer.extensionStreamHasListener is false.

Part of #6002

Measurements

AOT, macOS arm64, retired CPU instructions per request, mean of 3 shuffled
runs:

Request main this PR Change
no database 402k 386k -4.1%
findById 975k 967k -0.8%
find, 20 rows 1,667k 1,660k -0.4%
insertRow 1,008k 999k -0.9%

Bold: every run below every run of main. The DB requests change by about
as much in absolute terms; it's a smaller share of a larger request.

Tests

Full Serverpod test run on the combined branch (the PR branches are subsets): unit tests of serverpod, serverpod_serialization, serverpod_shared and serverpod_database pass; integration tests 1,683 (serial) + 529 (util/run_tests_integration_concurrently) pass; dart analyze is clean on this branch.

How it was measured: the app is compiled AOT and run natively; for each request type, k6 sends 15 s at 1,000 req/s, and the server process's retired instructions (proc_pid_rusage(RUSAGE_INFO_V4)) before and after are divided by the number of requests. Builds of main and this PR were run alternately in shuffled order. I can share the scripts.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

Breaking changes

None. sessionId is still a non-null UuidValue, now created when first
read.

A method call created a secure-random session id, a session log open
event and, when a session was emitted, VM service log events, even when
nothing used them:

- Session.sessionId is generated on first access.
- SessionLogManager builds its open event on first use.
- VmServiceSessionLogWriter returns early while nothing listens to the
  extension stream, as in production, instead of building events that
  postEvent drops. Under overload every session is slow and gets logged,
  so this work grew exactly when the server had no time for it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant