Skip to content

Commit a146c4a

Browse files
authored
Merge pull request #1499 from tbkka/tbkka-large-oss-fuzz-example
A performance-oriented test decoding 1 million messages from a stream
2 parents 3ef835e + 692d45a commit a146c4a

File tree

3 files changed

+37
-0
lines changed

3 files changed

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

Tests/SwiftProtobufTests/Test_AsyncMessageSequence.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ 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+
// (Note this currently only tests 100,000 zero-sized messages,
207+
// but the constant below is easy to edit if you want to experiment.)
208+
func testLargeExample() async throws {
209+
let messageCount = 100_000
210+
let bytes = [UInt8](repeating: 0, count: messageCount)
211+
let byteStream = asyncByteStream(bytes: bytes)
212+
let decodedStream = byteStream.binaryProtobufDelimitedMessages(
213+
of: SwiftProtoTesting_TestAllTypes.self,
214+
extensions: SwiftProtoTesting_Fuzz_FuzzTesting_Extensions)
215+
var count = 0
216+
for try await message in decodedStream {
217+
XCTAssertEqual(message, SwiftProtoTesting_TestAllTypes())
218+
count += 1
219+
}
220+
XCTAssertEqual(count, messageCount)
221+
}
202222

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

Tests/SwiftProtobufTests/Test_BinaryDelimited.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,21 @@ 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 messageCount = 100_000
152+
let bytes = [UInt8](repeating: 0, count: messageCount)
153+
let istream = openInputStream(bytes)
154+
155+
for _ in 0..<messageCount {
156+
let msg = try BinaryDelimited.parse(
157+
messageType: SwiftProtoTesting_TestAllTypes.self,
158+
from: istream)
159+
XCTAssertEqual(msg, SwiftProtoTesting_TestAllTypes())
160+
}
161+
XCTAssertThrowsError(try BinaryDelimited.parse(
162+
messageType: SwiftProtoTesting_TestAllTypes.self,
163+
from: istream))
164+
}
148165
}

0 commit comments

Comments
 (0)