Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚗️ Add binary size benchmarks #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions Benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>classNames</key>
<dict>
<key>BinarySizeBenchmark1</key>
<dict>
<key>testPerformance()</key>
<dict>
<key>com.apple.dt.XCTMetric_Memory.physical</key>
<dict>
<key>baselineAverage</key>
<real>2.457600</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
</dict>
</dict>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>runDestinationsByUUID</key>
<dict>
<key>3B817AB4-82BB-4C8E-BADA-A8577468EACF</key>
<dict>
<key>localComputer</key>
<dict>
<key>busSpeedInMHz</key>
<integer>400</integer>
<key>cpuCount</key>
<integer>1</integer>
<key>cpuKind</key>
<string>6-Core Intel Core i7</string>
<key>cpuSpeedInMHz</key>
<integer>2600</integer>
<key>logicalCPUCoresPerPackage</key>
<integer>12</integer>
<key>modelCode</key>
<string>MacBookPro16,1</string>
<key>physicalCPUCoresPerPackage</key>
<integer>6</integer>
<key>platformIdentifier</key>
<string>com.apple.platform.macosx</string>
</dict>
<key>targetArchitecture</key>
<string>x86_64</string>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BinarySizeBenchmarks"
BuildableName = "BinarySizeBenchmarks"
BlueprintName = "BinarySizeBenchmarks"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "binary-size-1"
BuildableName = "binary-size-1"
BlueprintName = "binary-size-1"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES"
viewDebuggingEnabled = "No">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "binary-size-1"
BuildableName = "binary-size-1"
BlueprintName = "binary-size-1"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "binary-size-1"
BuildableName = "binary-size-1"
BlueprintName = "binary-size-1"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
31 changes: 31 additions & 0 deletions Benchmarks/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"pins" : [
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-algorithms",
"state" : {
"revision" : "b14b7f4c528c942f121c8b860b9410b2bf57825e",
"version" : "1.0.0"
}
},
{
"identity" : "swift-nonempty",
"kind" : "remoteSourceControl",
"location" : "https://github.com/RemiBardon/swift-nonempty",
"state" : {
"revision" : "f951b7bcd4f13586307f53b0de5d1b20976aceab"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
}
],
"version" : 2
}
27 changes: 27 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// swift-tools-version: 5.7

import PackageDescription

let package = Package(
name: "swift-geo-benchmarks",
platforms: [
.macOS(.v13),
],
products: [
.executable(name: "binary-size-ref-1", targets: ["BinarySizeReference1"]),
.executable(name: "binary-size-1", targets: ["BinarySizeBenchmark1"]),
],
dependencies: [
.package(name: "swift-geo", path: ".."),
],
targets: [
.executableTarget(name: "BinarySizeReference1"),
.executableTarget(name: "BinarySizeBenchmark1", dependencies: [
.product(name: "WGS84", package: "swift-geo"),
]),
.testTarget(name: "BinarySizeBenchmarks"),
.testTarget(name: "PerformanceBenchmarks", dependencies: [
.product(name: "Turf", package: "swift-geo"),
]),
]
)
1 change: 1 addition & 0 deletions Benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# SwiftGeo benchmarks
14 changes: 14 additions & 0 deletions Benchmarks/Sources/BinarySizeBenchmark1/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//import WGS84
//import GeodeticConversions
//import TurfCore
//
//let point = Point2D(coordinates: .init(latitude: 10.2, longitude: 20.3))
//print(point)
//let coordinates = point.coordinates.to(WGS84Geographic3DCRS.self)
//print(coordinates)
//print(coordinates.dmsNotation)
//print(point.bbox)
import Foundation

print("Hello, World!")
print(cos(Double(1)))
4 changes: 4 additions & 0 deletions Benchmarks/Sources/BinarySizeReference1/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Foundation

print("Hello, World!")
print(cos(Double(1)))
97 changes: 97 additions & 0 deletions Benchmarks/Tests/BinarySizeBenchmarks/BinarySizeBenchmark1.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// BinarySizeBenchmark1.swift
// SwiftGeo
//
// Created by Rémi Bardon on 10/01/2023.
//

import XCTest

/// 1 kB
let kB: UInt64 = 1_000
/// 1 MB
let MB: UInt64 = 1_000_000

final class BinarySizeBenchmark1: XCTestCase {

static var refSize: UInt64 = 0

override class func setUp() {
let binaryName = "binary-size-ref-1"
do {
Self.swiftBuild(binaryName)
let binary = try FileAttributes(for: Self.binary(binaryName))
refSize = binary.fileSize
print("\(binaryName) binary has size \(binary.fileSizeString)")
} catch {
print("\(binaryName) binary could not be built: \(String(describing: error))")
}
}

func testBinarySize() throws {
let binaryName = "binary-size-1"
Self.swiftBuild(binaryName)
let binary = try FileAttributes(for: Self.binary(binaryName))
XCTAssertLessThan(binary.fileSize, 3500 * kB, binary.fileSizeString)
print("\(binaryName) binary has size \(binary.fileSizeString)")
}

// MARK: Helpers

private static let configuration: String = "release"

private static let fileURL: URL = URL(string: #file)!
private static let packageFolder: URL = fileURL
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
private static func binary(_ name: String) -> URL {
return Self.packageFolder.appending(
components: ".build", Self.configuration, name,
directoryHint: .notDirectory
)
}

/// - Copyright: <https://stackoverflow.com/a/26973384/10967642>
@discardableResult
private static func swift(_ args: [String]) -> Int32 {
let task = Process()
task.executableURL = URL(fileURLWithPath: "/usr/bin/swift")
task.arguments = args
print("Running `swift \(args.joined(separator: " "))`…")
try? task.run()
task.waitUntilExit()
return task.terminationStatus
}

/// - Copyright: <https://stackoverflow.com/a/26973384/10967642>
@discardableResult
private static func swift(_ args: String...) -> Int32 {
return swift(args)
}

@discardableResult
private static func swiftBuild(_ binaryName: String) -> Int32 {
let args = [
"build",
"--package-path", Self.packageFolder.absoluteString,
"--product", binaryName,
"-c", Self.configuration,
"-Xswiftc", "-cross-module-optimization",
"-Xswiftc", "-Osize",
]
return swift(args)
}

}

/// - Copyright: Inspired by <https://stackoverflow.com/a/48566887/10967642>.
fileprivate struct FileAttributes {
let fileSize: UInt64
let fileSizeString: String
init(for url: URL) throws {
let attributes = try FileManager.default.attributesOfItem(atPath: url.path)
self.fileSize = attributes[.size] as? UInt64 ?? UInt64(0)
self.fileSizeString = ByteCountFormatter.string(fromByteCount: Int64(self.fileSize), countStyle: .file)
}
}
Loading