Skip to content

Commit 2d7c2c3

Browse files
committed
Add placeholder ordo-benchmark and JMH benchmarks
1 parent 83c7b0f commit 2d7c2c3

File tree

8 files changed

+158
-67
lines changed

8 files changed

+158
-67
lines changed

.github/workflows/pull_request.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
steps:
3030
- uses: actions/checkout@v4
3131
- name: Install System Dependencies
32-
run: apt-get -qq update && apt-get -qq install -y make curl wget
32+
run: apt-get -qq update && apt-get -qq install -y make curl wget libjemalloc2 libjemalloc-dev
3333
- name: Cache JDK
3434
id: cache-jdk
3535
uses: actions/cache@v4
@@ -86,7 +86,7 @@ jobs:
8686
steps:
8787
- uses: actions/checkout@v4
8888
- name: Install System Dependencies
89-
run: apt-get -qq update && apt-get -qq install -y make curl wget
89+
run: apt-get -qq update && apt-get -qq install -y make curl wget libjemalloc2 libjemalloc-dev
9090
- name: Cache JDK
9191
id: cache-jdk
9292
uses: actions/cache@v4
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Benchmark
16+
import Foundation
17+
import JavaKit
18+
import JavaKitNetwork
19+
20+
@MainActor let benchmarks = {
21+
var jvm: JavaVirtualMachine {
22+
get throws {
23+
try .shared()
24+
}
25+
}
26+
Benchmark("Simple call to Java library") { benchmark in
27+
for _ in benchmark.scaledIterations {
28+
let environment = try jvm.environment()
29+
30+
let urlConnectionClass = try JavaClass<URLConnection>(environment: environment)
31+
blackHole(urlConnectionClass.getDefaultAllowUserInteraction())
32+
}
33+
}
34+
}

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ jextract-generate: jextract-swift generate-JExtract-interface-files
133133
swift run jextract-swift \
134134
--package-name com.example.swift.generated \
135135
--swift-module ExampleSwiftLibrary \
136-
--output-directory ${SAMPLES_DIR}/SwiftKitSampleApp/src/generated/java \
136+
--output-directory ${SAMPLES_DIR}/SwiftKitSampleApp/build/generated/sources/jextract/main \
137137
$(BUILD_DIR)/jextract/ExampleSwiftLibrary/MySwiftLibrary.swiftinterface; \
138138
swift run jextract-swift \
139139
--package-name org.swift.swiftkit.generated \
140140
--swift-module SwiftKitSwift \
141-
--output-directory ${SAMPLES_DIR}/SwiftKitSampleApp/src/generated/java \
141+
--output-directory ${SAMPLES_DIR}/SwiftKitSampleApp/build/generated/sources/jextract/main \
142142
$(BUILD_DIR)/jextract/SwiftKitSwift/SwiftKit.swiftinterface
143143

144144

