A fast, safe, and efficient CBOR serialization library for Swift on any platform. swiftpackageindex.com/thecoolwinter/CBOR/1.1.1/documentation/cbor
atproto swift cbor
at 1.1.1 122 lines 3.5 kB view raw
1// 2// Profiling.swift 3// CBOR 4// 5// Created by Khan Winter on 10/20/25. 6// 7 8import CBOR 9import Foundation 10 11struct Profiling { 12 @_optimize(none) 13 func blackhole(_ val: some Any) { } 14 15 func time(_ cbor: () throws -> Void, _ json: () throws -> Void) throws { 16 guard #available(macOS 15.0, *) else { fatalError() } 17 func calculateStats(_ measurements: [Duration]) -> (average: Double, stddev: Double) { 18 let values = measurements.map { 19 Double($0.components.seconds) + (Double($0.components.attoseconds) / 1e15) 20 } 21 let avg = values.reduce(0, +) / Double(values.count) 22 23 let variance = values.map { pow($0 - avg, 2) }.reduce(0, +) / Double(values.count) 24 let stddev = sqrt(variance) 25 26 return (avg, stddev) 27 } 28 29 let iterations = 10 30 31 var cborTimes: [Duration] = [] 32 var jsonTimes: [Duration] = [] 33 34 for _ in 0..<iterations { 35 let cborTime = try SuspendingClock().measure { 36 try cbor() 37 } 38 cborTimes.append(cborTime) 39 40 let jsonTime = try SuspendingClock().measure { 41 try json() 42 } 43 jsonTimes.append(jsonTime) 44 } 45 46 let cborStats = calculateStats(cborTimes) 47 let jsonStats = calculateStats(jsonTimes) 48 49 let percentChange = 100.0 * ((cborStats.average - jsonStats.average) / jsonStats.average) 50 51 print("CBOR: \(String(format: "%.3f", cborStats.average)) ± \(String(format: "%.3f", cborStats.stddev)) ms") 52 print("JSON: \(String(format: "%.3f", jsonStats.average)) ± \(String(format: "%.3f", jsonStats.stddev)) ms") 53 print("Difference: \(percentChange > 0 ? "+" : "")\(String(format: "%.2f", percentChange))%") 54 } 55 56 func complex() throws { 57 try time { 58 for _ in 0..<1000 { 59 blackhole(try CBOREncoder().encode(Company.mock)) 60 } 61 } _: { 62 for _ in 0..<1000 { 63 blackhole(try JSONEncoder().encode(Company.mock)) 64 } 65 } 66 67 } 68 69 func dictionary() throws { 70 let data: [String: Int] = (0..<1000).reduce(into: [String: Int](), { $0[String(describing: $1)] = $1 }) 71 72 try time { 73 for _ in 0..<100 { 74 blackhole(try CBOREncoder().encode(data)) 75 } 76 } _: { 77 for _ in 0..<100 { 78 blackhole(try JSONEncoder().encode(data)) 79 } 80 } 81 82 } 83 84 func int() throws { 85 try time { 86 for _ in 0..<1000 { 87 blackhole(try CBOREncoder().encode(Int.random(in: Int.min..<Int.max))) 88 } 89 } _: { 90 for _ in 0..<1000 { 91 blackhole(try JSONEncoder().encode(Int.random(in: Int.min..<Int.max))) 92 } 93 } 94 95 } 96 97 func string() throws { 98 try time { 99 for _ in 0..<1000 { 100 blackhole(try CBOREncoder().encode("ABCDEFGHIJKLMNOP")) 101 } 102 } _: { 103 for _ in 0..<1000 { 104 blackhole(try JSONEncoder().encode("ABCDEFGHIJKLMNOP")) 105 } 106 } 107 108 } 109 110 func data() throws { 111 try time { 112 for _ in 0..<1000 { 113 blackhole(try CBOREncoder().encode(Data([0x01, 0x02, 0x03, 0x04]))) 114 } 115 } _: { 116 for _ in 0..<1000 { 117 blackhole(try JSONEncoder().encode(Data([0x01, 0x02, 0x03, 0x04]))) 118 } 119 } 120 121 } 122}