Skip to content

more optimizations - #121

Merged
jeapostrophe merged 16 commits into
racket:masterfrom
Bogdanp:more-opts
Jun 28, 2022
Merged

jeapostrophe merged 16 commits into
racket:masterfrom
Bogdanp:more-opts

Conversation

@Bogdanp

@Bogdanp Bogdanp commented Jun 15, 2022 •

Copy link
Copy Markdown
Contributor

I'm opening this as a draft in case folks want to weigh in on the changes. I've still got a few changes I want to make over the next couple weeks so this isn't quite ready yet. This PR includes the commit from #120, but that can be merged independently so it doesn't block #119.

The PR may be easier to read if you read the commits independently. I forgot @samth also worked on removing run-server until I looked at the list of open PRs just now, but I took that a little further by also improving the server loop in a couple ways so I think this PR supersedes #106.

Tested against the /plaintext endpoint of this app. Master branch:

Requests      [total, rate, throughput]         30000, 1000.04, 1000.03
Duration      [total, attack, wait]             29.999s, 29.999s, 286.417µs
Latencies     [min, mean, 50, 90, 95, 99, max]  229.205µs, 318.265µs, 293.078µs, 367.174µs, 393.675µs, 606.119µs, 14.015ms
Bytes In      [total, mean]                     390000, 13.00
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           100.00%
Status Codes  [code:count]                      200:30000  
Error Set:

(note that this app uses its own dispatcher, not dispatch/servlet, which is why these numbers are better than the baseline numbers in commit 4506e72 -- dispatch/servlet needs to do a lot of work that applications that don't use continuations don't benefit from)

This branch:

Requests      [total, rate, throughput]         30000, 1000.04, 1000.03
Duration      [total, attack, wait]             29.999s, 29.999s, 262.316µs
Latencies     [min, mean, 50, 90, 95, 99, max]  197.619µs, 253.639µs, 248.664µs, 280.93µs, 297.689µs, 344.797µs, 929.773µs
Bytes In      [total, mean]                     390000, 13.00
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           100.00%
Status Codes  [code:count]                      200:30000
Error Set:

For comparison, here's how Node 17 running express does on the same benchmark on my machine:

Requests      [total, rate, throughput]         30000, 1000.03, 1000.03
Duration      [total, attack, wait]             29.999s, 29.999s, 182.925µs
Latencies     [min, mean, 50, 90, 95, 99, max]  136.996µs, 190.037µs, 185.03µs, 208.014µs, 219.565µs, 303.124µs, 1.406ms
Bytes In      [total, mean]                     390000, 13.00
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           100.00%
Status Codes  [code:count]                      200:30000
Error Set:

Bogdanp added 5 commits June 12, 2022 20:27
Baseline @ 2.5K requests/second for 2 minutes:

    Requests      [total, rate, throughput]         300000, 2500.01, 2500.00
    Duration      [total, attack, wait]             2m0s, 2m0s, 633.693µs
    Latencies     [min, mean, 50, 90, 95, 99, max]  228.976µs, 2.808ms, 360.599µs, 808.569µs, 1.1ms, 99.642ms, 340.486ms
    Bytes In      [total, mean]                     1500000, 5.00
    Bytes Out     [total, mean]                     0, 0.00
    Success       [ratio]                           100.00%
    Status Codes  [code:count]                      200:300000
    Error Set:

After this change:

    Requests      [total, rate, throughput]         300000, 2500.01, 2500.00
    Duration      [total, attack, wait]             2m0s, 2m0s, 660.098µs
    Latencies     [min, mean, 50, 90, 95, 99, max]  221.044µs, 666.861µs, 328.204µs, 635.192µs, 794.381µs, 6.891ms, 132.629ms
    Bytes In      [total, mean]                     1500000, 5.00
    Bytes Out     [total, mean]                     0, 0.00
    Success       [ratio]                           100.00%
    Status Codes  [code:count]                      200:300000
    Error Set:

Memory use is the same between this and the master branch on my
machine, but I suspect my machine's IO is just too fast.  If I were to
try this on a cloud machine, I suspect there would be a significant
improvement with this change.
This preserves the behavior of run-server except for timeouts.  The
mzlib server spawns two threads for every connection: one to handle
the connection, and another to break the first thread on timeout.  The
web server never used the timeout functionality, so the second thread
would be spawned and forced to wait until the handling thread
finished, then close the connection custodian.
The behavior remains mostly the same, except now we don't need to set
up a new handler on every request.
The non-private export of `make-read-request' has a contract and the
`dispatch-server^' signature also installs one.  This avoids taking
that hit twice.


