forked from tornikegomareli/gitdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffParserBenchmarks.swift
More file actions
48 lines (40 loc) · 1.46 KB
/
DiffParserBenchmarks.swift
File metadata and controls
48 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Foundation
import Testing
@testable import gitdiff
struct DiffParserBenchmarks {
@Test
func benchmarkLargeMultiHunkParse() async throws {
// Adjust these to stress the parser; keep runtime reasonable for CI
let hunks = 1000
let linesPerHunk = 400
let iterations = 3
let diff = makeLargeMultiHunkDiff(hunks: hunks, linesPerHunk: linesPerHunk)
// Warm-up
_ = try await DiffParser.parse(diff)
let clock = ContinuousClock()
var totals: [Duration] = []
for _ in 0..<iterations {
let duration = try await clock.measure {
let files = try await DiffParser.parse(diff)
#expect(!files.isEmpty)
}
totals.append(duration)
}
// Report results
let nanos = totals.map { $0.components.attoseconds / 1_000_000_000 } // Duration printing hack
// Fallback formatting using Double seconds from Duration
func seconds(_ d: Duration) -> Double {
Double(d.components.seconds) + Double(d.components.attoseconds) / 1e18
}
let secs = totals.map(seconds)
let avg = secs.reduce(0, +) / Double(secs.count)
let minT = secs.min() ?? avg
let maxT = secs.max() ?? avg
print(
"DiffParser benchmark (hunks=\(hunks), linesPerHunk=\(linesPerHunk), iters=\(iterations))")
print(
String(
format: " times: %@", secs.map { String(format: "%.4fs", $0) }.joined(separator: ", ")))
print(String(format: " avg: %.4fs min: %.4fs max: %.4fs", avg, minT, maxT))
}
}