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

A performance-oriented test decoding 1 million messages from a stream #1499

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
20 changes: 20 additions & 0 deletions Tests/SwiftProtobufTests/Test_AsyncMessageSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ final class Test_AsyncMessageSequence: XCTestCase {
}
XCTAssertTrue(truncatedThrown, "Should throw a BinaryDelimited.Error.truncated")
}

// Slow test case found by oss-fuzz: 1 million zero-sized messages
// A similar test with BinaryDelimited is about 4x faster, showing
// that we have some room for improvement here.
// (Note this currently only tests 100,000 zero-sized messages,
// but the constant below is easy to edit if you want to experiment.)
func testLargeExample() async throws {
let messageCount = 100_000
let bytes = [UInt8](repeating: 0, count: messageCount)
let byteStream = asyncByteStream(bytes: bytes)
tbkka marked this conversation as resolved.
Show resolved Hide resolved
let decodedStream = byteStream.binaryProtobufDelimitedMessages(
of: SwiftProtoTesting_TestAllTypes.self,
extensions: SwiftProtoTesting_Fuzz_FuzzTesting_Extensions)
var count = 0
for try await message in decodedStream {
XCTAssertEqual(message, SwiftProtoTesting_TestAllTypes())
count += 1
}
XCTAssertEqual(count, messageCount)
}

fileprivate func asyncByteStream(bytes: [UInt8]) -> AsyncStream<UInt8> {
AsyncStream(UInt8.self) { continuation in
Expand Down
17 changes: 17 additions & 0 deletions Tests/SwiftProtobufTests/Test_BinaryDelimited.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,21 @@ final class Test_BinaryDelimited: XCTestCase {
assertParseFails(atEndOfStream: stream2)
}

// oss-fuzz found this case that runs slowly for AsyncMessageSequence
// Copied here as well for comparison.
func testLargeExample() throws {
let messageCount = 100_000
let bytes = [UInt8](repeating: 0, count: messageCount)
let istream = openInputStream(bytes)

for _ in 0..<messageCount {
let msg = try BinaryDelimited.parse(
messageType: SwiftProtoTesting_TestAllTypes.self,
from: istream)
XCTAssertEqual(msg, SwiftProtoTesting_TestAllTypes())
}
XCTAssertThrowsError(try BinaryDelimited.parse(
messageType: SwiftProtoTesting_TestAllTypes.self,
from: istream))
}
}