Adding JBox2D sample into our benchmarks
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 30 Jan 2016 18:33:49 +0100
changeset 187791a30c1a8c4b
parent 1876 785891e2e2c6
child 1878 126d266b2da9
Adding JBox2D sample into our benchmarks
benchmarks/jbox2d-osgi/pom.xml
benchmarks/jbox2d/pom.xml
benchmarks/jbox2d/src/main/java/org/apidesign/benchmark/jbox2d/Scene.java
benchmarks/jbox2d/src/test/java/org/apidesign/benchmark/jbox2d/JBox2dSimulationTest.java
benchmarks/pom.xml
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/benchmarks/jbox2d-osgi/pom.xml	Sat Jan 30 18:33:49 2016 +0100
     1.3 @@ -0,0 +1,57 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     1.6 +    <modelVersion>4.0.0</modelVersion>
     1.7 +    <parent>
     1.8 +        <groupId>org.apidesign.bck2brwsr</groupId>
     1.9 +        <artifactId>benchmarks</artifactId>
    1.10 +        <version>1.0-SNAPSHOT</version>
    1.11 +    </parent>
    1.12 +    <packaging>bundle</packaging>
    1.13 +    <artifactId>jbox2d-osgi</artifactId>
    1.14 +    <properties>
    1.15 +        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    1.16 +        <maven.compiler.source>1.7</maven.compiler.source>
    1.17 +        <maven.compiler.target>1.7</maven.compiler.target>
    1.18 +        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    1.19 +        <export.packages>org.jbox2d.*</export.packages>
    1.20 +    </properties>
    1.21 +
    1.22 +    <dependencies>
    1.23 +        <dependency>
    1.24 +            <groupId>org.jbox2d</groupId>
    1.25 +            <artifactId>jbox2d-library</artifactId>
    1.26 +            <version>2.2.1.1</version>
    1.27 +            <scope>provided</scope>
    1.28 +        </dependency>
    1.29 +    </dependencies>
    1.30 +    <build>
    1.31 +        <plugins>
    1.32 +            <plugin>
    1.33 +                <groupId>org.apache.felix</groupId>
    1.34 +                <artifactId>maven-bundle-plugin</artifactId>
    1.35 +                <version>2.3.7</version>
    1.36 +                <configuration>
    1.37 +                    <instructions>
    1.38 +                        <Bundle-Name>${project.artifactId}</Bundle-Name>
    1.39 +                        <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
    1.40 +                        <Bundle-Description>OSGI version of ${project.name}</Bundle-Description>
    1.41 +                        <Export-Package>${export.packages}</Export-Package>
    1.42 +                    </instructions>
    1.43 +                </configuration>
    1.44 +                <extensions>true</extensions>
    1.45 +            </plugin>
    1.46 +            <plugin>
    1.47 +                <groupId>org.apidesign.bck2brwsr</groupId>
    1.48 +                <artifactId>bck2brwsr-maven-plugin</artifactId>
    1.49 +                <version>${project.version}</version>
    1.50 +                <executions>
    1.51 +                    <execution>
    1.52 +                        <goals>
    1.53 +                            <goal>library</goal>
    1.54 +                        </goals>
    1.55 +                    </execution>
    1.56 +                </executions>
    1.57 +            </plugin>
    1.58 +        </plugins>
    1.59 +    </build>
    1.60 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/benchmarks/jbox2d/pom.xml	Sat Jan 30 18:33:49 2016 +0100
     2.3 @@ -0,0 +1,119 @@
     2.4 +<?xml version="1.0"?>
     2.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     2.6 +  <modelVersion>4.0.0</modelVersion>
     2.7 +
     2.8 +  <groupId>org.apidesign.bck2brwsr</groupId>
     2.9 +  <artifactId>jbox2d</artifactId>
    2.10 +  <version>1.0-SNAPSHOT</version>
    2.11 +  <packaging>jar</packaging>
    2.12 +  <parent>
    2.13 +    <artifactId>benchmarks</artifactId>
    2.14 +    <groupId>org.apidesign.bck2brwsr</groupId>
    2.15 +    <version>1.0-SNAPSHOT</version>
    2.16 +  </parent>
    2.17 +
    2.18 +  <name>JBox2D in Action</name>
    2.19 +
    2.20 +  <properties>
    2.21 +    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    2.22 +  </properties>
    2.23 +
    2.24 +  <build>
    2.25 +      <plugins>
    2.26 +          <plugin>
    2.27 +            <groupId>org.apache.maven.plugins</groupId>
    2.28 +            <artifactId>maven-compiler-plugin</artifactId>
    2.29 +            <version>2.3.2</version>
    2.30 +            <configuration>
    2.31 +               <source>1.7</source>
    2.32 +               <target>1.7</target>
    2.33 +            </configuration>
    2.34 +          </plugin>
    2.35 +          <plugin>
    2.36 +              <groupId>org.apache.maven.plugins</groupId>
    2.37 +              <artifactId>maven-deploy-plugin</artifactId>
    2.38 +              <version>2.7</version>
    2.39 +              <configuration>
    2.40 +                  <skip>true</skip>
    2.41 +              </configuration>
    2.42 +          </plugin>      
    2.43 +          <plugin>
    2.44 +              <groupId>org.apache.maven.plugins</groupId>
    2.45 +              <artifactId>maven-surefire-plugin</artifactId>
    2.46 +              <configuration>
    2.47 +                  <systemPropertyVariables>
    2.48 +                      <vmtest.js>brwsr</vmtest.js>
    2.49 +                  </systemPropertyVariables>
    2.50 +              </configuration>
    2.51 +          </plugin>
    2.52 +          <plugin>
    2.53 +              <groupId>org.apidesign.bck2brwsr</groupId>
    2.54 +              <artifactId>bck2brwsr-maven-plugin</artifactId>
    2.55 +              <version>${project.version}</version>
    2.56 +              <executions>
    2.57 +                  <execution>
    2.58 +                      <goals>
    2.59 +                          <goal>library</goal>
    2.60 +                      </goals>
    2.61 +                  </execution>
    2.62 +              </executions>
    2.63 +              <configuration>
    2.64 +                  <aotDeps>
    2.65 +                      <aotDep>org.jbox2d:*</aotDep>
    2.66 +                  </aotDeps>
    2.67 +                  <minified>false</minified>
    2.68 +                  <debug>false</debug>
    2.69 +              </configuration>
    2.70 +          </plugin>
    2.71 +      </plugins>
    2.72 +  </build>
    2.73 +  
    2.74 +  <dependencies>
    2.75 +    <dependency>
    2.76 +        <groupId>org.apidesign.bck2brwsr</groupId>
    2.77 +        <artifactId>emul</artifactId>
    2.78 +        <version>${project.version}</version>
    2.79 +        <classifier>bck2brwsr</classifier>
    2.80 +    </dependency>
    2.81 +    <dependency>
    2.82 +      <groupId>org.apidesign.bck2brwsr</groupId>
    2.83 +      <artifactId>emul</artifactId>
    2.84 +      <version>${project.version}</version>
    2.85 +      <classifier>rt</classifier>
    2.86 +    </dependency>
    2.87 +    <dependency>
    2.88 +      <groupId>org.testng</groupId>
    2.89 +      <artifactId>testng</artifactId>
    2.90 +      <scope>test</scope>
    2.91 +      <exclusions>
    2.92 +        <exclusion>
    2.93 +          <artifactId>junit</artifactId>
    2.94 +          <groupId>junit</groupId>
    2.95 +        </exclusion>
    2.96 +      </exclusions>
    2.97 +    </dependency>
    2.98 +    <dependency>
    2.99 +      <groupId>org.apidesign.bck2brwsr</groupId>
   2.100 +      <artifactId>vmtest</artifactId>
   2.101 +      <version>${project.version}</version>
   2.102 +      <scope>test</scope>
   2.103 +    </dependency>
   2.104 +    <dependency>
   2.105 +      <groupId>org.apidesign.bck2brwsr</groupId>
   2.106 +      <artifactId>launcher.http</artifactId>
   2.107 +      <version>${project.version}</version>
   2.108 +      <scope>test</scope>
   2.109 +    </dependency>
   2.110 +    <dependency>
   2.111 +      <groupId>org.apidesign.bck2brwsr</groupId>
   2.112 +      <artifactId>jbox2d-2.1.1.1-osgi</artifactId>
   2.113 +      <version>${project.version}</version>
   2.114 +      <exclusions>
   2.115 +        <exclusion>
   2.116 +          <groupId>org.jbox2d</groupId>
   2.117 +          <artifactId>jbox2d-library</artifactId>
   2.118 +        </exclusion>
   2.119 +      </exclusions>
   2.120 +    </dependency>
   2.121 +  </dependencies>
   2.122 +</project>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/benchmarks/jbox2d/src/main/java/org/apidesign/benchmark/jbox2d/Scene.java	Sat Jan 30 18:33:49 2016 +0100
     3.3 @@ -0,0 +1,178 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.benchmark.jbox2d;
    3.22 +
    3.23 +/*
    3.24 + *  Copyright 2014 Alexey Andreev.
    3.25 + *
    3.26 + *  Licensed under the Apache License, Version 2.0 (the "License");
    3.27 + *  you may not use this file except in compliance with the License.
    3.28 + *  You may obtain a copy of the License at
    3.29 + *
    3.30 + *       http://www.apache.org/licenses/LICENSE-2.0
    3.31 + *
    3.32 + *  Unless required by applicable law or agreed to in writing, software
    3.33 + *  distributed under the License is distributed on an "AS IS" BASIS,
    3.34 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    3.35 + *  See the License for the specific language governing permissions and
    3.36 + *  limitations under the License.
    3.37 + */
    3.38 +
    3.39 +import org.jbox2d.collision.shapes.CircleShape;
    3.40 +import org.jbox2d.collision.shapes.PolygonShape;
    3.41 +import org.jbox2d.common.Vec2;
    3.42 +import org.jbox2d.dynamics.*;
    3.43 +import org.jbox2d.dynamics.joints.RevoluteJointDef;
    3.44 +
    3.45 +/** Taken from TeaVM.
    3.46 + * <a href="https://github.com/konsoletyper/teavm/blob/3341df3668bbe3f0c3ed78b621e311361c88acfc/samples/benchmark/src/main/java/org/teavm/samples/benchmark/Scene.java">Sample</a>
    3.47 + * is licensed under Apache license.
    3.48 + *
    3.49 + * @author Alexey Andreev
    3.50 + */
    3.51 +public class Scene {
    3.52 +    private World world;
    3.53 +    private Body axis;
    3.54 +    private Body reel;
    3.55 +    private long lastCalculated;
    3.56 +    private long startTime;
    3.57 +
    3.58 +    public Scene() {
    3.59 +        world = new World(new Vec2(0, -9.8f));
    3.60 +        initAxis();
    3.61 +        initReel();
    3.62 +        joinReelToAxis();
    3.63 +        initBalls();
    3.64 +        lastCalculated = System.currentTimeMillis();
    3.65 +        startTime = lastCalculated;
    3.66 +    }
    3.67 +
    3.68 +    private void initAxis() {
    3.69 +        BodyDef axisDef = new BodyDef();
    3.70 +        axisDef.type = BodyType.STATIC;
    3.71 +        axisDef.position = new Vec2(3, 3);
    3.72 +        axis = world.createBody(axisDef);
    3.73 +
    3.74 +        CircleShape axisShape = new CircleShape();
    3.75 +        axisShape.setRadius(0.02f);
    3.76 +        axisShape.m_p.set(0, 0);
    3.77 +
    3.78 +        FixtureDef axisFixture = new FixtureDef();
    3.79 +        axisFixture.shape = axisShape;
    3.80 +        axis.createFixture(axisFixture);
    3.81 +    }
    3.82 +
    3.83 +    private void initReel() {
    3.84 +        BodyDef reelDef = new BodyDef();
    3.85 +        reelDef.type = BodyType.DYNAMIC;
    3.86 +        reelDef.position = new Vec2(3, 3);
    3.87 +        reel = world.createBody(reelDef);
    3.88 +
    3.89 +        FixtureDef fixture = new FixtureDef();
    3.90 +        fixture.friction = 0.5f;
    3.91 +        fixture.restitution = 0.4f;
    3.92 +        fixture.density = 1;
    3.93 +
    3.94 +        int parts = 30;
    3.95 +        for (int i = 0; i < parts; ++i) {
    3.96 +            PolygonShape shape = new PolygonShape();
    3.97 +            double angle1 = i / (double) parts * 2 * Math.PI;
    3.98 +            double x1 = 2.7 * Math.cos(angle1);
    3.99 +            double y1 = 2.7 * Math.sin(angle1);
   3.100 +            double angle2 = (i + 1) / (double) parts * 2 * Math.PI;
   3.101 +            double x2 = 2.7 * Math.cos(angle2);
   3.102 +            double y2 = 2.7 * Math.sin(angle2);
   3.103 +            double angle = (angle1 + angle2) / 2;
   3.104 +            double x = 0.01 * Math.cos(angle);
   3.105 +            double y = 0.01 * Math.sin(angle);
   3.106 +
   3.107 +            shape.set(new Vec2[] { new Vec2((float) x1, (float) y1), new Vec2((float) x2, (float) y2),
   3.108 +                    new Vec2((float) (x2 - x), (float) (y2 - y)), new Vec2((float) (x1 - x), (float) (y1 - y)) }, 4);
   3.109 +            fixture.shape = shape;
   3.110 +            reel.createFixture(fixture);
   3.111 +        }
   3.112 +    }
   3.113 +
   3.114 +    private void initBalls() {
   3.115 +        float ballRadius = 0.15f;
   3.116 +
   3.117 +        BodyDef ballDef = new BodyDef();
   3.118 +        ballDef.type = BodyType.DYNAMIC;
   3.119 +        FixtureDef fixtureDef = new FixtureDef();
   3.120 +        fixtureDef.friction = 0.3f;
   3.121 +        fixtureDef.restitution = 0.3f;
   3.122 +        fixtureDef.density = 0.2f;
   3.123 +        CircleShape shape = new CircleShape();
   3.124 +        shape.m_radius = ballRadius;
   3.125 +        fixtureDef.shape = shape;
   3.126 +
   3.127 +        for (int i = 0; i < 5; ++i) {
   3.128 +            for (int j = 0; j < 5; ++j) {
   3.129 +                float x = (j + 0.5f) * (ballRadius * 2 + 0.01f);
   3.130 +                float y = (i + 0.5f) * (ballRadius * 2 + 0.01f);
   3.131 +                ballDef.position.x = 3 + x;
   3.132 +                ballDef.position.y = 3 + y;
   3.133 +                Body body = world.createBody(ballDef);
   3.134 +                body.createFixture(fixtureDef);
   3.135 +
   3.136 +                ballDef.position.x = 3 - x;
   3.137 +                ballDef.position.y = 3 + y;
   3.138 +                body = world.createBody(ballDef);
   3.139 +                body.createFixture(fixtureDef);
   3.140 +
   3.141 +                ballDef.position.x = 3 + x;
   3.142 +                ballDef.position.y = 3 - y;
   3.143 +                body = world.createBody(ballDef);
   3.144 +                body.createFixture(fixtureDef);
   3.145 +
   3.146 +                ballDef.position.x = 3 - x;
   3.147 +                ballDef.position.y = 3 - y;
   3.148 +                body = world.createBody(ballDef);
   3.149 +                body.createFixture(fixtureDef);
   3.150 +            }
   3.151 +        }
   3.152 +    }
   3.153 +
   3.154 +    private void joinReelToAxis() {
   3.155 +        RevoluteJointDef jointDef = new RevoluteJointDef();
   3.156 +        jointDef.bodyA = axis;
   3.157 +        jointDef.bodyB = reel;
   3.158 +        world.createJoint(jointDef);
   3.159 +    }
   3.160 +
   3.161 +    public void calculate() {
   3.162 +        long currentTime = System.currentTimeMillis();
   3.163 +        int timeToCalculate = (int) (currentTime - lastCalculated);
   3.164 +        long relativeTime = currentTime - startTime;
   3.165 +        while (timeToCalculate > 10) {
   3.166 +            int period = (int) ((relativeTime + 5000) / 10000);
   3.167 +            reel.applyTorque(period % 2 == 0 ? 8f : -8f);
   3.168 +            world.step(0.01f, 20, 40);
   3.169 +            lastCalculated += 10;
   3.170 +            timeToCalculate -= 10;
   3.171 +        }
   3.172 +    }
   3.173 +
   3.174 +    public int timeUntilNextStep() {
   3.175 +        return (int) Math.max(0, lastCalculated + 10 - System.currentTimeMillis());
   3.176 +    }
   3.177 +
   3.178 +    public World getWorld() {
   3.179 +        return world;
   3.180 +    }
   3.181 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/benchmarks/jbox2d/src/test/java/org/apidesign/benchmark/jbox2d/JBox2dSimulationTest.java	Sat Jan 30 18:33:49 2016 +0100
     4.3 @@ -0,0 +1,42 @@
     4.4 +/**
     4.5 + * Back 2 Browser Bytecode Translator
     4.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details.
    4.16 + *
    4.17 + * You should have received a copy of the GNU General Public License
    4.18 + * along with this program. Look for COPYING file in the top folder.
    4.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    4.20 + */
    4.21 +package org.apidesign.benchmark.jbox2d;
    4.22 +
    4.23 +import java.io.IOException;
    4.24 +import org.apidesign.bck2brwsr.vmtest.Compare;
    4.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    4.26 +import org.testng.annotations.Factory;
    4.27 +
    4.28 +public class JBox2dSimulationTest {
    4.29 +    public JBox2dSimulationTest() {
    4.30 +    }
    4.31 +
    4.32 +    @Compare(slowdown = 40.0)
    4.33 +    public int tenThousand() throws IOException {
    4.34 +        Scene s = new Scene();
    4.35 +        for (int i = 0; i < 10000; i++) {
    4.36 +            s.calculate();
    4.37 +        }
    4.38 +        return s.getWorld().getBodyCount();
    4.39 +    }
    4.40 +
    4.41 +    @Factory
    4.42 +    public static Object[] create() {
    4.43 +        return VMTest.create(JBox2dSimulationTest.class);
    4.44 +    }
    4.45 +}
     5.1 --- a/benchmarks/pom.xml	Wed Jan 27 23:33:22 2016 +0100
     5.2 +++ b/benchmarks/pom.xml	Sat Jan 30 18:33:49 2016 +0100
     5.3 @@ -13,5 +13,7 @@
     5.4    <name>Performance benchmarks</name>
     5.5    <modules>
     5.6      <module>sieve</module>
     5.7 +    <module>jbox2d</module>
     5.8 +    <module>jbox2d-osgi</module>
     5.9    </modules>
    5.10  </project>