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

hyper: simplify handlers #9728

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

hyper: simplify handlers #9728

wants to merge 3 commits into from

Conversation

joshka
Copy link
Contributor

@joshka joshka commented Mar 25, 2025

  • move the server header to the router and add it to all responses.
  • move the response boxing to the router simplifying the return types of
    each handler.

hyperium/http-body#150 will simplify the router
code even futher to the following:

let mut response = match request.uri().path() {
     "/ping" => ping()?.box_body(),
     "/json" => json::get()?.box_body(),
     "/db" => single_query::get().await?.box_body(),
     "/queries" => multiple_queries::get(request.uri().query()).await?.box_body(),
     "/fortunes" => fortunes::get().await?.box_body(),
     "/plaintext" => plaintext::get()?.box_body(),
     _ => not_found_error()?.box_body(),
};

Note: includes commits from #9727. Relevant commit that's only this change is a50128f

joshka added 3 commits March 24, 2025 15:23
… layers

This commit makes it possible to turn off the removal of intermediate
docker layers when building the tfb containers by adding a `--force-rm`
flag to the tfb script. This is useful in situations where you want to
inspect the intermediate layers for debugging purposes, or for caching
builds of dependencies as a docker layer to speed up the build process.

Note that the default behavior is to remove the intermediate layers to
avoid filling up the disk with unused layers on the citrine server.

Fixes: TechEmpower#9718
This change reduces the time it takes to build the hyper Docker image by
caching the dependency builds. This is done by building a dummy binary
before copying the source code into the image.
- move the server header to the router and add it to all responses.
- move the response boxing to the router simplifying the return types of
  each handler.

hyperium/http-body#150 will simplify the router
code even futher to the following:

```rust
let mut response = match request.uri().path() {
     "/ping" => ping()?.box_body(),
     "/json" => json::get()?.box_body(),
     "/db" => single_query::get().await?.box_body(),
     "/queries" => multiple_queries::get(request.uri().query()).await?.box_body(),
     "/fortunes" => fortunes::get().await?.box_body(),
     "/plaintext" => plaintext::get()?.box_body(),
     _ => not_found_error()?.box_body(),
};
```
Copy link
Contributor

The following frameworks were updated, pinging maintainers:
hyper: @polachok, @seanmonstar, @steveklabnik

@joshka
Copy link
Contributor Author

joshka commented Mar 27, 2025

Local benchmark results (Macbook Pro M2 Max)

Before the recent hyper changes:

hyper (JSON + Plaintext):

https://www.techempower.com/benchmarks/#section=test&shareid=69e73a28-4d6c-4aa8-821b-27eb62b7e616&hw=ph&test=json

JSON responses per second, (unspecified, hostname = 48c41e9f7187)

Framework 16 32 64 128 256 512 Best Errors Cls Lng Plt FE Aos IA
hyper 196,760 508,649 624,762 729,611 727,629 811,146 811,146 0 Mcr rs rs hyp lin rea

Plaintext responses per second, (unspecified, hostname = 48c41e9f7187)

Framework 256 1,024 4,096 16,384 Best Errors Cls Lng Plt FE Aos IA
hyper 5,481,306 5,887,571 5,447,194 4,462,003 5,887,571 0 Mcr rs rs hyp lin rea

Fortunes responses per second, (unspecified, hostname = e0169ecd4a21)

Framework 16 32 64 128 256 512 Best Errors Cls Lng Plt FE Aos DB Dos Orm IA
hyper 80,846 177,855 129,424 141,055 147,723 149,434 177,855 0 Mcr rs rs hyp lin Pg lin raw rea

After this change:

hyper (current-thread runtime):

https://www.techempower.com/benchmarks/#section=test&shareid=0be4944f-3746-4ff5-a6d5-6537c33df272&hw=ph&test=json

JSON responses per second, (unspecified, hostname = b7f8f718c8cc)

Framework 16 32 64 128 256 512 Best Errors Cls Lng Plt FE Aos IA
hyper (current-thread runtime) 207,752 470,367 598,843 692,511 821,558 872,231 872,231 0 Mcr rs rs hyp lin rea

