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

Allow use of middleware result builder in Router #512

Merged
merged 1 commit into from
Jul 15, 2024

Conversation

adam-fowler
Copy link
Member

Middleware are stored as Middleware<Request, Response, Context> instead of RouterMiddleware. Once middleware result builder is moved to separate package this allows for outside libraries to create Middleware that can be used by Hummingbird without knowing about RouterMiddleware (an HB type). RouterMiddleware is just a helper protocol to avoid having to set Input and Output in hummingbird specific middleware.

The work above has allowed me to move the middleware stack to Hummingbird so users can take advantage of it in the Hummingbird standard router. eg

router.add {
    LogRequestsMiddleware()
    FileMiddleware()
}

Middleware are stored as `Middleware<Request, Response, Context>` instead of `RouterMiddleware`. Once middleware result builder is moved to separate package this allows for outside libraries to create Middleware that can be used by Hummingbird without knowing about `RouterMiddleware` (an HB type).

I moved the middleware stack to Hummingbird so users can take advantage of it in the Hummingbird standard router. eg
```swift
router.add{
    LogRequestsMiddleware()
    FileMiddleware()
}
```
@adam-fowler
Copy link
Member Author

Here are some performance numbers with applying four middleware (that do nothing) to a router with the standard router.add(middleware:) and using the result builder

Middleware (router.add(middleware:))
╒═══════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│ Metric                    │      p0 │     p25 │     p50 │     p75 │     p90 │     p99 │    p100 │ Samples │
╞═══════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│ Malloc (total) *          │     491 │    1455 │    2085 │    2775 │    3465 │    4531 │    5466 │    1355 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Throughput (# / s) (#)    │    2189 │    2049 │    2014 │    1970 │    1852 │    1516 │    1291 │    1355 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (total CPU) (μs) *   │     620 │     667 │     680 │     696 │     741 │     955 │    1176 │    1355 │
╘═══════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛

Middleware (result builder)
╒═══════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│ Metric                    │      p0 │     p25 │     p50 │     p75 │     p90 │     p99 │    p100 │ Samples │
╞═══════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│ Malloc (total) *          │     146 │    1172 │    1757 │    2537 │    3353 │    4535 │    6053 │    1421 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Throughput (# / s) (#)    │    2288 │    2193 │    2171 │    2115 │    1948 │    1616 │    1393 │    1421 │
├───────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (total CPU) (μs) *   │     599 │     634 │     642 │     656 │     719 │     841 │     958 │    1421 │
╘═══════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛

@adam-fowler adam-fowler merged commit a0624b7 into main Jul 15, 2024
4 of 5 checks passed
@adam-fowler adam-fowler deleted the generalize-middleware-req branch July 15, 2024 10:20
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