Package.swift

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,18 @@ let package = Package(
9494

9595
// ==== Plugin for building Java code
9696
.plugin(
97-
name: "JavaCompilerPlugin",
98-
targets: [
99-
"JavaCompilerPlugin"
100-
]
97+
name: "JavaCompilerPlugin",
98+
targets: [
99+
"JavaCompilerPlugin"
100+
]
101101
),
102102

103103
// ==== Plugin for wrapping Java classes in Swift
104104
.plugin(
105-
name: "Java2SwiftPlugin",
106-
targets: [
107-
"Java2SwiftPlugin"
108-
]
105+
name: "Java2SwiftPlugin",
106+
targets: [
107+
"Java2SwiftPlugin"
108+
]
109109
),
110110

111111
// ==== jextract-swift (extract Java accessors from Swift interface files)
@@ -140,6 +140,7 @@ let package = Package(
140140
.package(url: "https://github.com/swiftlang/swift-syntax.git", branch: "main"),
141141
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
142142
.package(url: "https://github.com/apple/swift-collections.git", .upToNextMinor(from: "1.1.0")),
143+
.package(url: "https://github.com/ordo-one/package-benchmark", .upToNextMajor(from: "1.4.0")),
143144
],
144145
targets: [
145146
.macro(
@@ -225,16 +226,16 @@ let package = Package(
225226
]
226227
),
227228
.plugin(
228-
name: "JavaCompilerPlugin",
229-
capability: .buildTool()
229+
name: "JavaCompilerPlugin",
230+
capability: .buildTool()
230231
),
231232

232233
.plugin(
233-
name: "Java2SwiftPlugin",
234-
capability: .buildTool(),
235-
dependencies: [
236-
"Java2Swift"
237-
]
234+
name: "Java2SwiftPlugin",
235+
capability: .buildTool(),
236+
dependencies: [
237+
"Java2Swift"
238+
]
238239
),
239240

240241
.target(
@@ -374,5 +375,23 @@ let package = Package(
374375
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
375376
]
376377
),
378+
379+
.executableTarget(
380+
name: "JavaApiCallBenchmarks",
381+
dependencies: [
382+
"JavaRuntime",
383+
"JavaKit",
384+
"JavaKitNetwork",
385+
.product(name: "Benchmark", package: "package-benchmark"),
386+
],
387+
path: "Benchmarks/JavaApiCallBenchmarks",
388+
swiftSettings: [
389+
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
390+
.swiftLanguageMode(.v5),
391+
],
392+
plugins: [
393+
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
394+
],
395+
),
377396
]
378397
)

Samples/SwiftKitSampleApp/build.gradle

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.swift.swiftkit.gradle.BuildUtils
1616

1717
plugins {
1818
id("build-logic.java-application-conventions")
19+
id "me.champeau.jmh" version "0.7.2"
1920
}
2021

2122
group = "org.swift.swiftkit"
@@ -31,43 +32,38 @@ java {
3132
}
3233
}
3334

