Merge pull request #83 from jtulach/HotSpotBenchmark

Modifying the benchmark to run on HotSpot
This commit is contained in:
Alexey Andreev 2015-03-08 11:58:02 +03:00
commit 8b168b53eb
5 changed files with 334 additions and 33 deletions

View File

@ -0,0 +1,23 @@
TeaVM, GWT, HotSport JBox2D Benchmark
=====================================
Compares the speed of execution on a complex [JBox2D](http://www.jbox2d.org/) CPU extensive
computation. JavaScript produced by TeaVM and GWT can be compared by running
```
$ mvn clean install
```
then just open the generated HTML versions
```
$ open target/teavm-samples-benchmark-*-SNAPSHOT/index.html
```
In addition to that one can run the same demo with classical HotSpot virtual machine. Just try:
```
$ mvn -Pfx exec:java
```

View File

@ -33,16 +33,19 @@
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-jso</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-dom</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jbox2d</groupId>
@ -54,6 +57,7 @@
<artifactId>jbox2d-library</artifactId>
<version>2.2.1.1</version>
<classifier>sources</classifier>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
@ -61,6 +65,16 @@
<version>2.7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.dukescript.canvas</groupId>
<artifactId>html5-canvas</artifactId>
<version>0.7.2</version>
</dependency>
<dependency>
<groupId>org.netbeans.html</groupId>
<artifactId>net.java.html.boot</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
@ -76,6 +90,32 @@
</webResources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>org.teamvm.samples.benchmark.htmljava.BenchmarkStarter</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
<excludes>
<exclude>**/gwt/*</exclude>
<exclude>**/teavm/*</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
@ -91,7 +131,7 @@
<targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory>
<mainClass>org.teavm.samples.benchmark.teavm.BenchmarkStarter</mainClass>
<runtime>SEPARATE</runtime>
<minifying>true</minifying>
<minifying>false</minifying>
<debugInformationGenerated>true</debugInformationGenerated>
</configuration>
</execution>
@ -127,10 +167,47 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.netbeans.html</groupId>
<artifactId>html4j-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>process-js-annotations</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>fx</id>
<dependencies>
<dependency>
<groupId>org.netbeans.html</groupId>
<artifactId>net.java.html.boot.fx</artifactId>
<version>1.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<mainClass>org.teavm.samples.benchmark.htmljava.BenchmarkFX</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -0,0 +1,32 @@
/*
* Copyright 2015 Jaroslav Tulach.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.samples.benchmark.htmljava;
import net.java.html.boot.BrowserBuilder;
public final class BenchmarkFX {
private BenchmarkFX() {
}
public static void main(String... args) {
BrowserBuilder.newBrowser().loadPage("fx.html")
.loadClass(BenchmarkStarter.class)
.invoke("main")
.showAndWait();
System.exit(0);
}
}

View File

@ -0,0 +1,132 @@
/*
* Copyright 2015 Jaroslav Tulach.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.samples.benchmark.htmljava;
import com.dukescript.api.canvas.GraphicsContext2D;
import com.dukescript.api.canvas.Style;
import com.dukescript.canvas.html.HTML5Graphics;
import java.util.Timer;
import java.util.TimerTask;
import net.java.html.BrwsrCtx;
import net.java.html.js.JavaScriptBody;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.collision.shapes.ShapeType;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.Fixture;
import org.teavm.samples.benchmark.Scene;
public final class BenchmarkStarter {
private BenchmarkStarter() {
}
private static final Timer TIMER = new Timer("Make Step");
private static final Scene scene = new Scene();
private static double startMillisecond;
private static int currentSecond;
private static double timeSpentCalculating;
private static BrwsrCtx ctx;
public static void main(String[] args) {
startMillisecond = System.currentTimeMillis();
ctx = BrwsrCtx.findDefault(BenchmarkStarter.class);
makeStep();
}
static void makeStep() {
ctx.execute(new Runnable() {
@Override
public void run() {
makeStep0();
}
});
}
private static void makeStep0() {
double start = System.currentTimeMillis();
scene.calculate();
double end = System.currentTimeMillis();
int second = (int)((System.currentTimeMillis() - startMillisecond) / 1000);
if (second > currentSecond) {
publishResults(second, timeSpentCalculating);
timeSpentCalculating = 0;
currentSecond = second;
}
timeSpentCalculating += end - start;
render();
TIMER.schedule(new TimerTask() {
@Override
public void run() {
makeStep();
}
}, scene.timeUntilNextStep());
}
private static void render() {
GraphicsContext2D context = HTML5Graphics.getOrCreate("benchmark-canvas");
context.setFillStyle(new Style.Color("white"));
context.setStrokeStyle(new Style.Color("grey"));
context.fillRect(0, 0, 600, 600);
context.save();
context.translate(0, 600);
context.scale(1, -1);
context.scale(100, 100);
context.setLineWidth(0.01);
for (Body body = scene.getWorld().getBodyList(); body != null; body = body.getNext()) {
Vec2 center = body.getPosition();
context.save();
context.translate(center.x, center.y);
context.rotate(body.getAngle());
for (Fixture fixture = body.getFixtureList(); fixture != null; fixture = fixture.getNext()) {
Shape shape = fixture.getShape();
if (shape.getType() == ShapeType.CIRCLE) {
CircleShape circle = (CircleShape)shape;
context.beginPath();
context.arc(circle.m_p.x, circle.m_p.y, circle.getRadius(), 0, Math.PI * 2, true);
context.closePath();
context.stroke();
} else if (shape.getType() == ShapeType.POLYGON) {
PolygonShape poly = (PolygonShape)shape;
Vec2[] vertices = poly.getVertices();
context.beginPath();
context.moveTo(vertices[0].x, vertices[0].y);
for (int i = 1; i < poly.getVertexCount(); ++i) {
context.lineTo(vertices[i].x, vertices[i].y);
}
context.closePath();
context.stroke();
}
}
context.restore();
}
context.restore();
}
@JavaScriptBody(args = { "second", "timeSpentCalculating" }, body =
"var resultTableBody = document.getElementById('result-table-body');\n" +
"var row = document.createElement(\"tr\");\n" +
"resultTableBody.appendChild(row);\n" +
"var secondCell = document.createElement(\"td\");\n" +
"row.appendChild(secondCell);\n" +
"secondCell.appendChild(document.createTextNode(second));\n" +
"var timeCell = document.createElement(\"td\");\n" +
"row.appendChild(timeCell);\n" +
"timeCell.appendChild(document.createTextNode(timeSpentCalculating));\n"
)
private static native void publishResults(int second, double time);
}

View File

@ -0,0 +1,37 @@
<!--
Copyright 2014 Alexey Andreev.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>HotSpot/JavaFX jbox2d benchmark</title>
</head>
<body>
<h1>HotSpot/JavaFX performance</h1>
<div>
<canvas id="benchmark-canvas" width="600" height="600"></canvas>
</div>
<table>
<thead>
<tr>
<th>Second</th>
<th>Time spent computing, ms</th>
</tr>
</thead>
<tbody id="result-table-body">
</tbody>
</table>
</html>