Skip to content

Commit

Permalink
Use collate(maxSize:) instead of collect(upTo:)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Jan 5, 2024
1 parent 8c38c5e commit e5472cf
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
26 changes: 13 additions & 13 deletions Benchmarks/Benchmarks/HTTP1/HTTP1ChannelBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
//
//===----------------------------------------------------------------------===//

import Benchmark
import HTTPTypes
import NIOHTTPTypes
import Hummingbird
import HummingbirdCore
import Logging
import NIOCore
import NIOEmbedded
import Benchmark
import NIOHTTPTypes

extension Benchmark {
@discardableResult
convenience init?(
name: String,
configuration: Benchmark.Configuration = Benchmark.defaultConfiguration,
configuration: Benchmark.Configuration = Benchmark.defaultConfiguration,
write: @escaping @Sendable (Benchmark, NIOAsyncTestingChannel) async throws -> Void,
responder: @escaping @Sendable (HBRequest, Channel) async throws -> HBResponse
) {
) {
let http1 = HTTP1Channel(responder: responder)
let channel = NIOAsyncTestingChannel()
var task: Task<Void, Never>!
Expand Down Expand Up @@ -69,42 +69,42 @@ let benchmarks = {
],
warmupIterations: 10
)
let buffer = ByteBufferAllocator().buffer(repeating: 0xff, count: 10000)
let buffer = ByteBufferAllocator().buffer(repeating: 0xFF, count: 10000)
Benchmark(
name: "HTTP1:GET",
configuration: .init(warmupIterations: 10)
) { benchmark, channel in
) { _, 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: { request, channel in
} responder: { _, _ in
return .init(status: .ok, body: .init(byteBuffer: buffer))
}

Benchmark(
name: "HTTP1:PUT",
configuration: .init(warmupIterations: 10)
) { benchmark, channel in
) { _, 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: { request, channel in
} responder: { _, _ in
return .init(status: .ok, body: .init(byteBuffer: buffer))
}

Benchmark(
name: "HTTP1:Echo",
configuration: .init(warmupIterations: 10)
) { benchmark, channel in
) { _, 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, channel in
let buffer = try await request.body.collect(upTo: .max)
} responder: { request, _ in
let buffer = try await request.body.collate(maxSize: .max)
return .init(status: .ok, body: .init(byteBuffer: buffer))
}
}
}
2 changes: 1 addition & 1 deletion Benchmarks/Benchmarks/Router/RouterBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func routerBenchmarks() {
await bodyStream.send(buffer)
} setupRouter: { router in
router.put { request, _ in
let body = try await request.body.collect(upTo: .max)
let body = try await request.body.collate(maxSize: .max)
return body.readableBytes.description
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension JSONDecoder: HBRequestDecoder {
/// - type: Type to decode
/// - request: Request to decode from
public func decode<T: Decodable>(_ type: T.Type, from request: HBRequest, context: some HBBaseRequestContext) async throws -> T {
let buffer = try await request.body.collect(upTo: context.maxUploadSize)
let buffer = try await request.body.collate(maxSize: context.maxUploadSize)
return try self.decode(T.self, from: buffer)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension URLEncodedFormDecoder: HBRequestDecoder {
/// - type: Type to decode
/// - request: Request to decode from
public func decode<T: Decodable>(_ type: T.Type, from request: HBRequest, context: some HBBaseRequestContext) async throws -> T {
let buffer = try await request.body.collect(upTo: context.maxUploadSize)
let buffer = try await request.body.collate(maxSize: context.maxUploadSize)
let string = String(buffer: buffer)
return try self.decode(T.self, from: string)
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/HummingbirdCoreTests/CoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class HummingBirdCoreTests: XCTestCase {
func testConsumeBody() async throws {
try await testServer(
responder: { request, _ in
let buffer = try await request.body.collect(upTo: .max)
let buffer = try await request.body.collate(maxSize: .max)
return HBResponse(status: .ok, body: .init(byteBuffer: buffer))
},
configuration: .init(address: .hostname(port: 0)),
Expand Down Expand Up @@ -198,7 +198,7 @@ class HummingBirdCoreTests: XCTestCase {
}
try await testServer(
responder: { request, _ in
_ = try await request.body.collect(upTo: .max)
_ = try await request.body.collate(maxSize: .max)
return HBResponse(status: .ok)
},
httpChannelSetup: .http1(additionalChannelHandlers: [CreateErrorHandler()]),
Expand Down Expand Up @@ -264,7 +264,7 @@ class HummingBirdCoreTests: XCTestCase {
}
try await testServer(
responder: { request, _ in
_ = try await request.body.collect(upTo: .max)
_ = try await request.body.collate(maxSize: .max)
return .init(status: .ok)
},
httpChannelSetup: .http1(additionalChannelHandlers: [HTTPServerIncompleteRequest(), IdleStateHandler(readTimeout: .seconds(1))]),
Expand All @@ -287,7 +287,7 @@ class HummingBirdCoreTests: XCTestCase {
func testWriteIdleTimeout() async throws {
try await testServer(
responder: { request, _ in
_ = try await request.body.collect(upTo: .max)
_ = try await request.body.collate(maxSize: .max)
return .init(status: .ok)
},
httpChannelSetup: .http1(additionalChannelHandlers: [IdleStateHandler(writeTimeout: .seconds(1))]),
Expand Down
4 changes: 2 additions & 2 deletions Tests/HummingbirdTests/ApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ final class ApplicationTests: XCTestCase {
router
.group("/echo-body")
.post { request, _ -> HBResponse in
let buffer = try await request.body.collect(upTo: .max)
let buffer = try await request.body.collate(maxSize: .max)
return .init(status: .ok, headers: [:], body: .init(byteBuffer: buffer))
}
let app = HBApplication(responder: router.buildResponder())
Expand Down Expand Up @@ -301,7 +301,7 @@ final class ApplicationTests: XCTestCase {
router
.group("/echo-body")
.post { request, _ -> ByteBuffer? in
let buffer = try await request.body.collect(upTo: .max)
let buffer = try await request.body.collate(maxSize: .max)
return buffer.readableBytes > 0 ? buffer : nil
}
let app = HBApplication(responder: router.buildResponder())
Expand Down
10 changes: 5 additions & 5 deletions Tests/HummingbirdTests/PersistTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ final class PersistTests: XCTestCase {
let persist = HBMemoryPersistDriver()

router.put("/persist/:tag") { request, context -> HTTPResponse.Status in
let buffer = try await request.body.collect(upTo: .max)
let buffer = try await request.body.collate(maxSize: .max)
let tag = try context.parameters.require("tag")
try await persist.set(key: tag, value: String(buffer: buffer))
return .ok
}
router.put("/persist/:tag/:time") { request, context -> HTTPResponse.Status in
guard let time = context.parameters.get("time", as: Int.self) else { throw HBHTTPError(.badRequest) }
let buffer = try await request.body.collect(upTo: .max)
let buffer = try await request.body.collate(maxSize: .max)
let tag = try context.parameters.require("tag")
try await persist.set(key: tag, value: String(buffer: buffer), expires: .seconds(time))
return .ok
Expand Down Expand Up @@ -65,7 +65,7 @@ final class PersistTests: XCTestCase {
let (router, persist) = try createRouter()

router.put("/create/:tag") { request, context -> HTTPResponse.Status in
let buffer = try await request.body.collect(upTo: .max)
let buffer = try await request.body.collate(maxSize: .max)
let tag = try context.parameters.require("tag")
try await persist.create(key: tag, value: String(buffer: buffer))
return .ok
Expand All @@ -84,7 +84,7 @@ final class PersistTests: XCTestCase {
func testDoubleCreateFail() async throws {
let (router, persist) = try createRouter()
router.put("/create/:tag") { request, context -> HTTPResponse.Status in
let buffer = try await request.body.collect(upTo: .max)
let buffer = try await request.body.collate(maxSize: .max)
let tag = try context.parameters.require("tag")
do {
try await persist.create(key: tag, value: String(buffer: buffer))
Expand Down Expand Up @@ -154,7 +154,7 @@ final class PersistTests: XCTestCase {
let (router, persist) = try createRouter()
router.put("/codable/:tag") { request, context -> HTTPResponse.Status in
guard let tag = context.parameters.get("tag") else { throw HBHTTPError(.badRequest) }
let buffer = try await request.body.collect(upTo: .max)
let buffer = try await request.body.collate(maxSize: .max)
try await persist.set(key: tag, value: TestCodable(buffer: String(buffer: buffer)))
return .ok
}
Expand Down

0 comments on commit e5472cf

Please sign in to comment.