forked from apple/swift-nio-http2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerOnly10KRequestsBenchmark.swift
More file actions
209 lines (165 loc) · 7.16 KB
/
ServerOnly10KRequestsBenchmark.swift
File metadata and controls
209 lines (165 loc) · 7.16 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
import NIOEmbedded
import NIOHPACK
import NIOHTTP2
final class ServerOnly10KRequestsBenchmark: Benchmark {
private let concurrentStreams: Int
var channel: EmbeddedChannel?
// This offset is preserved across runs.
var streamID = HTTP2StreamID(1)
// A manually constructed data frame.
private var dataFrame: ByteBuffer = {
var buffer = ByteBuffer(repeating: 0xff, count: 1024 + 9)
// UInt24 length, is 1024 bytes.
buffer.setInteger(UInt8(0), at: buffer.readerIndex)
buffer.setInteger(UInt16(1024), at: buffer.readerIndex + 1)
// Type
buffer.setInteger(UInt8(0x00), at: buffer.readerIndex + 3)
// Flags, turn on end-stream.
buffer.setInteger(UInt8(0x01), at: buffer.readerIndex + 4)
// 4 byte stream identifier, set to zero for now as we update it later.
buffer.setInteger(UInt32(0), at: buffer.readerIndex + 5)
return buffer
}()
// A manually constructed headers frame.
private var headersFrame: ByteBuffer = {
var headers = HPACKHeaders()
headers.add(name: ":method", value: "GET", indexing: .indexable)
headers.add(name: ":authority", value: "localhost", indexing: .nonIndexable)
headers.add(name: ":path", value: "/", indexing: .indexable)
headers.add(name: ":scheme", value: "https", indexing: .indexable)
headers.add(
name: "user-agent",
value:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
indexing: .nonIndexable
)
headers.add(name: "accept-encoding", value: "gzip, deflate", indexing: .indexable)
var hpackEncoder = HPACKEncoder(allocator: .init())
var buffer = ByteBuffer()
buffer.writeRepeatingByte(0, count: 9)
buffer.moveReaderIndex(forwardBy: 9)
try! hpackEncoder.encode(headers: headers, to: &buffer)
let encodedLength = buffer.readableBytes
buffer.moveReaderIndex(to: buffer.readerIndex - 9)
// UInt24 length
buffer.setInteger(UInt8(0), at: buffer.readerIndex)
buffer.setInteger(UInt16(encodedLength), at: buffer.readerIndex + 1)
// Type
buffer.setInteger(UInt8(0x01), at: buffer.readerIndex + 3)
// Flags, turn on END_HEADERs.
buffer.setInteger(UInt8(0x04), at: buffer.readerIndex + 4)
// 4 byte stream identifier, set to zero for now as we update it later.
buffer.setInteger(UInt32(0), at: buffer.readerIndex + 5)
return buffer
}()
private let emptySettings: ByteBuffer = {
var buffer = ByteBuffer()
buffer.reserveCapacity(9)
// UInt24 length, is 0 bytes.
buffer.writeInteger(UInt8(0))
buffer.writeInteger(UInt16(0))
// Type
buffer.writeInteger(UInt8(0x04))
// Flags, none.
buffer.writeInteger(UInt8(0x00))
// 4 byte stream identifier, set to zero.
buffer.writeInteger(UInt32(0))
return buffer
}()
private var settingsACK: ByteBuffer {
// Copy the empty SETTINGS and add the ACK flag
var settingsCopy = self.emptySettings
settingsCopy.setInteger(UInt8(0x01), at: settingsCopy.readerIndex + 4)
return settingsCopy
}
init(concurrentStreams: Int) {
self.concurrentStreams = concurrentStreams
}
func setUp() throws {
let channel = EmbeddedChannel()
try channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
streamChannel.eventLoop.makeCompletedFuture {
try streamChannel.pipeline.syncOperations.addHandler(TestServer())
}
}.map { _ in }.wait()
try channel.connect(to: .init(unixDomainSocketPath: "/fake"), promise: nil)
// Gotta do the handshake here.
var initialBytes = ByteBuffer(string: "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
initialBytes.writeImmutableBuffer(self.emptySettings)
initialBytes.writeImmutableBuffer(self.settingsACK)
try channel.writeInbound(initialBytes)
while try channel.readOutbound(as: ByteBuffer.self) != nil {}
self.channel = channel
}
func tearDown() {
_ = try! self.channel!.finish()
self.channel = nil
}
func run() throws -> Int {
var bodyByteCount = 0
var completedIterations = 0
while completedIterations < 10_000 {
bodyByteCount &+= try self.sendInterleavedRequests(self.concurrentStreams)
completedIterations += self.concurrentStreams
}
return bodyByteCount
}
private func sendInterleavedRequests(_ interleavedRequests: Int) throws -> Int {
var streamID = self.streamID
for _ in 0..<interleavedRequests {
self.headersFrame.setInteger(UInt32(Int32(streamID)), at: self.headersFrame.readerIndex + 5)
try self.channel!.writeInbound(self.headersFrame)
streamID = streamID.advanced(by: 2)
}
streamID = self.streamID
for _ in 0..<interleavedRequests {
self.dataFrame.setInteger(UInt32(Int32(streamID)), at: self.dataFrame.readerIndex + 5)
try self.channel!.writeInbound(self.dataFrame)
streamID = streamID.advanced(by: 2)
}
self.channel!.embeddedEventLoop.run()
self.streamID = streamID
var count = 0
while let data = try self.channel!.readOutbound(as: ByteBuffer.self) {
count &+= data.readableBytes
self.channel!.embeddedEventLoop.run()
}
return count
}
}
private class TestServer: ChannelInboundHandler {
public typealias InboundIn = HTTP2Frame.FramePayload
public typealias OutboundOut = HTTP2Frame.FramePayload
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let payload = self.unwrapInboundIn(data)
switch payload {
case .headers(let headers) where headers.endStream:
self.sendResponse(context: context)
case .data(let data) where data.endStream:
self.sendResponse(context: context)
default:
()
}
}
private func sendResponse(context: ChannelHandlerContext) {
let responseHeaders = HPACKHeaders([(":status", "200"), ("server", "test-benchmark")])
let response = HTTP2Frame.FramePayload.headers(.init(headers: responseHeaders, endStream: false))
let responseData = HTTP2Frame.FramePayload.data(.init(data: .byteBuffer(ByteBuffer()), endStream: true))
context.write(self.wrapOutboundOut(response), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(responseData), promise: nil)
}
}