Skip to content

Commit 8d56221

Browse files
committed
Add a subcommand to swift-parser-cli to measure performance
1 parent 0d0b70a commit 8d56221

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Sources/swift-parser-cli/swift-parser-cli.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class SwiftParserCli: ParsableCommand {
7979
static var configuration = CommandConfiguration(
8080
abstract: "Utility to test SwiftSyntax syntax tree creation.",
8181
subcommands: [
82+
PerformanceTest.self,
8283
PrintDiags.self,
8384
PrintTree.self,
8485
Reduce.self,
@@ -148,6 +149,41 @@ class VerifyRoundTrip: ParsableCommand {
148149
}
149150
}
150151

152+
class PerformanceTest: ParsableCommand {
153+
static var configuration = CommandConfiguration(
154+
commandName: "performance-test",
155+
abstract: "Parse all .swift files in '--directory' and its subdirectories '--iteration' times and output the average time (in milliseconds) one iteration took."
156+
)
157+
158+
required init() {}
159+
160+
@Option(help: "The directory in which all .swift files should be parsed")
161+
var directory: String
162+
163+
@Option(help: "How many times should the directory be parsed?")
164+
var iterations: Int
165+
166+
func run() throws {
167+
let sourceDir = URL(fileURLWithPath: self.directory)
168+
169+
let files = try FileManager.default
170+
.enumerator(at: sourceDir, includingPropertiesForKeys: nil)!
171+
.compactMap({ $0 as? URL })
172+
.filter { $0.pathExtension == "swift" }
173+
.map { try Data(contentsOf: $0) }
174+
175+
let start = Date()
176+
for _ in 0..<self.iterations {
177+
for file in files {
178+
file.withUnsafeBytes { buf in
179+
_ = Parser.parse(source: buf.bindMemory(to: UInt8.self))
180+
}
181+
}
182+
}
183+
print(Date().timeIntervalSince(start) / Double(self.iterations) * 1000)
184+
}
185+
}
186+
151187
class PrintDiags: ParsableCommand {
152188
static var configuration = CommandConfiguration(
153189
commandName: "print-diags",

0 commit comments

Comments
 (0)