/
HTTP1ChannelBenchmarks.swift
110 lines (105 loc) · 4.04 KB
/
HTTP1ChannelBenchmarks.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Benchmark
import HTTPTypes
import Hummingbird
import HummingbirdCore
import Logging
import NIOCore
import NIOEmbedded
import NIOHTTPTypes
extension Benchmark {
@discardableResult
convenience init?(
name: String,
configuration: Benchmark.Configuration = Benchmark.defaultConfiguration,
write: @escaping @Sendable (Benchmark, NIOAsyncTestingChannel) async throws -> Void,
responder: @escaping @Sendable (Request, Channel) async throws -> Response
) {
let http1 = HTTP1Channel(responder: responder)
let channel = NIOAsyncTestingChannel()
var task: Task<Void, Never>!
self.init(name, configuration: configuration) { benchmark in
for _ in benchmark.scaledIterations {
for _ in 0..<25 {
try await write(benchmark, channel)
await channel.testingEventLoop.run()
// receive response
while true {
let part = try await channel.waitForOutboundWrite(as: HTTPResponsePart.self)
if case .end(nil) = part {
break
}
}
}
}
} setup: {
let asyncChannel = try await channel.eventLoop.submit {
try HTTP1Channel.Value(wrappingChannelSynchronously: channel)
}.get()
task = Task {
await http1.handle(value: asyncChannel, logger: Logger(label: "Testing"))
}
} teardown: {
try await channel.close()
_ = await task.result
}
}
}
let benchmarks = {
Benchmark.defaultConfiguration = .init(
metrics: [
.cpuTotal,
.throughput,
.mallocCountTotal,
],
warmupIterations: 10
)
let buffer = ByteBufferAllocator().buffer(repeating: 0xFF, count: 10000)
Benchmark(
name: "HTTP1:GET",
configuration: .init(warmupIterations: 10)
) { _, channel in
let head = HTTPRequest(method: .get, scheme: "http", authority: "localhost", path: "/")
try await channel.writeInbound(HTTPRequestPart.head(head))
try await channel.writeInbound(HTTPRequestPart.end(nil))
} responder: { _, _ in
return .init(status: .ok, body: .init(byteBuffer: buffer))
}
Benchmark(
name: "HTTP1:PUT",
configuration: .init(warmupIterations: 10)
) { _, channel in
let head = HTTPRequest(method: .put, scheme: "http", authority: "localhost", path: "/")
try await channel.writeInbound(HTTPRequestPart.head(head))
try await channel.writeInbound(HTTPRequestPart.body(buffer))
try await channel.writeInbound(HTTPRequestPart.body(buffer))
try await channel.writeInbound(HTTPRequestPart.end(nil))
} responder: { _, _ in
return .init(status: .ok, body: .init(byteBuffer: buffer))
}
Benchmark(
name: "HTTP1:Echo",
configuration: .init(warmupIterations: 10)
) { _, channel in
let head = HTTPRequest(method: .post, scheme: "http", authority: "localhost", path: "/")
try await channel.writeInbound(HTTPRequestPart.head(head))
try await channel.writeInbound(HTTPRequestPart.body(buffer))
try await channel.writeInbound(HTTPRequestPart.body(buffer))
try await channel.writeInbound(HTTPRequestPart.end(nil))
} responder: { request, _ in
let buffer = try await request.body.collate(maxSize: .max)
return .init(status: .ok, body: .init(byteBuffer: buffer))
}
}