1+ import Foundation
2+
3+ // Benchmark comparison between original and optimized versions
4+ public struct BenchmarkComparison {
5+
6+ public static func run( ) {
7+ print ( " === SwiftJSONSanitizer Performance Benchmark === \n " )
8+
9+ // Generate test data
10+ let smallJSON = generateJSON ( depth: 3 , breadth: 5 )
11+ let mediumJSON = generateJSON ( depth: 4 , breadth: 6 )
12+ let largeJSON = generateJSON ( depth: 5 , breadth: 8 )
13+ let veryLargeJSON = generateJSON ( depth: 6 , breadth: 8 )
14+
15+ print ( " Test data sizes: " )
16+ print ( " - Small: \( smallJSON. count) bytes " )
17+ print ( " - Medium: \( mediumJSON. count) bytes " )
18+ print ( " - Large: \( largeJSON. count) bytes " )
19+ print ( " - Very Large: \( veryLargeJSON. count) bytes " )
20+ print ( " " )
21+
22+ // Warm up
23+ _ = SwiftJSONSanitizer . sanitize ( smallJSON, options: . minify)
24+ _ = SwiftJSONSanitizer . sanitize ( smallJSON, options: . prettyPrint)
25+
26+ // Run benchmarks
27+ print ( " Running benchmarks (100 iterations each)... " )
28+ print ( " " )
29+
30+ // Small JSON
31+ print ( " Small JSON: " )
32+ benchmarkSize ( smallJSON, label: " Small " , iterations: 100 )
33+
34+ // Medium JSON
35+ print ( " \n Medium JSON: " )
36+ benchmarkSize ( mediumJSON, label: " Medium " , iterations: 100 )
37+
38+ // Large JSON
39+ print ( " \n Large JSON: " )
40+ benchmarkSize ( largeJSON, label: " Large " , iterations: 100 )
41+
42+ // Very Large JSON
43+ print ( " \n Very Large JSON: " )
44+ benchmarkSize ( veryLargeJSON, label: " Very Large " , iterations: 50 )
45+
46+ // Memory pressure test
47+ print ( " \n === Memory Pressure Test === " )
48+ memoryPressureTest ( )
49+ }
50+
51+ private static func benchmarkSize( _ json: String , label: String , iterations: Int ) {
52+ // Minify benchmark
53+ let minifyStart = CFAbsoluteTimeGetCurrent ( )
54+ for _ in 0 ..< iterations {
55+ _ = SwiftJSONSanitizer . sanitize ( json, options: . minify)
56+ }
57+ let minifyTime = CFAbsoluteTimeGetCurrent ( ) - minifyStart
58+
59+ // Pretty print benchmark
60+ let prettyStart = CFAbsoluteTimeGetCurrent ( )
61+ for _ in 0 ..< iterations {
62+ _ = SwiftJSONSanitizer . sanitize ( json, options: . prettyPrint)
63+ }
64+ let prettyTime = CFAbsoluteTimeGetCurrent ( ) - prettyStart
65+
66+ // Calculate and print results
67+ let minifyPerIteration = ( minifyTime / Double( iterations) ) * 1000
68+ let prettyPerIteration = ( prettyTime / Double( iterations) ) * 1000
69+
70+ print ( " Minify: \( String ( format: " %.3f " , minifyPerIteration) ) ms per iteration " )
71+ print ( " Pretty: \( String ( format: " %.3f " , prettyPerIteration) ) ms per iteration " )
72+ print ( " Throughput (minify): \( String ( format: " %.1f " , Double ( json. count * iterations) / minifyTime / 1_000_000 ) ) MB/s " )
73+ print ( " Throughput (pretty): \( String ( format: " %.1f " , Double ( json. count * iterations) / prettyTime / 1_000_000 ) ) MB/s " )
74+ }
75+
76+ private static func memoryPressureTest( ) {
77+ let hugeJSON = generateJSON ( depth: 7 , breadth: 5 )
78+ print ( " Testing with \( hugeJSON. count) bytes... " )
79+
80+ let start = CFAbsoluteTimeGetCurrent ( )
81+ _ = SwiftJSONSanitizer . sanitize ( hugeJSON, options: . prettyPrint)
82+ let time = CFAbsoluteTimeGetCurrent ( ) - start
83+
84+ print ( " Time: \( String ( format: " %.3f " , time * 1000 ) ) ms " )
85+ print ( " Throughput: \( String ( format: " %.1f " , Double ( hugeJSON. count) / time / 1_000_000 ) ) MB/s " )
86+ }
87+
88+ private static func generateJSON( depth: Int , breadth: Int ) -> String {
89+ func generateObject( _ currentDepth: Int ) -> String {
90+ if currentDepth == 0 {
91+ return " { \" leaf \" : \" value with some text content here that makes it realistic \" } "
92+ }
93+
94+ var object = " { "
95+ for i in 0 ..< breadth {
96+ object += " \" key \( i) \" : "
97+ if i % 4 == 0 {
98+ object += " [1,2,3,4,5 " // Missing closing bracket
99+ } else if i % 4 == 1 {
100+ object += generateObject ( currentDepth - 1 )
101+ } else if i % 4 == 2 {
102+ object += " \" string value with special chars: {}, [], and a comma, " // Missing closing quote
103+ } else {
104+ object += " true "
105+ }
106+ if i < breadth - 1 {
107+ object += " , "
108+ }
109+ }
110+ // Randomly omit closing brace
111+ if currentDepth % 2 == 0 {
112+ object += " } "
113+ }
114+ return object
115+ }
116+
117+ return generateObject ( depth)
118+ }
119+ }
120+
121+ // Function to run from command line
122+ func runBenchmark( ) {
123+ BenchmarkComparison . run ( )
124+ }
0 commit comments