(define-safety-limits/private-submodule
max-concurrent positive-count/c 1000 #:unlimited +inf.0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This limit may be too conservative, so it might be worth raising. It's a hard balance, though:

  • if there's no limit, then it's easy to DoS the server,
  • if the default limit is too low, we may be limiting performance on very fast servers
  • if the default limit is too high, slow servers may be open to DoS

IME, once you have several thousands of threads running, performance tends to degrade so 1000 feels right. Arguably, people doing very high performance stuff w/ the web server should be aware of and tweaking safety-limits anyway. I'll have to take a look at what other servers' defaults are.

cc @LiberalArtist

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end, I bumped this to 10k. Most of the servers I looked at had no default limit (but they did have ways to enforce a limit), except for gunicorn which has a limit of 1k by default.

@samth

samth commented Jun 15, 2022

Copy link
Copy Markdown
Member

Does removing run-server mean it no longer depends on compatibility-lib?

@jeapostrophe jeapostrophe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awesome to me

@Bogdanp

Bogdanp commented Jun 16, 2022

Copy link
Copy Markdown
Contributor Author

Does removing run-server mean it no longer depends on compatibility-lib?

Yup, looks like it does.

@LiberalArtist

Copy link
Copy Markdown
Contributor

Re replacing run-server:

It's a pathological case, but mechanisms like (kill-thread (current-thread)) and (custodian-shutdown-all (current-custodian)) escape without running winders, e.g.:

#lang racket
(dynamic-wind
 void
 (λ ()
   (kill-thread (current-thread)))
 (λ ()
   (println 'this-never-prints)))

I think run-server's cleanup thread handles this by syncing on the handler thread (without the vestigial timeout, it might be more efficient to go via thread-dead-evt), but I think this replacement would not call custodian-shutdown-all on the custodian that owns the ports.

This sort of subtlety is why I suggested in #106 (comment):

If it seems better to copy–paste for now, I'd add a comment here and maybe even in mzlib/thread in the hope of prompting someone to tackle the refactoring in the future.

More broadly, this is reminding me of mailing-list discussion in https://groups.google.com/g/racket-users/c/AAeEp_llxaY/m/uRviksPGAgAJ and https://groups.google.com/g/racket-dev/c/PyetqHSjtAA/m/slBdazdqAwAJ about the subtleties of delimited control and the Concurrent ML subsystem and the limitations of finalization with dynamic-wind. (I'd recently recommended these threads to people adapting the Shepherd (a daemon-managing daemon) to use Guile's Fibers.) In particular, @lexi-lambda pointed out the technical report “Algebraic Effect
Handlers with Resources and Deep Finalization”
(which I want to finish reading!), which says in §8.7 "Dynamic Wind" (internal citations omitted):

The Scheme language always supported delimited continuations and
they have also struggled with initialization- and finalization
for continuations that resumed more than once. The
unwind-protect in Scheme is like a finally clause, while
dynamic-wind is like initially/finally with a pre- and
postlude. Sitaram describes how the standard dynamic-wind is not
good enough in general:

While this may seem like a natural extension of the first-order
unwind-protect to a higher-order control scenario, it does not
tackle the pragmatic need that unwind-protect addresses,
namely, the need to ensure that a kind of “clean-up” happens only
for those jumps that significantly exit the block, and not for
those that are a minor excursion. The crux is identifying which
of these two categories a jump falls into.

Interestingly, this is exactly what is addressed by algebraic
effects where “significant exit”s are operations that do not
resume, while “minor excursions” are regular operations that
resume with a result.

I'll try to take a look at other aspects of this PR later.

@Bogdanp

Bogdanp commented Jun 16, 2022

Copy link
Copy Markdown
Contributor Author

The (custodian-shutdown-all (current-custodian)) case is covered by the custodian that wraps the handler, but you're right about the (kill-thread (current-thread)) case. I don't think the server claims that handler threads are kill-safe, but it's easy enough to bring back the cleanup thread so I'll just do that. An alternative I've considered is keeping track of the handler threads in a list inside the loop and syncing on them to remove them from the set when done. That would get rid of the need for both dynamic-wind and a cleanup thread, but would result in more work per loop iteration so it's probably not worth it.

I think this replacement would not call custodian-shutdown-all on the custodian that owns the ports.

I'm not sure I understand what you mean here. The custodian that gets shut down is the one that wraps both the thread and the TCP ports.

@Bogdanp

Bogdanp commented Jun 22, 2022 •

Copy link
Copy Markdown
Contributor Author

Here's how we're doing so far:

For comparison, here's the plaintext benchmark on express.js on the same machine. The test machine in this case is a 24-core AMD server.

@jeapostrophe

Copy link
Copy Markdown
Contributor

I think we should merge and brag ;)

@Bogdanp

Bogdanp commented Jun 25, 2022

Copy link
Copy Markdown
Contributor Author

I'll wrap this up tomorrow morning and then we can merge. There are more improvements that could be made, but we can leave those for another time so this doesn't get too large and because I'm not sure when I'll have time to do them.

@Bogdanp

Bogdanp commented Jun 26, 2022

Copy link
Copy Markdown
Contributor Author

OK, I think this is good to go now.

@Bogdanp
Bogdanp marked this pull request as ready for review June 26, 2022 11:48
@jeapostrophe
jeapostrophe merged commit 39f49da into racket:master Jun 28, 2022
@Bogdanp
Bogdanp deleted the more-opts branch June 28, 2022 14:42
@Bogdanp Bogdanp mentioned this pull request Jun 28, 2022
Bogdanp added a commit to Bogdanp/FrameworkBenchmarks that referenced this pull request Aug 9, 2022
Also tweak parameters and adjust `place-tcp-listener` implementation
for recent web-server changes[1].

[1]: racket/web-server#121
NateBrady23 pushed a commit to TechEmpower/FrameworkBenchmarks that referenced this pull request Aug 15, 2022
Also tweak parameters and adjust `place-tcp-listener` implementation
for recent web-server changes[1].

[1]: racket/web-server#121
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.

4 participants