|
5 | 5 | import javax.script.ScriptEngineManager; |
6 | 6 | import javax.script.ScriptEngine; |
7 | 7 | import javax.script.Invocable; |
8 | | -import java.io.File; |
9 | | -import java.io.FileReader; |
10 | 8 | import java.io.IOException; |
11 | | -import java.io.InputStreamReader; |
12 | 9 |
|
13 | 10 | /** |
14 | 11 | * Simple benchmark for Graal.js via GraalVM Polyglot Context and ScriptEngine. |
15 | 12 | */ |
16 | 13 | public class App { |
17 | 14 |
|
18 | | - public static final int WARMUP=10; |
19 | | - public static final int ITERATIONS=10; |
20 | | - public static final String BENCHFILE = "src/bench.js"; |
| 15 | + public static final int WARMUP = 10; |
| 16 | + public static final int ITERATIONS = 10; |
| 17 | + public static final String BENCHFILE = "src/bench.js"; |
21 | 18 |
|
22 | | - public static final String SOURCE = "" + |
23 | | - "var N = 2000;\n" + |
24 | | - "var EXPECTED = 17393;\n" + |
25 | | - "\n" + |
26 | | - "function Natural() {\n" + |
27 | | - " x = 2;\n" + |
28 | | - " return {\n" + |
29 | | - " 'next' : function() { return x++; }\n" + |
30 | | - " };\n" + |
31 | | - "}\n" + |
32 | | - "\n" + |
33 | | - "function Filter(number, filter) {\n" + |
34 | | - " var self = this;\n" + |
35 | | - " this.number = number;\n" + |
36 | | - " this.filter = filter;\n" + |
37 | | - " this.accept = function(n) {\n" + |
38 | | - " var filter = self;\n" + |
39 | | - " for (;;) {\n" + |
40 | | - " if (n % filter.number === 0) {\n" + |
41 | | - " return false;\n" + |
42 | | - " }\n" + |
43 | | - " filter = filter.filter;\n" + |
44 | | - " if (filter === null) {\n" + |
45 | | - " break;\n" + |
46 | | - " }\n" + |
47 | | - " }\n" + |
48 | | - " return true;\n" + |
49 | | - " };\n" + |
50 | | - " return this;\n" + |
51 | | - "}\n" + |
52 | | - "\n" + |
53 | | - "function Primes(natural) {\n" + |
54 | | - " var self = this;\n" + |
55 | | - " this.natural = natural;\n" + |
56 | | - " this.filter = null;\n" + |
57 | | - "\n" + |
58 | | - " this.next = function() {\n" + |
59 | | - " for (;;) {\n" + |
60 | | - " var n = self.natural.next();\n" + |
61 | | - " if (self.filter === null || self.filter.accept(n)) {\n" + |
62 | | - " self.filter = new Filter(n, self.filter);\n" + |
63 | | - " return n;\n" + |
64 | | - " }\n" + |
65 | | - " }\n" + |
66 | | - " };\n" + |
67 | | - "}\n" + |
68 | | - "\n" + |
69 | | - "function primesMain() {\n" + |
70 | | - " var primes = new Primes(Natural());\n" + |
71 | | - " var primArray = [];\n" + |
72 | | - " for (var i=0;i<=N;i++) { primArray.push(primes.next()); }\n" + |
73 | | - " if (primArray[N] != EXPECTED) { throw new Error('wrong prime found: '+primArray[N]); }\n" + |
74 | | - "}\n"; |
| 19 | + public static final String SOURCE = "" |
| 20 | + + "var N = 2000;\n" |
| 21 | + + "var EXPECTED = 17393;\n" |
| 22 | + + "\n" |
| 23 | + + "function Natural() {\n" |
| 24 | + + " x = 2;\n" |
| 25 | + + " return {\n" |
| 26 | + + " 'next' : function() { return x++; }\n" |
| 27 | + + " };\n" |
| 28 | + + "}\n" |
| 29 | + + "\n" |
| 30 | + + "function Filter(number, filter) {\n" |
| 31 | + + " var self = this;\n" |
| 32 | + + " this.number = number;\n" |
| 33 | + + " this.filter = filter;\n" |
| 34 | + + " this.accept = function(n) {\n" |
| 35 | + + " var filter = self;\n" |
| 36 | + + " for (;;) {\n" |
| 37 | + + " if (n % filter.number === 0) {\n" |
| 38 | + + " return false;\n" |
| 39 | + + " }\n" |
| 40 | + + " filter = filter.filter;\n" |
| 41 | + + " if (filter === null) {\n" |
| 42 | + + " break;\n" |
| 43 | + + " }\n" |
| 44 | + + " }\n" |
| 45 | + + " return true;\n" |
| 46 | + + " };\n" |
| 47 | + + " return this;\n" |
| 48 | + + "}\n" |
| 49 | + + "\n" |
| 50 | + + "function Primes(natural) {\n" |
| 51 | + + " var self = this;\n" |
| 52 | + + " this.natural = natural;\n" |
| 53 | + + " this.filter = null;\n" |
| 54 | + + "\n" |
| 55 | + + " this.next = function() {\n" |
| 56 | + + " for (;;) {\n" |
| 57 | + + " var n = self.natural.next();\n" |
| 58 | + + " if (self.filter === null || self.filter.accept(n)) {\n" |
| 59 | + + " self.filter = new Filter(n, self.filter);\n" |
| 60 | + + " return n;\n" |
| 61 | + + " }\n" |
| 62 | + + " }\n" |
| 63 | + + " };\n" |
| 64 | + + "}\n" |
| 65 | + + "\n" |
| 66 | + + "function primesMain() {\n" |
| 67 | + + " var primes = new Primes(Natural());\n" |
| 68 | + + " var primArray = [];\n" |
| 69 | + + " for (var i=0;i<=N;i++) { primArray.push(primes.next()); }\n" |
| 70 | + + " if (primArray[N] != EXPECTED) { throw new Error('wrong prime found: '+primArray[N]); }\n" |
| 71 | + + "}\n"; |
75 | 72 |
|
76 | | - public static void main(String[] args) throws Exception { |
| 73 | + public static void main(String[] args) throws Exception { |
77 | 74 | benchGraalPolyglotContext(); |
78 | 75 | benchGraalScriptEngine(); |
79 | 76 | benchNashornScriptEngine(); |
80 | 77 | } |
81 | 78 |
|
82 | | - private static void benchGraalPolyglotContext() throws IOException { |
83 | | - System.out.println("=== Graal.js via org.graalvm.polyglot.Context === "); |
84 | | - try (Context context = Context.create()) { |
| 79 | + private static void benchGraalPolyglotContext() throws IOException { |
| 80 | + System.out.println("=== Graal.js via org.graalvm.polyglot.Context === "); |
| 81 | + try (Context context = Context.create()) { |
85 | 82 | context.eval("js", SOURCE); |
86 | 83 | Value primesMain = context.getBindings("js").getMember("primesMain"); |
87 | 84 | System.out.println("warming up ..."); |
88 | | - for (int i=0;i<WARMUP;i++) { |
| 85 | + for (int i = 0; i < WARMUP; i++) { |
89 | 86 | primesMain.execute(); |
90 | 87 | } |
91 | 88 | System.out.println("warmup finished, now measuring"); |
92 | | - for (int i=0;i<ITERATIONS;i++) { |
| 89 | + for (int i = 0; i < ITERATIONS; i++) { |
93 | 90 | long start = System.currentTimeMillis(); |
94 | 91 | primesMain.execute(); |
95 | | - System.out.println("iteration: "+(System.currentTimeMillis()-start)); |
| 92 | + System.out.println("iteration: " + (System.currentTimeMillis() - start)); |
96 | 93 | } |
97 | | - } // context.close() is automatic |
98 | | - } |
| 94 | + } // context.close() is automatic |
| 95 | + } |
99 | 96 |
|
100 | | - private static void benchNashornScriptEngine() throws IOException { |
101 | | - System.out.println("=== Nashorn via javax.script.ScriptEngine ==="); |
| 97 | + private static void benchNashornScriptEngine() throws IOException { |
| 98 | + System.out.println("=== Nashorn via javax.script.ScriptEngine ==="); |
102 | 99 | ScriptEngine nashornEngine = new ScriptEngineManager().getEngineByName("nashorn"); |
103 | 100 | if (nashornEngine == null) { |
104 | 101 | System.out.println("*** Nashorn not found ***"); |
105 | 102 | } else { |
106 | 103 | benchScriptEngineIntl(nashornEngine); |
107 | 104 | } |
108 | | - } |
| 105 | + } |
109 | 106 |
|
110 | | - private static void benchGraalScriptEngine() throws IOException { |
111 | | - System.out.println("=== Graal.js via javax.script.ScriptEngine ==="); |
| 107 | + private static void benchGraalScriptEngine() throws IOException { |
| 108 | + System.out.println("=== Graal.js via javax.script.ScriptEngine ==="); |
112 | 109 | ScriptEngine graaljsEngine = new ScriptEngineManager().getEngineByName("graal.js"); |
113 | 110 | if (graaljsEngine == null) { |
114 | 111 | System.out.println("*** Graal.js not found ***"); |
115 | 112 | } else { |
116 | 113 | benchScriptEngineIntl(graaljsEngine); |
117 | 114 | } |
118 | | - } |
| 115 | + } |
119 | 116 |
|
120 | 117 | private static void benchScriptEngineIntl(ScriptEngine eng) throws IOException { |
121 | 118 | try { |
122 | | - eng.eval(SOURCE); |
123 | | - Invocable inv = (Invocable)eng; |
| 119 | + eng.eval(SOURCE); |
| 120 | + Invocable inv = (Invocable) eng; |
124 | 121 | System.out.println("warming up ..."); |
125 | | - for (int i=0;i<WARMUP;i++) { |
| 122 | + for (int i = 0; i < WARMUP; i++) { |
126 | 123 | inv.invokeFunction("primesMain"); |
127 | 124 | } |
128 | 125 | System.out.println("warmup finished, now measuring"); |
129 | | - for (int i=0;i<ITERATIONS;i++) { |
| 126 | + for (int i = 0; i < ITERATIONS; i++) { |
130 | 127 | long start = System.currentTimeMillis(); |
131 | 128 | inv.invokeFunction("primesMain"); |
132 | | - System.out.println("iteration: "+(System.currentTimeMillis()-start)); |
| 129 | + System.out.println("iteration: " + (System.currentTimeMillis() - start)); |
133 | 130 | } |
134 | 131 | } catch (Exception ex) { |
135 | 132 | System.out.println(ex); |
|
0 commit comments