|
15 | 15 | import SwiftFoundation
|
16 | 16 | import SwiftXCTest
|
17 | 17 | #endif
|
18 |
| - |
| 18 | +import Dispatch |
19 | 19 |
|
20 | 20 | class TestNSJSONSerialization : XCTestCase {
|
21 | 21 |
|
@@ -598,6 +598,7 @@ extension TestNSJSONSerialization {
|
598 | 598 | ("test_invalidJsonObjectToStreamBuffer", test_invalidJsonObjectToStreamBuffer),
|
599 | 599 | ("test_jsonObjectToOutputStreamInsufficeintBuffer", test_jsonObjectToOutputStreamInsufficeintBuffer),
|
600 | 600 | ("test_booleanJSONObject", test_booleanJSONObject),
|
| 601 | + ("test_serialization_perf", test_serialize_perf), |
601 | 602 | ]
|
602 | 603 | }
|
603 | 604 |
|
@@ -743,6 +744,35 @@ extension TestNSJSONSerialization {
|
743 | 744 | XCTAssertEqual(try trySerialize(jsonArr4), "[{\"a\":null},{\"b\":null}]")
|
744 | 745 | }
|
745 | 746 |
|
| 747 | + func test_serialize_perf() { |
| 748 | + let numItems = 20 // Number of elements in the payload |
| 749 | + let loops = 500 // Number of times to invoke serialization per measurement |
| 750 | + let concurrency = 4 // Number of concurrent threads |
| 751 | + var PAYLOAD = [String:Any]() // The string to convert |
| 752 | + |
| 753 | + for i in 1...numItems { |
| 754 | + PAYLOAD["Item \(i)"] = Double(i) |
| 755 | + } |
| 756 | + |
| 757 | + let queue = DispatchQueue(label: "testcaseQueue", attributes: .concurrent) |
| 758 | + let group = DispatchGroup() |
| 759 | + |
| 760 | + self.measure { |
| 761 | + for _ in 1...concurrency { |
| 762 | + queue.async(group: group) { |
| 763 | + for _ in 1...loops { |
| 764 | + do { |
| 765 | + _ = try JSONSerialization.data(withJSONObject: PAYLOAD) |
| 766 | + } catch { |
| 767 | + XCTFail("Could not serialize to JSON: \(error)") |
| 768 | + } |
| 769 | + } |
| 770 | + } |
| 771 | + } |
| 772 | + _ = group.wait(timeout: .distantFuture) // forever |
| 773 | + } |
| 774 | + } |
| 775 | + |
746 | 776 | func test_nested_array() {
|
747 | 777 | var arr: [Any] = ["a"]
|
748 | 778 | XCTAssertEqual(try trySerialize(arr), "[\"a\"]")
|
|
0 commit comments