The sieve benchmark makes the testing more real than the fake matrix multiplication
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 24 Jan 2016 12:08:45 +0100
changeset 1854826eb936c9a8
parent 1853 42c6e5a05360
child 1855 34efbdde4eca
child 1950 71e5cd5b29bc
The sieve benchmark makes the testing more real than the fake matrix multiplication
benchmarks/matrix-multiplication/pom.xml
benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java
benchmarks/matrix-multiplication/src/main/select-time.xsl
benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java
benchmarks/pom.xml
benchmarks/sieve/pom.xml
benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Filter.java
benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Natural.java
benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Primes.java
benchmarks/sieve/src/main/select-time.xsl
benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/SieveTest.java
     1.1 --- a/benchmarks/matrix-multiplication/pom.xml	Sat Dec 26 08:59:42 2015 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,103 +0,0 @@
     1.4 -<?xml version="1.0"?>
     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 -
     1.8 -  <groupId>org.apidesign.bck2brwsr</groupId>
     1.9 -  <artifactId>matrix.multiplication</artifactId>
    1.10 -  <version>1.0-SNAPSHOT</version>
    1.11 -  <packaging>jar</packaging>
    1.12 -  <parent>
    1.13 -    <artifactId>benchmarks</artifactId>
    1.14 -    <groupId>org.apidesign.bck2brwsr</groupId>
    1.15 -    <version>1.0-SNAPSHOT</version>
    1.16 -  </parent>
    1.17 -
    1.18 -  <name>Matrix multiplication</name>
    1.19 -
    1.20 -  <properties>
    1.21 -    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    1.22 -  </properties>
    1.23 -
    1.24 -  <build>
    1.25 -      <plugins>
    1.26 -          <plugin>
    1.27 -            <groupId>org.apache.maven.plugins</groupId>
    1.28 -            <artifactId>maven-compiler-plugin</artifactId>
    1.29 -            <version>2.3.2</version>
    1.30 -            <configuration>
    1.31 -               <source>1.7</source>
    1.32 -               <target>1.7</target>
    1.33 -            </configuration>
    1.34 -          </plugin>
    1.35 -          <plugin>
    1.36 -              <groupId>org.apache.maven.plugins</groupId>
    1.37 -              <artifactId>maven-deploy-plugin</artifactId>
    1.38 -              <version>2.7</version>
    1.39 -              <configuration>
    1.40 -                  <skip>true</skip>
    1.41 -              </configuration>
    1.42 -          </plugin>      
    1.43 -          <plugin>
    1.44 -              <groupId>org.codehaus.mojo</groupId>
    1.45 -              <artifactId>xml-maven-plugin</artifactId>
    1.46 -              <version>1.0</version>
    1.47 -              <executions>
    1.48 -                  <execution>
    1.49 -                      <goals>
    1.50 -                          <goal>transform</goal>
    1.51 -                      </goals>
    1.52 -                      <phase>install</phase>
    1.53 -                  </execution>
    1.54 -              </executions>
    1.55 -              <configuration>
    1.56 -                  <transformationSets>
    1.57 -                      <transformationSet>
    1.58 -                          <dir>target/surefire-reports</dir>
    1.59 -                          <outputDir>target/surefire-reports</outputDir>
    1.60 -                          <includes>
    1.61 -                              <include>TEST*.xml</include>
    1.62 -                          </includes>
    1.63 -                          <stylesheet>src/main/select-time.xsl</stylesheet>
    1.64 -                          <fileMappers>
    1.65 -                              <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
    1.66 -                                  <targetExtension>.csv</targetExtension>
    1.67 -                              </fileMapper>
    1.68 -                          </fileMappers>                          
    1.69 -                      </transformationSet>
    1.70 -                  </transformationSets>
    1.71 -              </configuration>
    1.72 -          </plugin>
    1.73 -      </plugins>
    1.74 -  </build>
    1.75 -  
    1.76 -  <dependencies>
    1.77 -    <dependency>
    1.78 -      <groupId>org.apidesign.bck2brwsr</groupId>
    1.79 -      <artifactId>emul.mini</artifactId>
    1.80 -      <version>${project.version}</version>
    1.81 -    </dependency>
    1.82 -    <dependency>
    1.83 -      <groupId>org.testng</groupId>
    1.84 -      <artifactId>testng</artifactId>
    1.85 -      <scope>test</scope>
    1.86 -      <exclusions>
    1.87 -        <exclusion>
    1.88 -          <artifactId>junit</artifactId>
    1.89 -          <groupId>junit</groupId>
    1.90 -        </exclusion>
    1.91 -      </exclusions>
    1.92 -    </dependency>
    1.93 -    <dependency>
    1.94 -      <groupId>org.apidesign.bck2brwsr</groupId>
    1.95 -      <artifactId>vmtest</artifactId>
    1.96 -      <version>${project.version}</version>
    1.97 -      <scope>test</scope>
    1.98 -    </dependency>
    1.99 -    <dependency>
   1.100 -      <groupId>org.apidesign.bck2brwsr</groupId>
   1.101 -      <artifactId>launcher.http</artifactId>
   1.102 -      <version>${project.version}</version>
   1.103 -      <scope>test</scope>
   1.104 -    </dependency>
   1.105 -  </dependencies>
   1.106 -</project>
     2.1 --- a/benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java	Sat Dec 26 08:59:42 2015 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,108 +0,0 @@
     2.4 -/**
     2.5 - * Back 2 Browser Bytecode Translator
     2.6 - * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 - *
     2.8 - * This program is free software: you can redistribute it and/or modify
     2.9 - * it under the terms of the GNU General Public License as published by
    2.10 - * the Free Software Foundation, version 2 of the License.
    2.11 - *
    2.12 - * This program is distributed in the hope that it will be useful,
    2.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 - * GNU General Public License for more details.
    2.16 - *
    2.17 - * You should have received a copy of the GNU General Public License
    2.18 - * along with this program. Look for COPYING file in the top folder.
    2.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 - */
    2.21 -package org.apidesign.benchmark.matrixmul;
    2.22 -
    2.23 -import java.io.IOException;
    2.24 -import java.util.Arrays;
    2.25 -
    2.26 -public class Matrix {
    2.27 -    private final int rank;
    2.28 -    private final float data[][];
    2.29 -    
    2.30 -    public Matrix(int r) {
    2.31 -        this(r, new float[r][r]);
    2.32 -    }
    2.33 -    
    2.34 -    private Matrix(int r, float[][] data) {
    2.35 -        this.rank = r;
    2.36 -        this.data = data;
    2.37 -    }
    2.38 -    
    2.39 -    public void setElement(int i, int j, float value) {
    2.40 -        data[i][j] = value;
    2.41 -    }
    2.42 -    public float getElement(int i, int j) {
    2.43 -        return data[i][j];
    2.44 -    }
    2.45 -    
    2.46 -    public void generateData() {
    2.47 -        //final Random rand = new Random();
    2.48 -        //final int x = 10;
    2.49 -        for (int i = 0; i < rank; i++) {
    2.50 -            for (int j = 0; j < rank; j++) {
    2.51 -                data[i][j] = 1 / (1 + i + j);
    2.52 -            }
    2.53 -        }
    2.54 -    }
    2.55 -
    2.56 -    public Matrix multiply(Matrix m) {
    2.57 -        if (rank != m.rank) {
    2.58 -            throw new IllegalArgumentException("Rank doesn't match");
    2.59 -        }
    2.60 -        
    2.61 -        final float res[][] = new float[rank][rank];
    2.62 -        for (int i = 0; i < rank; i++) {
    2.63 -            for (int j = 0; j < rank; j++) {
    2.64 -                float ij = 0;
    2.65 -                for (int q = 0; q < rank; q++) {
    2.66 -                    ij += data[i][q] * m.data[q][j];
    2.67 -                }
    2.68 -                res[i][j] = ij;
    2.69 -            }
    2.70 -        }
    2.71 -        return new Matrix(rank, res);
    2.72 -    }
    2.73 -    
    2.74 -    public void printOn(Appendable s) throws IOException {
    2.75 -        for (int i = 0; i < rank; i++) {
    2.76 -            String sep = "";
    2.77 -            for (int j = 0; j < rank; j++) {
    2.78 -                s.append(sep + data[i][j]);
    2.79 -                sep = " ";
    2.80 -            }
    2.81 -            s.append("\n");
    2.82 -        }
    2.83 -    }
    2.84 -
    2.85 -    @Override
    2.86 -    public boolean equals(Object obj) {
    2.87 -        if (obj instanceof Matrix) {
    2.88 -            Matrix snd = (Matrix)obj;
    2.89 -            if (snd.rank != rank) {
    2.90 -                return false;
    2.91 -            }
    2.92 -            for (int i = 0; i < rank; i++) {
    2.93 -                for (int j = 0; j < rank; j++) {
    2.94 -                    if (data[i][j] != snd.data[i][j]) {
    2.95 -                        return false;
    2.96 -                    }
    2.97 -                }
    2.98 -            }
    2.99 -            return true;
   2.100 -        }
   2.101 -        return false;
   2.102 -    }
   2.103 -
   2.104 -    @Override
   2.105 -    public int hashCode() {
   2.106 -        int hash = 3;
   2.107 -        hash = 97 * hash + this.rank;
   2.108 -        hash = 97 * hash + Arrays.deepHashCode(this.data);
   2.109 -        return hash;
   2.110 -    }
   2.111 -}
     3.1 --- a/benchmarks/matrix-multiplication/src/main/select-time.xsl	Sat Dec 26 08:59:42 2015 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,54 +0,0 @@
     3.4 -<?xml version="1.0" encoding="UTF-8"?>
     3.5 -<!--
     3.6 -
     3.7 -    Back 2 Browser Bytecode Translator
     3.8 -    Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.9 -
    3.10 -    This program is free software: you can redistribute it and/or modify
    3.11 -    it under the terms of the GNU General Public License as published by
    3.12 -    the Free Software Foundation, version 2 of the License.
    3.13 -
    3.14 -    This program is distributed in the hope that it will be useful,
    3.15 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.16 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.17 -    GNU General Public License for more details.
    3.18 -
    3.19 -    You should have received a copy of the GNU General Public License
    3.20 -    along with this program. Look for COPYING file in the top folder.
    3.21 -    If not, see http://opensource.org/licenses/GPL-2.0.
    3.22 -
    3.23 --->
    3.24 -<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    3.25 -    <xsl:output method="text"/>
    3.26 -
    3.27 -    <xsl:template match="/">
    3.28 -        <xsl:apply-templates mode="header" select="testsuite/testcase"/><xsl:text>End
    3.29 -</xsl:text>
    3.30 -        <xsl:apply-templates mode="value" select="testsuite/testcase"/><xsl:text>NaN
    3.31 -</xsl:text>
    3.32 -    </xsl:template>
    3.33 -        
    3.34 -    
    3.35 -    <xsl:template match="testcase" mode="header">
    3.36 -      <xsl:if test="contains(@name,'tenThousand')">
    3.37 -        <xsl:if test="not(contains(@name, '[Java]'))">
    3.38 -          <xsl:if test="not(contains(@name, '[Compare'))">
    3.39 -              <xsl:value-of select="@name"/>
    3.40 -              <xsl:text>,</xsl:text>
    3.41 -          </xsl:if>
    3.42 -        </xsl:if>
    3.43 -      </xsl:if>
    3.44 -    </xsl:template>
    3.45 -
    3.46 -    <xsl:template match="testcase" mode="value">
    3.47 -      <xsl:if test="contains(@name,'tenThousand')">
    3.48 -        <xsl:if test="not(contains(@name, '[Java]'))">
    3.49 -          <xsl:if test="not(contains(@name, '[Compare'))">
    3.50 -            <xsl:value-of select="@time"/>
    3.51 -            <xsl:text>,</xsl:text>
    3.52 -          </xsl:if>
    3.53 -        </xsl:if>
    3.54 -      </xsl:if>
    3.55 -    </xsl:template>
    3.56 -
    3.57 -</xsl:stylesheet>
     4.1 --- a/benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java	Sat Dec 26 08:59:42 2015 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,94 +0,0 @@
     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.matrixmul;
    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 -/**
    4.29 - *
    4.30 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.31 - */
    4.32 -public class MatrixTest {
    4.33 -    public MatrixTest() {
    4.34 -    }
    4.35 -
    4.36 -    @Compare(scripting = false) 
    4.37 -    public String oneIteration() throws IOException {
    4.38 -    
    4.39 -        Matrix m1 = new Matrix(5);
    4.40 -        Matrix m2 = new Matrix(5);
    4.41 -        
    4.42 -        m1.generateData();
    4.43 -        m2.generateData();
    4.44 -        
    4.45 -        Matrix res = m1.multiply(m2);
    4.46 -        
    4.47 -        StringBuilder sb = new StringBuilder();
    4.48 -        res.printOn(sb);
    4.49 -        return sb.toString();
    4.50 -    }
    4.51 -    
    4.52 -    @Compare(scripting = false) 
    4.53 -    public String tenThousandIterations() throws IOException {
    4.54 -    
    4.55 -        Matrix m1 = new Matrix(5);
    4.56 -        Matrix m2 = new Matrix(5);
    4.57 -        
    4.58 -        m1.generateData();
    4.59 -        m2.generateData();
    4.60 -        
    4.61 -        Matrix res = null;
    4.62 -        for (int i = 0; i < 10000; i++) {
    4.63 -            res = m1.multiply(m2);
    4.64 -            m1 = res;
    4.65 -        }
    4.66 -        
    4.67 -        StringBuilder sb = new StringBuilder();
    4.68 -        res.printOn(sb);
    4.69 -        return sb.toString();
    4.70 -    }
    4.71 -    
    4.72 -    @Compare(scripting = false) 
    4.73 -    public String tenUselessIterations() throws IOException {
    4.74 -    
    4.75 -        Matrix m1 = new Matrix(5);
    4.76 -        Matrix m2 = new Matrix(5);
    4.77 -        
    4.78 -        m1.generateData();
    4.79 -        m2.generateData();
    4.80 -        
    4.81 -        Matrix res = null;
    4.82 -        for (int i = 0; i < 10; i++) {
    4.83 -            res = m1.multiply(m2);
    4.84 -            m1 = res;
    4.85 -        }
    4.86 -        
    4.87 -        StringBuilder sb = new StringBuilder();
    4.88 -        res.printOn(sb);
    4.89 -        return sb.toString();
    4.90 -    }
    4.91 -
    4.92 -    
    4.93 -    @Factory
    4.94 -    public static Object[] create() {
    4.95 -        return VMTest.create(MatrixTest.class);
    4.96 -    }
    4.97 -}
     5.1 --- a/benchmarks/pom.xml	Sat Dec 26 08:59:42 2015 +0100
     5.2 +++ b/benchmarks/pom.xml	Sun Jan 24 12:08:45 2016 +0100
     5.3 @@ -12,6 +12,6 @@
     5.4    <packaging>pom</packaging>
     5.5    <name>Performance benchmarks</name>
     5.6    <modules>
     5.7 -    <module>matrix-multiplication</module>
     5.8 +    <module>sieve</module>
     5.9    </modules>
    5.10  </project>
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/benchmarks/sieve/pom.xml	Sun Jan 24 12:08:45 2016 +0100
     6.3 @@ -0,0 +1,103 @@
     6.4 +<?xml version="1.0"?>
     6.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">
     6.6 +  <modelVersion>4.0.0</modelVersion>
     6.7 +
     6.8 +  <groupId>org.apidesign.bck2brwsr</groupId>
     6.9 +  <artifactId>sieve</artifactId>
    6.10 +  <version>1.0-SNAPSHOT</version>
    6.11 +  <packaging>jar</packaging>
    6.12 +  <parent>
    6.13 +    <artifactId>benchmarks</artifactId>
    6.14 +    <groupId>org.apidesign.bck2brwsr</groupId>
    6.15 +    <version>1.0-SNAPSHOT</version>
    6.16 +  </parent>
    6.17 +
    6.18 +  <name>Sieve of Eratosthenes</name>
    6.19 +
    6.20 +  <properties>
    6.21 +    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    6.22 +  </properties>
    6.23 +
    6.24 +  <build>
    6.25 +      <plugins>
    6.26 +          <plugin>
    6.27 +            <groupId>org.apache.maven.plugins</groupId>
    6.28 +            <artifactId>maven-compiler-plugin</artifactId>
    6.29 +            <version>2.3.2</version>
    6.30 +            <configuration>
    6.31 +               <source>1.7</source>
    6.32 +               <target>1.7</target>
    6.33 +            </configuration>
    6.34 +          </plugin>
    6.35 +          <plugin>
    6.36 +              <groupId>org.apache.maven.plugins</groupId>
    6.37 +              <artifactId>maven-deploy-plugin</artifactId>
    6.38 +              <version>2.7</version>
    6.39 +              <configuration>
    6.40 +                  <skip>true</skip>
    6.41 +              </configuration>
    6.42 +          </plugin>      
    6.43 +          <plugin>
    6.44 +              <groupId>org.codehaus.mojo</groupId>
    6.45 +              <artifactId>xml-maven-plugin</artifactId>
    6.46 +              <version>1.0</version>
    6.47 +              <executions>
    6.48 +                  <execution>
    6.49 +                      <goals>
    6.50 +                          <goal>transform</goal>
    6.51 +                      </goals>
    6.52 +                      <phase>install</phase>
    6.53 +                  </execution>
    6.54 +              </executions>
    6.55 +              <configuration>
    6.56 +                  <transformationSets>
    6.57 +                      <transformationSet>
    6.58 +                          <dir>target/surefire-reports</dir>
    6.59 +                          <outputDir>target/surefire-reports</outputDir>
    6.60 +                          <includes>
    6.61 +                              <include>TEST*.xml</include>
    6.62 +                          </includes>
    6.63 +                          <stylesheet>src/main/select-time.xsl</stylesheet>
    6.64 +                          <fileMappers>
    6.65 +                              <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
    6.66 +                                  <targetExtension>.csv</targetExtension>
    6.67 +                              </fileMapper>
    6.68 +                          </fileMappers>                          
    6.69 +                      </transformationSet>
    6.70 +                  </transformationSets>
    6.71 +              </configuration>
    6.72 +          </plugin>
    6.73 +      </plugins>
    6.74 +  </build>
    6.75 +  
    6.76 +  <dependencies>
    6.77 +    <dependency>
    6.78 +      <groupId>org.apidesign.bck2brwsr</groupId>
    6.79 +      <artifactId>emul.mini</artifactId>
    6.80 +      <version>${project.version}</version>
    6.81 +    </dependency>
    6.82 +    <dependency>
    6.83 +      <groupId>org.testng</groupId>
    6.84 +      <artifactId>testng</artifactId>
    6.85 +      <scope>test</scope>
    6.86 +      <exclusions>
    6.87 +        <exclusion>
    6.88 +          <artifactId>junit</artifactId>
    6.89 +          <groupId>junit</groupId>
    6.90 +        </exclusion>
    6.91 +      </exclusions>
    6.92 +    </dependency>
    6.93 +    <dependency>
    6.94 +      <groupId>org.apidesign.bck2brwsr</groupId>
    6.95 +      <artifactId>vmtest</artifactId>
    6.96 +      <version>${project.version}</version>
    6.97 +      <scope>test</scope>
    6.98 +    </dependency>
    6.99 +    <dependency>
   6.100 +      <groupId>org.apidesign.bck2brwsr</groupId>
   6.101 +      <artifactId>launcher.http</artifactId>
   6.102 +      <version>${project.version}</version>
   6.103 +      <scope>test</scope>
   6.104 +    </dependency>
   6.105 +  </dependencies>
   6.106 +</project>
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Filter.java	Sun Jan 24 12:08:45 2016 +0100
     7.3 @@ -0,0 +1,41 @@
     7.4 +/**
     7.5 + * Back 2 Browser Bytecode Translator
     7.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     7.7 + *
     7.8 + * This program is free software: you can redistribute it and/or modify
     7.9 + * it under the terms of the GNU General Public License as published by
    7.10 + * the Free Software Foundation, version 2 of the License.
    7.11 + *
    7.12 + * This program is distributed in the hope that it will be useful,
    7.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    7.15 + * GNU General Public License for more details.
    7.16 + *
    7.17 + * You should have received a copy of the GNU General Public License
    7.18 + * along with this program. Look for COPYING file in the top folder.
    7.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    7.20 + */
    7.21 +package org.apidesign.benchmark.sieve;
    7.22 +
    7.23 +final class Filter {
    7.24 +    private final int number;
    7.25 +    private final Filter next;
    7.26 +
    7.27 +    public Filter(int number, Filter next) {
    7.28 +        this.number = number;
    7.29 +        this.next = next;
    7.30 +    }
    7.31 +
    7.32 +    public boolean accept(int n) {
    7.33 +        Filter filter = this;
    7.34 +        for (;;) {
    7.35 +            if (n % filter.number == 0) {
    7.36 +                return false;
    7.37 +            }
    7.38 +            filter = filter.next;
    7.39 +            if (filter == null) {
    7.40 +                return true;
    7.41 +            }
    7.42 +        }
    7.43 +    }
    7.44 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Natural.java	Sun Jan 24 12:08:45 2016 +0100
     8.3 @@ -0,0 +1,26 @@
     8.4 +/**
     8.5 + * Back 2 Browser Bytecode Translator
     8.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     8.7 + *
     8.8 + * This program is free software: you can redistribute it and/or modify
     8.9 + * it under the terms of the GNU General Public License as published by
    8.10 + * the Free Software Foundation, version 2 of the License.
    8.11 + *
    8.12 + * This program is distributed in the hope that it will be useful,
    8.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    8.15 + * GNU General Public License for more details.
    8.16 + *
    8.17 + * You should have received a copy of the GNU General Public License
    8.18 + * along with this program. Look for COPYING file in the top folder.
    8.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    8.20 + */
    8.21 +package org.apidesign.benchmark.sieve;
    8.22 +
    8.23 +final class Natural {
    8.24 +    private int cnt = 2;
    8.25 +
    8.26 +    int next() {
    8.27 +        return cnt++;
    8.28 +    }
    8.29 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Primes.java	Sun Jan 24 12:08:45 2016 +0100
     9.3 @@ -0,0 +1,55 @@
     9.4 +/**
     9.5 + * Back 2 Browser Bytecode Translator
     9.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     9.7 + *
     9.8 + * This program is free software: you can redistribute it and/or modify
     9.9 + * it under the terms of the GNU General Public License as published by
    9.10 + * the Free Software Foundation, version 2 of the License.
    9.11 + *
    9.12 + * This program is distributed in the hope that it will be useful,
    9.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    9.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    9.15 + * GNU General Public License for more details.
    9.16 + *
    9.17 + * You should have received a copy of the GNU General Public License
    9.18 + * along with this program. Look for COPYING file in the top folder.
    9.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    9.20 + */
    9.21 +package org.apidesign.benchmark.sieve;
    9.22 +
    9.23 +abstract class Primes {
    9.24 +    private final Natural natural;
    9.25 +    private Filter filter;
    9.26 +
    9.27 +    protected Primes() {
    9.28 +        this.natural = new Natural();
    9.29 +    }
    9.30 +
    9.31 +    int next() {
    9.32 +        for (;;) {
    9.33 +            int n = natural.next();
    9.34 +            if (filter == null || filter.accept(n)) {
    9.35 +                filter = new Filter(n, filter);
    9.36 +                return n;
    9.37 +            }
    9.38 +        }
    9.39 +    }
    9.40 +
    9.41 +    protected abstract void log(String msg);
    9.42 +
    9.43 +    public final int compute(int count) {
    9.44 +        int cnt = 0;
    9.45 +        int res;
    9.46 +        for (;;) {
    9.47 +            res = next();
    9.48 +            cnt += 1;
    9.49 +            if (cnt % 1000 == 0) {
    9.50 +                log("Computed " + cnt + " primes. Last one is " + res);
    9.51 +            }
    9.52 +            if (cnt >= count) {
    9.53 +                break;
    9.54 +            }
    9.55 +        }
    9.56 +        return res;
    9.57 +    }
    9.58 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/benchmarks/sieve/src/main/select-time.xsl	Sun Jan 24 12:08:45 2016 +0100
    10.3 @@ -0,0 +1,54 @@
    10.4 +<?xml version="1.0" encoding="UTF-8"?>
    10.5 +<!--
    10.6 +
    10.7 +    Back 2 Browser Bytecode Translator
    10.8 +    Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    10.9 +
   10.10 +    This program is free software: you can redistribute it and/or modify
   10.11 +    it under the terms of the GNU General Public License as published by
   10.12 +    the Free Software Foundation, version 2 of the License.
   10.13 +
   10.14 +    This program is distributed in the hope that it will be useful,
   10.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10.17 +    GNU General Public License for more details.
   10.18 +
   10.19 +    You should have received a copy of the GNU General Public License
   10.20 +    along with this program. Look for COPYING file in the top folder.
   10.21 +    If not, see http://opensource.org/licenses/GPL-2.0.
   10.22 +
   10.23 +-->
   10.24 +<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   10.25 +    <xsl:output method="text"/>
   10.26 +
   10.27 +    <xsl:template match="/">
   10.28 +        <xsl:apply-templates mode="header" select="testsuite/testcase"/><xsl:text>End
   10.29 +</xsl:text>
   10.30 +        <xsl:apply-templates mode="value" select="testsuite/testcase"/><xsl:text>NaN
   10.31 +</xsl:text>
   10.32 +    </xsl:template>
   10.33 +        
   10.34 +    
   10.35 +    <xsl:template match="testcase" mode="header">
   10.36 +      <xsl:if test="contains(@name,'tenThousand')">
   10.37 +        <xsl:if test="not(contains(@name, '[Java]'))">
   10.38 +          <xsl:if test="not(contains(@name, '[Compare'))">
   10.39 +              <xsl:value-of select="@name"/>
   10.40 +              <xsl:text>,</xsl:text>
   10.41 +          </xsl:if>
   10.42 +        </xsl:if>
   10.43 +      </xsl:if>
   10.44 +    </xsl:template>
   10.45 +
   10.46 +    <xsl:template match="testcase" mode="value">
   10.47 +      <xsl:if test="contains(@name,'tenThousand')">
   10.48 +        <xsl:if test="not(contains(@name, '[Java]'))">
   10.49 +          <xsl:if test="not(contains(@name, '[Compare'))">
   10.50 +            <xsl:value-of select="@time"/>
   10.51 +            <xsl:text>,</xsl:text>
   10.52 +          </xsl:if>
   10.53 +        </xsl:if>
   10.54 +      </xsl:if>
   10.55 +    </xsl:template>
   10.56 +
   10.57 +</xsl:stylesheet>
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/SieveTest.java	Sun Jan 24 12:08:45 2016 +0100
    11.3 @@ -0,0 +1,58 @@
    11.4 +/**
    11.5 + * Back 2 Browser Bytecode Translator
    11.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    11.7 + *
    11.8 + * This program is free software: you can redistribute it and/or modify
    11.9 + * it under the terms of the GNU General Public License as published by
   11.10 + * the Free Software Foundation, version 2 of the License.
   11.11 + *
   11.12 + * This program is distributed in the hope that it will be useful,
   11.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11.15 + * GNU General Public License for more details.
   11.16 + *
   11.17 + * You should have received a copy of the GNU General Public License
   11.18 + * along with this program. Look for COPYING file in the top folder.
   11.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
   11.20 + */
   11.21 +package org.apidesign.benchmark.sieve;
   11.22 +
   11.23 +import java.io.IOException;
   11.24 +import net.java.html.js.JavaScriptBody;
   11.25 +import org.apidesign.bck2brwsr.vmtest.Compare;
   11.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
   11.27 +import org.testng.annotations.Factory;
   11.28 +
   11.29 +/**
   11.30 + *
   11.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   11.32 + */
   11.33 +public class SieveTest extends Primes {
   11.34 +    public SieveTest() {
   11.35 +    }
   11.36 +
   11.37 +    @JavaScriptBody(args = {  }, body = "return new Date().getTime();")
   11.38 +    protected int time() {
   11.39 +        return (int) System.currentTimeMillis();
   11.40 +    }
   11.41 +
   11.42 +    @Compare(scripting = false) 
   11.43 +    public int oneThousand() throws IOException {
   11.44 +        SieveTest sieve = new SieveTest();
   11.45 +        int now = time();
   11.46 +        int res = sieve.compute(1000);
   11.47 +        int took = time() - now;
   11.48 +        log("oneThousand in " + took + " ms");
   11.49 +        return res;
   11.50 +    }
   11.51 +    
   11.52 +    @Factory
   11.53 +    public static Object[] create() {
   11.54 +        return VMTest.create(SieveTest.class);
   11.55 +    }
   11.56 +
   11.57 +    @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg);")
   11.58 +    @Override
   11.59 +    protected void log(String msg) {
   11.60 +    }
   11.61 +}