Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Commit 2f851ca

Browse files
authored
Merge pull request #4 from JaroslavTulach/UnitTestingWithGraal
Run the benchmarks in the unit test
2 parents a1b78b0 + a138dcb commit 2f851ca

File tree

3 files changed

+70
-33
lines changed

3 files changed

+70
-33
lines changed

pom.xml

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@
99
<url>http://maven.apache.org</url>
1010
<properties>
1111
<graalvm.version>1.0.0-rc8</graalvm.version>
12+
<compiler.dir>${project.build.directory}/compiler</compiler.dir>
1213
</properties>
1314
<build>
1415
<pluginManagement>
1516
<plugins>
1617
<plugin>
1718
<artifactId>maven-compiler-plugin</artifactId>
18-
<version>3.7.0</version>
19+
<version>3.8.0</version>
1920
<configuration>
20-
<source>1.10</source>
21-
<target>1.10</target>
21+
<source>11</source>
22+
<target>11</target>
2223
</configuration>
2324
</plugin>
2425
<plugin>
2526
<artifactId>maven-surefire-plugin</artifactId>
26-
<version>2.21.0</version>
27+
<version>2.22.1</version>
2728
<configuration>
28-
<jvm>${JAVA_HOME}/bin/java</jvm>
29-
<includes>
30-
<include>**/*Test.java</include>
31-
</includes>
29+
<argLine>-XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI --module-path=${compiler.dir} --upgrade-module-path=${compiler.dir}/compiler.jar</argLine>
3230
</configuration>
3331
</plugin>
3432
<plugin>
@@ -38,7 +36,7 @@
3836
<executions>
3937
<execution>
4038
<id>copy</id>
41-
<phase>package</phase>
39+
<phase>process-test-classes</phase>
4240
<goals>
4341
<goal>copy</goal>
4442
</goals>
@@ -50,10 +48,26 @@
5048
<version>${graalvm.version}</version>
5149
<type>jar</type>
5250
<overWrite>true</overWrite>
53-
<destFileName>compiler-${graalvm.version}.jar</destFileName>
51+
<destFileName>compiler.jar</destFileName>
52+
</artifactItem>
53+
<artifactItem>
54+
<groupId>org.graalvm.truffle</groupId>
55+
<artifactId>truffle-api</artifactId>
56+
<version>${graalvm.version}</version>
57+
<type>jar</type>
58+
<overWrite>true</overWrite>
59+
<destFileName>truffle-api.jar</destFileName>
60+
</artifactItem>
61+
<artifactItem>
62+
<groupId>org.graalvm.sdk</groupId>
63+
<artifactId>graal-sdk</artifactId>
64+
<version>${graalvm.version}</version>
65+
<type>jar</type>
66+
<overWrite>true</overWrite>
67+
<destFileName>graal-sdk.jar</destFileName>
5468
</artifactItem>
5569
</artifactItems>
56-
<outputDirectory>${project.build.directory}</outputDirectory>
70+
<outputDirectory>${compiler.dir}</outputDirectory>
5771
</configuration>
5872
</execution>
5973
</executions>
@@ -78,7 +92,7 @@
7892
<classpath/>
7993
<argument>-XX:+UnlockExperimentalVMOptions</argument>
8094
<argument>-XX:+EnableJVMCI</argument>
81-
<argument>--upgrade-module-path=${project.build.directory}/compiler-${graalvm.version}.jar</argument>
95+
<argument>--upgrade-module-path=${compiler.dir}/compiler.jar</argument>
8296
<argument>com.mycompany.app.App</argument>
8397
</arguments>
8498
</configuration>
@@ -125,6 +139,12 @@
125139
<artifactId>graal-sdk</artifactId>
126140
<version>${graalvm.version}</version>
127141
</dependency>
142+
<dependency>
143+
<groupId>org.graalvm.truffle</groupId>
144+
<artifactId>truffle-api</artifactId>
145+
<version>${graalvm.version}</version>
146+
<scope>runtime</scope>
147+
</dependency>
128148
<dependency>
129149
<groupId>org.graalvm.js</groupId>
130150
<artifactId>js</artifactId>

src/main/java/com/mycompany/app/App.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ public static void main(String[] args) throws Exception {
7676
benchNashornScriptEngine();
7777
}
7878

79-
private static void benchGraalPolyglotContext() throws IOException {
79+
static long benchGraalPolyglotContext() throws IOException {
8080
System.out.println("=== Graal.js via org.graalvm.polyglot.Context === ");
81+
long took = 0;
8182
try (Context context = Context.create()) {
8283
context.eval("js", SOURCE);
8384
Value primesMain = context.getBindings("js").getMember("primesMain");
@@ -89,32 +90,37 @@ private static void benchGraalPolyglotContext() throws IOException {
8990
for (int i = 0; i < ITERATIONS; i++) {
9091
long start = System.currentTimeMillis();
9192
primesMain.execute();
92-
System.out.println("iteration: " + (System.currentTimeMillis() - start));
93+
took = System.currentTimeMillis() - start;
94+
System.out.println("iteration: " + took);
9395
}
9496
} // context.close() is automatic
97+
return took;
9598
}
9699

97-
private static void benchNashornScriptEngine() throws IOException {
100+
static long benchNashornScriptEngine() throws IOException {
98101
System.out.println("=== Nashorn via javax.script.ScriptEngine ===");
99102
ScriptEngine nashornEngine = new ScriptEngineManager().getEngineByName("nashorn");
100103
if (nashornEngine == null) {
101104
System.out.println("*** Nashorn not found ***");
105+
return 0;
102106
} else {
103-
benchScriptEngineIntl(nashornEngine);
107+
return benchScriptEngineIntl(nashornEngine);
104108
}
105109
}
106110

107-
private static void benchGraalScriptEngine() throws IOException {
111+
static long benchGraalScriptEngine() throws IOException {
108112
System.out.println("=== Graal.js via javax.script.ScriptEngine ===");
109113
ScriptEngine graaljsEngine = new ScriptEngineManager().getEngineByName("graal.js");
110114
if (graaljsEngine == null) {
111115
System.out.println("*** Graal.js not found ***");
116+
return 0;
112117
} else {
113-
benchScriptEngineIntl(graaljsEngine);
118+
return benchScriptEngineIntl(graaljsEngine);
114119
}
115120
}
116121

117-
private static void benchScriptEngineIntl(ScriptEngine eng) throws IOException {
122+
private static long benchScriptEngineIntl(ScriptEngine eng) throws IOException {
123+
long took = 0L;
118124
try {
119125
eng.eval(SOURCE);
120126
Invocable inv = (Invocable) eng;
@@ -126,14 +132,13 @@ private static void benchScriptEngineIntl(ScriptEngine eng) throws IOException {
126132
for (int i = 0; i < ITERATIONS; i++) {
127133
long start = System.currentTimeMillis();
128134
inv.invokeFunction("primesMain");
129-
System.out.println("iteration: " + (System.currentTimeMillis() - start));
135+
took = System.currentTimeMillis() - start;
136+
System.out.println("iteration: " + (took));
130137
}
131138
} catch (Exception ex) {
132139
System.out.println(ex);
133140
}
141+
return took;
134142
}
135143

136-
public static Value get42() {
137-
return Context.create().eval("js", "42");
138-
}
139144
}
Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
package com.mycompany.app;
22

3-
import org.junit.Assert;
3+
import static org.junit.Assert.fail;
4+
import org.junit.BeforeClass;
45
import org.junit.Test;
56

6-
/**
7-
* Unit test for simple App.
8-
*/
97
public class AppTest {
10-
/**
11-
* Rigourous Test :-)
12-
*/
8+
private static long nashorn;
9+
10+
@BeforeClass
11+
public static void nashornBenchmark() throws Exception {
12+
nashorn = App.benchNashornScriptEngine();
13+
}
14+
15+
@Test
16+
public void testGraalPolyglotSpeed() throws Exception {
17+
long graalJS = App.benchGraalPolyglotContext();
18+
if (nashorn < graalJS) {
19+
fail(String.format("Graal.js (%d ms) should be faster than Nashorn (%d ms).", graalJS, nashorn));
20+
}
21+
}
22+
1323
@Test
14-
public void testApp()
15-
{
16-
Assert.assertTrue(App.get42().isNumber());
24+
public void testGraalScriptEngineSpeed() throws Exception {
25+
long graalJS = App.benchGraalScriptEngine();
26+
if (nashorn < graalJS) {
27+
fail(String.format("Graal.js (%d ms) should be faster than Nashorn (%d ms).", graalJS, nashorn));
28+
}
1729
}
1830
}

0 commit comments

Comments
 (0)