Database-access responses per second, single query, (unspecified, hostname = b7f8f718c8cc)

Framework 16 32 64 128 256 512 Best Errors Cls Lng Plt FE Aos DB Dos Orm IA
hyper (current-thread runtime) 49,527 82,542 133,047 119,479 118,664 105,070 133,047 0 Mcr rs rs hyp lin Pg lin raw rea

Responses per second, multiple queries, (unspecified, hostname = b7f8f718c8cc)

Framework 1 5 10 15 20 Errors Cls Lng Plt FE Aos DB Dos Orm IA
hyper (current-thread runtime) 115,887 30,055 15,253 10,466 7,838 0 Mcr rs rs hyp lin Pg lin raw rea

Fortunes responses per second, (unspecified, hostname = b7f8f718c8cc)

Framework 16 32 64 128 256 512 Best Errors Cls Lng Plt FE Aos DB Dos Orm IA
hyper (current-thread runtime) 45,863 80,784 122,626 113,597 111,767 108,218 122,626 0 Mcr rs rs hyp lin Pg lin raw rea

Plaintext responses per second, (unspecified, hostname = b7f8f718c8cc)

Framework 256 1,024 4,096 16,384 Best Errors Cls Lng Plt FE Aos IA
hyper (current-thread runtime) 4,498,684 5,150,947 5,067,568 4,265,876 5,150,947 0 Mcr rs rs hyp lin rea

hyper (multi-thread runtime):

https://www.techempower.com/benchmarks/#section=test&shareid=e44af5ec-34be-4542-bb04-f970a375a62e&hw=ph&test=json

JSON responses per second, (unspecified, hostname = cda50185af70)

Framework 16 32 64 128 256 512 Best Errors Cls Lng Plt FE Aos IA
hyper (multi-thread runtime) 136,775 205,623 280,521 351,340 517,568 681,464 681,464 0 Mcr rs rs hyp lin rea

Database-access responses per second, single query, (unspecified, hostname = cda50185af70)

Framework 16 32 64 128 256 512 Best Errors Cls Lng Plt FE Aos DB Dos Orm IA
hyper (multi-thread runtime) 60,558 80,468 114,034 125,198 123,046 123,854 125,198 0 Mcr rs rs hyp lin Pg lin raw rea

Responses per second, multiple queries, (unspecified, hostname = cda50185af70)

Framework 1 5 10 15 20 Errors Cls Lng Plt FE Aos DB Dos Orm IA
hyper (multi-thread runtime) 124,719 31,089 16,536 11,172 8,463 0 Mcr rs rs hyp lin Pg lin raw rea

Fortunes responses per second, (unspecified, hostname = cda50185af70)

Framework 16 32 64 128 256 512 Best Errors Cls Lng Plt FE Aos DB Dos Orm IA
hyper (multi-thread runtime) 57,991 79,421 110,869 120,830 121,191 119,472 121,191 0 Mcr rs rs hyp lin Pg lin raw rea

Plaintext responses per second, (unspecified, hostname = cda50185af70)

Framework 256 1,024 4,096 16,384 Best Errors Cls Lng Plt FE Aos IA
hyper (multi-thread runtime) 4,216,747 4,716,550 4,561,864 4,090,954 4,716,550 0 Mcr rs rs hyp lin rea

Copy link
Contributor

@steveklabnik steveklabnik left a comment

Choose a reason for hiding this comment

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

Seems good.

@joshka
Copy link
Contributor Author

joshka commented Mar 27, 2025

There's definitely a performance degradation in the results, and I haven't investigated why yet. The goal of the changes was to make the source a bit more maintainable, and then work on improving the perf aspects on top of that in ways where the code which impacts performance can be a bit more obvious and measured (with test variants). Because hyper is also the base for many of the rust frameworks, having numbers for each of the tests allows this to be used as the framework overhead in those tests (using the "versus" field in the configs", which would help show which parts of the perf are just pure hyper and which are from framework overhead (or in some cases improvements).

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.

2 participants