Skip to content

Commit ecdc429

Browse files
committed
A performance-oriented test decoding 1 million messages from a stream
Checking this in to help aid in future performance optimization for AsyncMessageSequence
1 parent 10295a8 commit ecdc429

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
889 KB
Binary file not shown.

Tests/SwiftProtobufTests/Test_AsyncMessageSequence.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ final class Test_AsyncMessageSequence: XCTestCase {
199199
}
200200
XCTAssertTrue(truncatedThrown, "Should throw a BinaryDelimited.Error.truncated")
201201
}
202+
203+
// Slow test case found by oss-fuzz: 1 million zero-sized messages
204+
// A similar test with BinaryDelimited is about 4x faster, showing
205+
// that we have some room for improvement here:
206+
func testLargeExample() async throws {
207+
let bytes = [UInt8](repeating: 0, count: 1000000)
208+
let byteStream = asyncByteStream(bytes: bytes)
209+
let decodedStream = byteStream.binaryProtobufDelimitedMessages(
210+
of: SwiftProtoTesting_TestAllTypes.self,
211+
extensions: SwiftProtoTesting_Fuzz_FuzzTesting_Extensions)
212+
var count = 0
213+
for try await message in decodedStream {
214+
XCTAssertEqual(message, SwiftProtoTesting_TestAllTypes())
215+
count += 1
216+
}
217+
XCTAssertEqual(count, 1000000)
218+
}
202219

203220
fileprivate func asyncByteStream(bytes: [UInt8]) -> AsyncStream<UInt8> {
204221
AsyncStream(UInt8.self) { continuation in

Tests/SwiftProtobufTests/Test_BinaryDelimited.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,20 @@ final class Test_BinaryDelimited: XCTestCase {
145145
assertParseFails(atEndOfStream: stream2)
146146
}
147147

148+
// oss-fuzz found this case that runs slowly for AsyncMessageSequence
149+
// Copied here as well for comparison.
150+
func testLargeExample() throws {
151+
let bytes = [UInt8](repeating: 0, count: 1000000)
152+
let istream = openInputStream(bytes)
153+
154+
for _ in 0..<1000000 {
155+
let msg = try BinaryDelimited.parse(
156+
messageType: SwiftProtoTesting_TestAllTypes.self,
157+
from: istream)
158+
XCTAssertEqual(msg, SwiftProtoTesting_TestAllTypes())
159+
}
160+
XCTAssertThrowsError(try BinaryDelimited.parse(
161+
messageType: SwiftProtoTesting_TestAllTypes.self,
162+
from: istream))
163+
}
148164
}

0 commit comments

Comments
 (0)