34-
sourceSets {
35-
generated {
36-
java.srcDir "${buildDir}/generated/src/java/"
37-
}
3835

39-
// Make the 'main' and 'test' source sets depend on the generated sources
40-
main {
41-
compileClasspath += sourceSets.generated.output
42-
runtimeClasspath += sourceSets.generated.output
43-
}
44-
test {
45-
compileClasspath += sourceSets.main.output
46-
runtimeClasspath += sourceSets.main.output
36+
def jextract = tasks.register("jextract", Exec) {
37+
description = "Extracts Java accessor sources using jextract"
38+
39+
outputs.dir(layout.buildDirectory.dir("generated/sources/jextract/main"))
40+
inputs.dir("$rootDir/Sources/ExampleSwiftLibrary") // monitored library
41+
42+
// any changes in the source generator sources also mean the resulting output might change
43+
inputs.dir("$rootDir/Sources/JExtractSwift")
44+
inputs.dir("$rootDir/Sources/JExtractSwiftTool")
4745

48-
compileClasspath += sourceSets.generated.output
49-
runtimeClasspath += sourceSets.generated.output
46+
workingDir = rootDir
47+
commandLine "make"
48+
args "jextract-generate"
49+
}
50+
51+
sourceSets {
52+
main {
53+
java {
54+
srcDir(jextract)
55+
}
5056
}
5157
}
5258

5359
dependencies {
5460
implementation(project(':SwiftKit'))
55-
generatedImplementation(project(':SwiftKit'))
5661

5762
testImplementation(platform("org.junit:junit-bom:5.10.0"))
5863
testImplementation("org.junit.jupiter:junit-jupiter")
5964
}
6065

61-
configurations {
62-
generatedImplementation.extendsFrom(mainImplementation)
63-
generatedRuntimeOnly.extendsFrom(mainRuntimeOnly)
64-
}
65-
66-
tasks.named("compileJava").configure {
67-
dependsOn("jextract")
68-
}
69-
70-
tasks.test {
66+
tasks.named('test', Test) {
7167
useJUnitPlatform()
7268
}
7369

@@ -84,27 +80,15 @@ application {
8480
"--enable-native-access=ALL-UNNAMED",
8581

8682
// Include the library paths where our dylibs are that we want to load and call
87-
"-Djava.library.path=" + BuildUtils.javaLibraryPaths().join(":"),
83+
"-Djava.library.path=" + BuildUtils.javaLibraryPaths(rootDir).join(":"),
8884

8985
// Enable tracing downcalls (to Swift)
9086
"-Djextract.trace.downcalls=true"
9187
]
9288
}
9389

94-
task jextract(type: Exec) {
95-
description = "Extracts Java accessor sources using jextract"
96-
outputs.dir(layout.buildDirectory.dir("generated"))
97-
inputs.dir("$rootDir/Sources/ExampleSwiftLibrary") // monitored library
98-
99-
// any changes in the source generator sources also mean the resulting output might change
100-
inputs.dir("$rootDir/Sources/JExtractSwift")
101-
inputs.dir("$rootDir/Sources/JExtractSwiftTool")
102-
103-
workingDir = rootDir
104-
commandLine "make"
105-
args "jextract-generate"
106-
}
107-
108-
tasks.named("compileGeneratedJava").configure {
109-
dependsOn jextract
110-
}
90+
jmh {
91+
jvmArgsAppend = [
92+
"-Djava.library.path=" + BuildUtils.javaLibraryPaths(rootDir).join(":"),
93+
]
94+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package org.swift.swiftkit;
16+
17+
import java.util.concurrent.TimeUnit;
18+
19+
import org.openjdk.jmh.annotations.Benchmark;
20+
import org.openjdk.jmh.annotations.BenchmarkMode;
21+
import org.openjdk.jmh.annotations.Level;
22+
import org.openjdk.jmh.annotations.Mode;
23+
import org.openjdk.jmh.annotations.OutputTimeUnit;
24+
import org.openjdk.jmh.annotations.Scope;
25+
import org.openjdk.jmh.annotations.Setup;
26+
import org.openjdk.jmh.annotations.State;
27+
import org.openjdk.jmh.infra.Blackhole;
28+
29+
import com.example.swift.generated.MySwiftClass;
30+
31+
public class JavaToSwiftBenchmark {
32+
33+
@State(Scope.Benchmark)
34+
public static class BenchmarkState {
35+
MySwiftClass obj;
36+
37+
@Setup(Level.Trial)
38+
public void beforeALl() {
39+
System.loadLibrary("swiftCore");
40+
System.loadLibrary("ExampleSwiftLibrary");
41+
42+
// Tune down debug statements so they don't fill up stdout
43+
System.setProperty("jextract.trace.downcalls", "false");
44+
45+
obj = new MySwiftClass(1, 2);
46+
}
47+
}
48+
49+
@Benchmark @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS)
50+
public void simpleSwiftApiCall(BenchmarkState state, Blackhole blackhole) {
51+
blackhole.consume(state.obj.makeRandomIntMethod());
52+
}
53+
}

Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/MySwiftClassTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414

1515
package org.swift.swiftkit;
1616

17-
import com.example.swift.generated.MySwiftClass;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import org.junit.jupiter.api.BeforeAll;
1919
import org.junit.jupiter.api.Test;
20-
import org.junit.jupiter.api.condition.DisabledOnOs;
21-
import org.junit.jupiter.api.condition.OS;
2220

23-
import static org.junit.jupiter.api.Assertions.assertEquals;
24-
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import com.example.swift.generated.MySwiftClass;
2522

2623
public class MySwiftClassTest {
2724

Sources/ExampleSwiftLibrary/MySwiftLibrary.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public class MySwiftClass {
7878
p("make int -> 12")
7979
return 12
8080
}
81+
82+
public func makeRandomIntMethod() -> Int {
83+
return Int.random(in: 1..<256)
84+
}
8185
}
8286

8387
@_silgen_name("swift_getTypeByMangledNameInEnvironment")

0 commit comments

Comments
 (0)