PureYAML/Tests/ParseProfileTests.swift

86 lines
4.2 KiB
Swift

import Foundation
@testable import PureYAML
import Testing
/// Where the time of `parseStreamLocated` goes on a large file, phase by phase (the phases of `Parser.parseEvents` and of the composer, each timed apart on the same
/// text). It prints `PARSEPROFILE` lines with the median of the rounds and the machine's load, and holds nothing to a budget. Run in a release build with
/// `PURETREE_ASSETS` set to the asset library: `swift test -c release -Xswiftc -enable-testing --filter ParseProfileTests`.
@Suite(
"Parse profile", .serialized,
.enabled(if: ProcessInfo.processInfo.environment["PURETREE_ASSETS"] != nil, "set PURETREE_ASSETS to the PureTreeAssets folder"),
)
struct ParseProfileTests {
static let files = ["openapi/box-api-3.0.json", "openapi/stripe-spec3.json", "openapi/github-api.github.com.json", "openapi/github-api.github.com.yaml"]
private typealias LibYAML = PureYAML.Parsing.LibYAML
private static func milliseconds(_ body: () throws -> Void) -> Double {
let elapsed = ContinuousClock().measure {
do {
try body()
} catch {
Issue.record("a phase failed: \(error)")
}
}
return Double(elapsed.components.seconds) * 1000 + Double(elapsed.components.attoseconds) / 1e15
}
private static func median(_ values: [Double]) -> Double {
let sorted = values.sorted()
return sorted.isEmpty ? 0 : (sorted[sorted.count / 2] + sorted[(sorted.count - 1) / 2]) / 2
}
private static func load() -> String {
var averages = [Double](repeating: 0, count: 3)
return getloadavg(&averages, 3) == 3 ? String(format: "%.2f", averages[0]) : "unknown"
}
/// One round: each phase timed on its own, the input of each the output of the one before.
private static func round(_ text: String, into record: (String, Double) -> Void) {
let parser = PureYAML.Parsing.Parser()
record("1 reader: Array(unicodeScalars) and the printable check", milliseconds { _ = LibYAML.Reader(text) })
var tokens: [LibYAML.Token] = []
record("2 scanner: reader and every token (includes phase 1)", milliseconds {
var scanner = LibYAML.Scanner(text)
tokens = try scanner.scanAll()
})
var events: [PureYAML.Parsing.Event] = []
record("3 parser: tokens to events", milliseconds {
var machine = LibYAML.Parser(tokens: tokens)
events = try machine.parse()
})
var bumped: [PureYAML.Parsing.Event] = []
record("4 bumpingMark over the events", milliseconds { bumped = events.map { $0.bumpingMark() } })
record("5 composer: events to located values", milliseconds {
var composer = PureYAML.Parsing.EventStreamComposer(scalarParser: parser, mode: .stream, tracksLocations: true)
for event in bumped {
try composer.consume(event)
}
_ = try composer.composedLocatedStream()
})
record("0 the whole: Parser.parseStreamLocated", milliseconds { _ = try parser.parseStreamLocated(text) })
record("6 teardown: releasing the tokens and the events", milliseconds {
tokens = []
events = []
bumped = []
})
}
@Test("the phases of parseStreamLocated", arguments: files)
func phases(name: String) throws {
let root = URL(fileURLWithPath: ProcessInfo.processInfo.environment["PURETREE_ASSETS"] ?? "/nonexistent")
let text = try String(contentsOf: root.appendingPathComponent(name), encoding: .utf8)
var times: [String: [Double]] = [:]
for _ in 0 ..< 5 {
Self.round(text) { phase, value in times[phase, default: []].append(value) }
}
let load = Self.load()
for phase in times.keys.sorted() {
let all = times[phase] ?? []
let median = String(format: "%.1f", Self.median(all)), least = String(format: "%.1f", all.min() ?? 0)
print("PARSEPROFILE|\(name)|\(text.utf8.count)|\(phase)|median \(median)|min \(least)|n \(all.count)|load \(load)")
}
print("PARSEPROFILE|\(name)|unicode scalars \(text.unicodeScalars.count)|bytes \(text.utf8.count)")
}
}