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

Commit aa949bc

Browse files
committed
Run the benchmarks in the unit test
1 parent 8c2eb12 commit aa949bc

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

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+
private 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: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package com.mycompany.app;
22

3-
import org.junit.Assert;
3+
import static org.junit.Assert.fail;
44
import org.junit.Test;
55

6-
/**
7-
* Unit test for simple App.
8-
*/
96
public class AppTest {
10-
/**
11-
* Rigourous Test :-)
12-
*/
137
@Test
14-
public void testApp()
15-
{
16-
Assert.assertTrue(App.get42().isNumber());
8+
public void testSpeed() throws Exception {
9+
long nashorn = App.benchNashornScriptEngine();
10+
long graalJS = App.benchGraalPolyglotContext();
11+
if (nashorn < graalJS) {
12+
fail("Graal.js should be five times faster. Nashorn " + nashorn + " ms. Graal.js " + graalJS + " ms");
13+
}
1714
}
1815
}

0 commit comments

Comments
 (0)