# HG changeset patch # User Jaroslav Tulach # Date 1454175229 -3600 # Node ID 91a30c1a8c4b17a2693bd6d7b5223957d82c0450 # Parent 785891e2e2c6f6566b99975eb2ebc74b34dfff70 Adding JBox2D sample into our benchmarks diff -r 785891e2e2c6 -r 91a30c1a8c4b benchmarks/jbox2d-osgi/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/jbox2d-osgi/pom.xml Sat Jan 30 18:33:49 2016 +0100 @@ -0,0 +1,57 @@ + + + 4.0.0 + + org.apidesign.bck2brwsr + benchmarks + 1.0-SNAPSHOT + + bundle + jbox2d-osgi + + UTF-8 + 1.7 + 1.7 + UTF-8 + org.jbox2d.* + + + + + org.jbox2d + jbox2d-library + 2.2.1.1 + provided + + + + + + org.apache.felix + maven-bundle-plugin + 2.3.7 + + + ${project.artifactId} + ${project.groupId}.${project.artifactId} + OSGI version of ${project.name} + ${export.packages} + + + true + + + org.apidesign.bck2brwsr + bck2brwsr-maven-plugin + ${project.version} + + + + library + + + + + + + diff -r 785891e2e2c6 -r 91a30c1a8c4b benchmarks/jbox2d/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/jbox2d/pom.xml Sat Jan 30 18:33:49 2016 +0100 @@ -0,0 +1,119 @@ + + + 4.0.0 + + org.apidesign.bck2brwsr + jbox2d + 1.0-SNAPSHOT + jar + + benchmarks + org.apidesign.bck2brwsr + 1.0-SNAPSHOT + + + JBox2D in Action + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.7 + + true + + + + org.apache.maven.plugins + maven-surefire-plugin + + + brwsr + + + + + org.apidesign.bck2brwsr + bck2brwsr-maven-plugin + ${project.version} + + + + library + + + + + + org.jbox2d:* + + false + false + + + + + + + + org.apidesign.bck2brwsr + emul + ${project.version} + bck2brwsr + + + org.apidesign.bck2brwsr + emul + ${project.version} + rt + + + org.testng + testng + test + + + junit + junit + + + + + org.apidesign.bck2brwsr + vmtest + ${project.version} + test + + + org.apidesign.bck2brwsr + launcher.http + ${project.version} + test + + + org.apidesign.bck2brwsr + jbox2d-2.1.1.1-osgi + ${project.version} + + + org.jbox2d + jbox2d-library + + + + + diff -r 785891e2e2c6 -r 91a30c1a8c4b benchmarks/jbox2d/src/main/java/org/apidesign/benchmark/jbox2d/Scene.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/jbox2d/src/main/java/org/apidesign/benchmark/jbox2d/Scene.java Sat Jan 30 18:33:49 2016 +0100 @@ -0,0 +1,178 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012-2015 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.benchmark.jbox2d; + +/* + * 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. + */ + +import org.jbox2d.collision.shapes.CircleShape; +import org.jbox2d.collision.shapes.PolygonShape; +import org.jbox2d.common.Vec2; +import org.jbox2d.dynamics.*; +import org.jbox2d.dynamics.joints.RevoluteJointDef; + +/** Taken from TeaVM. + * Sample + * is licensed under Apache license. + * + * @author Alexey Andreev + */ +public class Scene { + private World world; + private Body axis; + private Body reel; + private long lastCalculated; + private long startTime; + + public Scene() { + world = new World(new Vec2(0, -9.8f)); + initAxis(); + initReel(); + joinReelToAxis(); + initBalls(); + lastCalculated = System.currentTimeMillis(); + startTime = lastCalculated; + } + + private void initAxis() { + BodyDef axisDef = new BodyDef(); + axisDef.type = BodyType.STATIC; + axisDef.position = new Vec2(3, 3); + axis = world.createBody(axisDef); + + CircleShape axisShape = new CircleShape(); + axisShape.setRadius(0.02f); + axisShape.m_p.set(0, 0); + + FixtureDef axisFixture = new FixtureDef(); + axisFixture.shape = axisShape; + axis.createFixture(axisFixture); + } + + private void initReel() { + BodyDef reelDef = new BodyDef(); + reelDef.type = BodyType.DYNAMIC; + reelDef.position = new Vec2(3, 3); + reel = world.createBody(reelDef); + + FixtureDef fixture = new FixtureDef(); + fixture.friction = 0.5f; + fixture.restitution = 0.4f; + fixture.density = 1; + + int parts = 30; + for (int i = 0; i < parts; ++i) { + PolygonShape shape = new PolygonShape(); + double angle1 = i / (double) parts * 2 * Math.PI; + double x1 = 2.7 * Math.cos(angle1); + double y1 = 2.7 * Math.sin(angle1); + double angle2 = (i + 1) / (double) parts * 2 * Math.PI; + double x2 = 2.7 * Math.cos(angle2); + double y2 = 2.7 * Math.sin(angle2); + double angle = (angle1 + angle2) / 2; + double x = 0.01 * Math.cos(angle); + double y = 0.01 * Math.sin(angle); + + shape.set(new Vec2[] { new Vec2((float) x1, (float) y1), new Vec2((float) x2, (float) y2), + new Vec2((float) (x2 - x), (float) (y2 - y)), new Vec2((float) (x1 - x), (float) (y1 - y)) }, 4); + fixture.shape = shape; + reel.createFixture(fixture); + } + } + + private void initBalls() { + float ballRadius = 0.15f; + + BodyDef ballDef = new BodyDef(); + ballDef.type = BodyType.DYNAMIC; + FixtureDef fixtureDef = new FixtureDef(); + fixtureDef.friction = 0.3f; + fixtureDef.restitution = 0.3f; + fixtureDef.density = 0.2f; + CircleShape shape = new CircleShape(); + shape.m_radius = ballRadius; + fixtureDef.shape = shape; + + for (int i = 0; i < 5; ++i) { + for (int j = 0; j < 5; ++j) { + float x = (j + 0.5f) * (ballRadius * 2 + 0.01f); + float y = (i + 0.5f) * (ballRadius * 2 + 0.01f); + ballDef.position.x = 3 + x; + ballDef.position.y = 3 + y; + Body body = world.createBody(ballDef); + body.createFixture(fixtureDef); + + ballDef.position.x = 3 - x; + ballDef.position.y = 3 + y; + body = world.createBody(ballDef); + body.createFixture(fixtureDef); + + ballDef.position.x = 3 + x; + ballDef.position.y = 3 - y; + body = world.createBody(ballDef); + body.createFixture(fixtureDef); + + ballDef.position.x = 3 - x; + ballDef.position.y = 3 - y; + body = world.createBody(ballDef); + body.createFixture(fixtureDef); + } + } + } + + private void joinReelToAxis() { + RevoluteJointDef jointDef = new RevoluteJointDef(); + jointDef.bodyA = axis; + jointDef.bodyB = reel; + world.createJoint(jointDef); + } + + public void calculate() { + long currentTime = System.currentTimeMillis(); + int timeToCalculate = (int) (currentTime - lastCalculated); + long relativeTime = currentTime - startTime; + while (timeToCalculate > 10) { + int period = (int) ((relativeTime + 5000) / 10000); + reel.applyTorque(period % 2 == 0 ? 8f : -8f); + world.step(0.01f, 20, 40); + lastCalculated += 10; + timeToCalculate -= 10; + } + } + + public int timeUntilNextStep() { + return (int) Math.max(0, lastCalculated + 10 - System.currentTimeMillis()); + } + + public World getWorld() { + return world; + } +} diff -r 785891e2e2c6 -r 91a30c1a8c4b benchmarks/jbox2d/src/test/java/org/apidesign/benchmark/jbox2d/JBox2dSimulationTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/jbox2d/src/test/java/org/apidesign/benchmark/jbox2d/JBox2dSimulationTest.java Sat Jan 30 18:33:49 2016 +0100 @@ -0,0 +1,42 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012-2015 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.benchmark.jbox2d; + +import java.io.IOException; +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +public class JBox2dSimulationTest { + public JBox2dSimulationTest() { + } + + @Compare(slowdown = 40.0) + public int tenThousand() throws IOException { + Scene s = new Scene(); + for (int i = 0; i < 10000; i++) { + s.calculate(); + } + return s.getWorld().getBodyCount(); + } + + @Factory + public static Object[] create() { + return VMTest.create(JBox2dSimulationTest.class); + } +} diff -r 785891e2e2c6 -r 91a30c1a8c4b benchmarks/pom.xml --- a/benchmarks/pom.xml Wed Jan 27 23:33:22 2016 +0100 +++ b/benchmarks/pom.xml Sat Jan 30 18:33:49 2016 +0100 @@ -13,5 +13,7 @@ Performance benchmarks sieve + jbox2d + jbox2d-osgi