Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ let package = Package(
.library(name: "SwiftRefactor", type: .static, targets: ["SwiftRefactor"]),
],
targets: [
.target(name: "_InstructionCounter"),
.target(
name: "SwiftBasicFormat",
dependencies: ["SwiftSyntax"],
Expand Down Expand Up @@ -148,7 +149,7 @@ let package = Package(
.executableTarget(
name: "swift-parser-cli",
dependencies: [
"SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics", "SwiftOperators",
"_InstructionCounter", "SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics", "SwiftOperators",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
Expand Down
17 changes: 17 additions & 0 deletions Sources/_InstructionCounter/include/InstructionsExecuted.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include <unistd.h>

/// Returns the number of instructions the process has executed since it was
/// launched.
uint64_t getInstructionsExecuted();
23 changes: 23 additions & 0 deletions Sources/_InstructionCounter/src/InstructionsExecuted.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "InstructionsExecuted.h"
#include <libproc.h>
#include <sys/resource.h>

uint64_t getInstructionsExecuted() {
struct rusage_info_v4 ru;
if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) {
return ru.ri_instructions;
}
return 0;
}
8 changes: 7 additions & 1 deletion Sources/swift-parser-cli/swift-parser-cli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import _InstructionCounter
import SwiftDiagnostics
import SwiftSyntax
import SwiftParser
Expand Down Expand Up @@ -177,14 +178,19 @@ class PerformanceTest: ParsableCommand {
.map { try Data(contentsOf: $0) }

let start = Date()
let startInstructions = getInstructionsExecuted()
for _ in 0..<self.iterations {
for file in files {
file.withUnsafeBytes { buf in
_ = Parser.parse(source: buf.bindMemory(to: UInt8.self))
}
}
}
print(Date().timeIntervalSince(start) / Double(self.iterations) * 1000)
let endInstructions = getInstructionsExecuted()
let endDate = Date()

print("Time: \(endDate.timeIntervalSince(start) / Double(self.iterations) * 1000)ms")
print("Instructions: \(Double(endInstructions - startInstructions) / Double(self.iterations))")
}
}

Expand Down