Skip to content

Commit 68056e8

Browse files
authored
Merge pull request #1416 from ahoppen/ahoppen/instruction-counter
Output number of instructions executed during performance test in `swift-parser-cli performance-test`
2 parents 9dff225 + f451816 commit 68056e8

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ let package = Package(
4848
.library(name: "SwiftRefactor", type: .static, targets: ["SwiftRefactor"]),
4949
],
5050
targets: [
51+
.target(name: "_InstructionCounter"),
5152
.target(
5253
name: "SwiftBasicFormat",
5354
dependencies: ["SwiftSyntax"],
@@ -148,7 +149,7 @@ let package = Package(
148149
.executableTarget(
149150
name: "swift-parser-cli",
150151
dependencies: [
151-
"SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics", "SwiftOperators",
152+
"_InstructionCounter", "SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics", "SwiftOperators",
152153
.product(name: "ArgumentParser", package: "swift-argument-parser"),
153154
]
154155
),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <unistd.h>
14+
15+
/// Returns the number of instructions the process has executed since it was
16+
/// launched.
17+
uint64_t getInstructionsExecuted();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "InstructionsExecuted.h"
14+
#include <libproc.h>
15+
#include <sys/resource.h>
16+
17+
uint64_t getInstructionsExecuted() {
18+
struct rusage_info_v4 ru;
19+
if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) {
20+
return ru.ri_instructions;
21+
}
22+
return 0;
23+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import _InstructionCounter
1314
import SwiftDiagnostics
1415
import SwiftSyntax
1516
import SwiftParser
@@ -177,14 +178,19 @@ class PerformanceTest: ParsableCommand {
177178
.map { try Data(contentsOf: $0) }
178179

179180
let start = Date()
181+
let startInstructions = getInstructionsExecuted()
180182
for _ in 0..<self.iterations {
181183
for file in files {
182184
file.withUnsafeBytes { buf in
183185
_ = Parser.parse(source: buf.bindMemory(to: UInt8.self))
184186
}
185187
}
186188
}
187-
print(Date().timeIntervalSince(start) / Double(self.iterations) * 1000)
189+
let endInstructions = getInstructionsExecuted()
190+
let endDate = Date()
191+
192+
print("Time: \(endDate.timeIntervalSince(start) / Double(self.iterations) * 1000)ms")
193+
print("Instructions: \(Double(endInstructions - startInstructions) / Double(self.iterations))")
188194
}
189195
}
190196

0 commit comments

Comments
 (0)