# HG changeset patch # User Jaroslav Tulach # Date 1453633725 -3600 # Node ID 826eb936c9a82abf23440efcd657c8749cd63579 # Parent 42c6e5a05360a4e312b3325c4ee5cb8b0e0650a3 The sieve benchmark makes the testing more real than the fake matrix multiplication diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/matrix-multiplication/pom.xml --- a/benchmarks/matrix-multiplication/pom.xml Sat Dec 26 08:59:42 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,103 +0,0 @@ - - - 4.0.0 - - org.apidesign.bck2brwsr - matrix.multiplication - 1.0-SNAPSHOT - jar - - benchmarks - org.apidesign.bck2brwsr - 1.0-SNAPSHOT - - - Matrix multiplication - - - 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.codehaus.mojo - xml-maven-plugin - 1.0 - - - - transform - - install - - - - - - target/surefire-reports - target/surefire-reports - - TEST*.xml - - src/main/select-time.xsl - - - .csv - - - - - - - - - - - - org.apidesign.bck2brwsr - emul.mini - ${project.version} - - - org.testng - testng - test - - - junit - junit - - - - - org.apidesign.bck2brwsr - vmtest - ${project.version} - test - - - org.apidesign.bck2brwsr - launcher.http - ${project.version} - test - - - diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java --- a/benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java Sat Dec 26 08:59:42 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -/** - * 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.matrixmul; - -import java.io.IOException; -import java.util.Arrays; - -public class Matrix { - private final int rank; - private final float data[][]; - - public Matrix(int r) { - this(r, new float[r][r]); - } - - private Matrix(int r, float[][] data) { - this.rank = r; - this.data = data; - } - - public void setElement(int i, int j, float value) { - data[i][j] = value; - } - public float getElement(int i, int j) { - return data[i][j]; - } - - public void generateData() { - //final Random rand = new Random(); - //final int x = 10; - for (int i = 0; i < rank; i++) { - for (int j = 0; j < rank; j++) { - data[i][j] = 1 / (1 + i + j); - } - } - } - - public Matrix multiply(Matrix m) { - if (rank != m.rank) { - throw new IllegalArgumentException("Rank doesn't match"); - } - - final float res[][] = new float[rank][rank]; - for (int i = 0; i < rank; i++) { - for (int j = 0; j < rank; j++) { - float ij = 0; - for (int q = 0; q < rank; q++) { - ij += data[i][q] * m.data[q][j]; - } - res[i][j] = ij; - } - } - return new Matrix(rank, res); - } - - public void printOn(Appendable s) throws IOException { - for (int i = 0; i < rank; i++) { - String sep = ""; - for (int j = 0; j < rank; j++) { - s.append(sep + data[i][j]); - sep = " "; - } - s.append("\n"); - } - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof Matrix) { - Matrix snd = (Matrix)obj; - if (snd.rank != rank) { - return false; - } - for (int i = 0; i < rank; i++) { - for (int j = 0; j < rank; j++) { - if (data[i][j] != snd.data[i][j]) { - return false; - } - } - } - return true; - } - return false; - } - - @Override - public int hashCode() { - int hash = 3; - hash = 97 * hash + this.rank; - hash = 97 * hash + Arrays.deepHashCode(this.data); - return hash; - } -} diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/matrix-multiplication/src/main/select-time.xsl --- a/benchmarks/matrix-multiplication/src/main/select-time.xsl Sat Dec 26 08:59:42 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ - - - - - - - End - - NaN - - - - - - - - - - , - - - - - - - - - - - , - - - - - - diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java --- a/benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java Sat Dec 26 08:59:42 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,94 +0,0 @@ -/** - * 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.matrixmul; - -import java.io.IOException; -import org.apidesign.bck2brwsr.vmtest.Compare; -import org.apidesign.bck2brwsr.vmtest.VMTest; -import org.testng.annotations.Factory; - -/** - * - * @author Jaroslav Tulach - */ -public class MatrixTest { - public MatrixTest() { - } - - @Compare(scripting = false) - public String oneIteration() throws IOException { - - Matrix m1 = new Matrix(5); - Matrix m2 = new Matrix(5); - - m1.generateData(); - m2.generateData(); - - Matrix res = m1.multiply(m2); - - StringBuilder sb = new StringBuilder(); - res.printOn(sb); - return sb.toString(); - } - - @Compare(scripting = false) - public String tenThousandIterations() throws IOException { - - Matrix m1 = new Matrix(5); - Matrix m2 = new Matrix(5); - - m1.generateData(); - m2.generateData(); - - Matrix res = null; - for (int i = 0; i < 10000; i++) { - res = m1.multiply(m2); - m1 = res; - } - - StringBuilder sb = new StringBuilder(); - res.printOn(sb); - return sb.toString(); - } - - @Compare(scripting = false) - public String tenUselessIterations() throws IOException { - - Matrix m1 = new Matrix(5); - Matrix m2 = new Matrix(5); - - m1.generateData(); - m2.generateData(); - - Matrix res = null; - for (int i = 0; i < 10; i++) { - res = m1.multiply(m2); - m1 = res; - } - - StringBuilder sb = new StringBuilder(); - res.printOn(sb); - return sb.toString(); - } - - - @Factory - public static Object[] create() { - return VMTest.create(MatrixTest.class); - } -} diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/pom.xml --- a/benchmarks/pom.xml Sat Dec 26 08:59:42 2015 +0100 +++ b/benchmarks/pom.xml Sun Jan 24 12:08:45 2016 +0100 @@ -12,6 +12,6 @@ pom Performance benchmarks - matrix-multiplication + sieve diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/sieve/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/sieve/pom.xml Sun Jan 24 12:08:45 2016 +0100 @@ -0,0 +1,103 @@ + + + 4.0.0 + + org.apidesign.bck2brwsr + sieve + 1.0-SNAPSHOT + jar + + benchmarks + org.apidesign.bck2brwsr + 1.0-SNAPSHOT + + + Sieve of Eratosthenes + + + 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.codehaus.mojo + xml-maven-plugin + 1.0 + + + + transform + + install + + + + + + target/surefire-reports + target/surefire-reports + + TEST*.xml + + src/main/select-time.xsl + + + .csv + + + + + + + + + + + + org.apidesign.bck2brwsr + emul.mini + ${project.version} + + + org.testng + testng + test + + + junit + junit + + + + + org.apidesign.bck2brwsr + vmtest + ${project.version} + test + + + org.apidesign.bck2brwsr + launcher.http + ${project.version} + test + + + diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Filter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Filter.java Sun Jan 24 12:08:45 2016 +0100 @@ -0,0 +1,41 @@ +/** + * 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.sieve; + +final class Filter { + private final int number; + private final Filter next; + + public Filter(int number, Filter next) { + this.number = number; + this.next = next; + } + + public boolean accept(int n) { + Filter filter = this; + for (;;) { + if (n % filter.number == 0) { + return false; + } + filter = filter.next; + if (filter == null) { + return true; + } + } + } +} diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Natural.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Natural.java Sun Jan 24 12:08:45 2016 +0100 @@ -0,0 +1,26 @@ +/** + * 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.sieve; + +final class Natural { + private int cnt = 2; + + int next() { + return cnt++; + } +} diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Primes.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Primes.java Sun Jan 24 12:08:45 2016 +0100 @@ -0,0 +1,55 @@ +/** + * 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.sieve; + +abstract class Primes { + private final Natural natural; + private Filter filter; + + protected Primes() { + this.natural = new Natural(); + } + + int next() { + for (;;) { + int n = natural.next(); + if (filter == null || filter.accept(n)) { + filter = new Filter(n, filter); + return n; + } + } + } + + protected abstract void log(String msg); + + public final int compute(int count) { + int cnt = 0; + int res; + for (;;) { + res = next(); + cnt += 1; + if (cnt % 1000 == 0) { + log("Computed " + cnt + " primes. Last one is " + res); + } + if (cnt >= count) { + break; + } + } + return res; + } +} diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/sieve/src/main/select-time.xsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/sieve/src/main/select-time.xsl Sun Jan 24 12:08:45 2016 +0100 @@ -0,0 +1,54 @@ + + + + + + + End + + NaN + + + + + + + + + + , + + + + + + + + + + + , + + + + + + diff -r 42c6e5a05360 -r 826eb936c9a8 benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/SieveTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/SieveTest.java Sun Jan 24 12:08:45 2016 +0100 @@ -0,0 +1,58 @@ +/** + * 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.sieve; + +import java.io.IOException; +import net.java.html.js.JavaScriptBody; +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class SieveTest extends Primes { + public SieveTest() { + } + + @JavaScriptBody(args = { }, body = "return new Date().getTime();") + protected int time() { + return (int) System.currentTimeMillis(); + } + + @Compare(scripting = false) + public int oneThousand() throws IOException { + SieveTest sieve = new SieveTest(); + int now = time(); + int res = sieve.compute(1000); + int took = time() - now; + log("oneThousand in " + took + " ms"); + return res; + } + + @Factory + public static Object[] create() { + return VMTest.create(SieveTest.class); + } + + @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg);") + @Override + protected void log(String msg) { + } +}