فهرست منبع

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

Checking this in to help aid in future performance optimization for
AsyncMessageSequence
Tim Kientzle 2 سال پیش
والد
کامیت
ecdc42978c

BIN
FuzzTesting/FailCases/clusterfuzz-testcase-minimized-FuzzAsyncMessageSequence_debug-5217919734513664


+ 17 - 0
Tests/SwiftProtobufTests/Test_AsyncMessageSequence.swift

@@ -199,6 +199,23 @@ 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:
+  func testLargeExample() async throws {
+    let bytes = [UInt8](repeating: 0, count: 1000000)
+    let byteStream = asyncByteStream(bytes: bytes)
+    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, 1000000)
+  }
   
   fileprivate func asyncByteStream(bytes: [UInt8]) -> AsyncStream<UInt8> {
       AsyncStream(UInt8.self) { continuation in

+ 16 - 0
Tests/SwiftProtobufTests/Test_BinaryDelimited.swift

@@ -145,4 +145,20 @@ 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 bytes = [UInt8](repeating: 0, count: 1000000)
+    let istream = openInputStream(bytes)
+
+    for _ in 0..<1000000 {
+      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))
+  }
 }