PureXML/Benchmarks/bench-purexml.swift
Mihaela Mihaljevic 8e1415aa64 feat(bench): PureXML vs libxml2 benchmark harness with first baseline
scripts/benchmark.sh generates a deterministic corpus, builds PureXML
in release and a C driver against the SDK's libxml2, times parse,
serialize, and XPath over the same bytes (best-of-N, internal timing,
IO excluded), cross-checks the same XPath count on both sides, and
prints the comparison. First honest baseline recorded in
docs/benchmarks.md: parse ~30x, xpath ~36x, serialize ~5x slower than
libxml2 on a 4.2 MB corpus; these are the numbers #139's optimization
passes burn down. The SAX stage isolation is blocked on #142 (the
public streaming surface is internal, found by this harness).
2026-06-10 18:16:09 +02:00

56 lines
1.8 KiB
Swift

// PureXML side of the benchmark (scripts/benchmark.sh). Times the same
// operations the C driver times, over the same file, with internal timing.
// Output: CSV lines "library,operation,bytes,seconds".
// Foundation is used for file access and timing only; the measured code is
// the Foundation-free PureXML library.
import Foundation
import PureXML
let arguments = CommandLine.arguments
guard arguments.count >= 3, let iterations = Int(arguments[2]) else {
FileHandle.standardError.write(Data("usage: bench-purexml <file.xml> <iterations>\n".utf8))
exit(1)
}
let path = arguments[1]
let source = try String(contentsOfFile: path, encoding: .utf8)
let bytes = source.utf8.count
func best(_ iterations: Int, _ body: () throws -> Void) rethrows -> Double {
var bestTime = Double.greatestFiniteMagnitude
for _ in 0 ..< iterations {
let start = Date()
try body()
bestTime = min(bestTime, -start.timeIntervalSinceNow)
}
return bestTime
}
// Parse.
var tree: PureXML.Model.TreeNode!
let parseTime = try best(iterations) {
tree = try PureXML.parseTree(source)
}
print("purexml,parse,\(bytes),\(String(format: "%.6f", parseTime))")
// Serialize.
let node = tree.node
var serialized = ""
let serializeTime = best(iterations) {
serialized = PureXML.serialize(node)
}
print("purexml,serialize,\(bytes),\(String(format: "%.6f", serializeTime))")
FileHandle.standardError.write(Data("purexml serialized bytes: \(serialized.utf8.count)\n".utf8))
// XPath: the same broad selection the C driver counts.
let query = try PureXML.XPath.Query("count(//item[@kind='even'])")
var count = 0.0
let xpathTime = try best(iterations) {
count = try query.value(at: tree).number
}
print("purexml,xpath,\(bytes),\(String(format: "%.6f", xpathTime))")
FileHandle.standardError.write(Data("purexml xpath count: \(Int(count))\n".utf8))