Can execute the matrix multiplication test using VMTest
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 25 Dec 2012 14:07:02 +0100
changeset 38170d15cf323ba
parent 380 4ce33ad7d507
child 382 57fc3a0563c9
Can execute the matrix multiplication test using VMTest
benchmarks/matrix-multiplication/pom.xml
benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Main.java
benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java
benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java
     1.1 --- a/benchmarks/matrix-multiplication/pom.xml	Tue Dec 25 13:31:35 2012 +0100
     1.2 +++ b/benchmarks/matrix-multiplication/pom.xml	Tue Dec 25 14:07:02 2012 +0100
     1.3 @@ -25,20 +25,6 @@
     1.4                 <target>1.7</target>
     1.5              </configuration>
     1.6            </plugin>
     1.7 -          
     1.8 -          <plugin>
     1.9 -              <groupId>org.apidesign.bck2brwsr</groupId>
    1.10 -              <artifactId>mojo</artifactId>
    1.11 -              <version>0.3-SNAPSHOT</version>
    1.12 -              <executions>
    1.13 -                  <execution>
    1.14 -                      <goals>
    1.15 -                          <goal>j2js</goal>
    1.16 -                      </goals>
    1.17 -                  </execution>
    1.18 -              </executions>
    1.19 -          </plugin>
    1.20 -          
    1.21        </plugins>
    1.22    </build>
    1.23    
    1.24 @@ -88,24 +74,22 @@
    1.25        <version>0.3-SNAPSHOT</version>
    1.26      </dependency>
    1.27      <dependency>
    1.28 -        <groupId>com.googlecode.jstd-maven-plugin</groupId>
    1.29 -        <artifactId>jstd-maven-plugin</artifactId>
    1.30 -        <version>1.3.2.5</version>
    1.31 -        <scope>test</scope>
    1.32 +      <groupId>org.testng</groupId>
    1.33 +      <artifactId>testng</artifactId>
    1.34 +      <version>6.5.2</version>
    1.35 +      <scope>test</scope>
    1.36 +      <exclusions>
    1.37 +        <exclusion>
    1.38 +          <artifactId>junit</artifactId>
    1.39 +          <groupId>junit</groupId>
    1.40 +        </exclusion>
    1.41 +      </exclusions>
    1.42 +    </dependency>
    1.43 +    <dependency>
    1.44 +      <groupId>org.apidesign.bck2brwsr</groupId>
    1.45 +      <artifactId>vmtest</artifactId>
    1.46 +      <version>0.3-SNAPSHOT</version>
    1.47 +      <scope>test</scope>
    1.48      </dependency>
    1.49    </dependencies>
    1.50 -  
    1.51 -  <repositories>
    1.52 -        <repository>
    1.53 -            <id>jstd-maven-plugin google code repo</id>
    1.54 -            <url>http://jstd-maven-plugin.googlecode.com/svn/maven2</url>
    1.55 -        </repository>
    1.56 -  </repositories>
    1.57 -  <pluginRepositories>
    1.58 -        <pluginRepository>
    1.59 -            <id>jstd-maven-plugin google code repo</id>
    1.60 -            <url>http://jstd-maven-plugin.googlecode.com/svn/maven2</url>
    1.61 -        </pluginRepository>
    1.62 -  </pluginRepositories>
    1.63 -  
    1.64  </project>
     2.1 --- a/benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Main.java	Tue Dec 25 13:31:35 2012 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,42 +0,0 @@
     2.4 -/**
     2.5 - * Back 2 Browser Bytecode Translator
     2.6 - * Copyright (C) 2012 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 -public class Main {
    2.24 -
    2.25 -    public static final int ITERATION_COUNT = 100000;
    2.26 -    
    2.27 -    public static void main(String[] args) {
    2.28 -        Matrix m1 = new Matrix(5);
    2.29 -        Matrix m2 = new Matrix(5);
    2.30 -        
    2.31 -        m1.generateData();
    2.32 -        m2.generateData();
    2.33 -        
    2.34 -        //m1.printOn(System.out);
    2.35 -        //System.out.println("x");
    2.36 -        //m2.printOn(System.out);
    2.37 -        
    2.38 -        for (int i = 0; i < ITERATION_COUNT; i++) {
    2.39 -            m1.multiply(m2);
    2.40 -        }
    2.41 -        
    2.42 -        //System.out.println("=");
    2.43 -        //m1.printOn(System.out);
    2.44 -    }
    2.45 -}
     3.1 --- a/benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java	Tue Dec 25 13:31:35 2012 +0100
     3.2 +++ b/benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java	Tue Dec 25 14:07:02 2012 +0100
     3.3 @@ -17,17 +17,20 @@
     3.4   */
     3.5  package org.apidesign.benchmark.matrixmul;
     3.6  
     3.7 -//import java.io.PrintStream;
     3.8 -//import java.util.Random;
     3.9 +import java.io.IOException;
    3.10 +import java.util.Arrays;
    3.11  
    3.12  public class Matrix {
    3.13 -
    3.14      private final int rank;
    3.15 -    private float data[][];
    3.16 +    private final float data[][];
    3.17      
    3.18      public Matrix(int r) {
    3.19 -        rank = r;
    3.20 -        data = new float[r][r];
    3.21 +        this(r, new float[r][r]);
    3.22 +    }
    3.23 +    
    3.24 +    private Matrix(int r, float[][] data) {
    3.25 +        this.rank = r;
    3.26 +        this.data = data;
    3.27      }
    3.28      
    3.29      public void setElement(int i, int j, float value) {
    3.30 @@ -62,20 +65,44 @@
    3.31                  res[i][j] = ij;
    3.32              }
    3.33          }
    3.34 -        data = res;
    3.35 -        
    3.36 -        return this;
    3.37 +        return new Matrix(rank, res);
    3.38      }
    3.39      
    3.40 -    /*
    3.41 -    public void printOn(PrintStream s) {
    3.42 +    public void printOn(Appendable s) throws IOException {
    3.43          for (int i = 0; i < rank; i++) {
    3.44 +            String sep = "";
    3.45              for (int j = 0; j < rank; j++) {
    3.46 -                s.printf("%f ", data[i][j]);
    3.47 +                s.append(sep + data[i][j]);
    3.48 +                sep = " ";
    3.49              }
    3.50 -            s.println();
    3.51 +            s.append("\n");
    3.52          }
    3.53      }
    3.54 -    */
    3.55 -    
    3.56 +
    3.57 +    @Override
    3.58 +    public boolean equals(Object obj) {
    3.59 +        if (obj instanceof Matrix) {
    3.60 +            Matrix snd = (Matrix)obj;
    3.61 +            if (snd.rank != rank) {
    3.62 +                return false;
    3.63 +            }
    3.64 +            for (int i = 0; i < rank; i++) {
    3.65 +                for (int j = 0; j < rank; j++) {
    3.66 +                    if (data[i][j] != snd.data[i][j]) {
    3.67 +                        return false;
    3.68 +                    }
    3.69 +                }
    3.70 +            }
    3.71 +            return true;
    3.72 +        }
    3.73 +        return false;
    3.74 +    }
    3.75 +
    3.76 +    @Override
    3.77 +    public int hashCode() {
    3.78 +        int hash = 3;
    3.79 +        hash = 97 * hash + this.rank;
    3.80 +        hash = 97 * hash + Arrays.deepHashCode(this.data);
    3.81 +        return hash;
    3.82 +    }
    3.83  }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java	Tue Dec 25 14:07:02 2012 +0100
     4.3 @@ -0,0 +1,61 @@
     4.4 +/**
     4.5 + * Back 2 Browser Bytecode Translator
     4.6 + * Copyright (C) 2012 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 static final int ITERATION_COUNT = 10;
    4.34 +    
    4.35 +    public MatrixTest() {
    4.36 +    }
    4.37 +
    4.38 +    @Compare public String tenThousandIterations() throws IOException {
    4.39 +    
    4.40 +        Matrix m1 = new Matrix(5);
    4.41 +        Matrix m2 = new Matrix(5);
    4.42 +        
    4.43 +        m1.generateData();
    4.44 +        m2.generateData();
    4.45 +        
    4.46 +        Matrix res = null;
    4.47 +        for (int i = 0; i < ITERATION_COUNT; i++) {
    4.48 +            Matrix m = m1.multiply(m2);
    4.49 +            if (res != null && !res.equals(m)) {
    4.50 +                return "different";
    4.51 +            }
    4.52 +            res = m;
    4.53 +        }
    4.54 +        
    4.55 +        StringBuilder sb = new StringBuilder();
    4.56 +        res.printOn(sb);
    4.57 +        return sb.toString();
    4.58 +    }
    4.59 +    
    4.60 +    @Factory
    4.61 +    public static Object[] create() {
    4.62 +        return VMTest.create(MatrixTest.class);
    4.63 +    }
    4.64 +}
     5.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Tue Dec 25 13:31:35 2012 +0100
     5.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Tue Dec 25 14:07:02 2012 +0100
     5.3 @@ -149,7 +149,7 @@
     5.4                  
     5.5                  if (id != null && value != null) {
     5.6                      LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
     5.7 -                    value = value.replace("%20", " ");
     5.8 +                    value = decodeURL(value);
     5.9                      cases.get(Integer.parseInt(id)).result(value, null);
    5.10                  }
    5.11                  
    5.12 @@ -253,6 +253,17 @@
    5.13          }
    5.14      }
    5.15      
    5.16 +    private static String decodeURL(String s) {
    5.17 +        for (;;) {
    5.18 +            int pos = s.indexOf('%');
    5.19 +            if (pos == -1) {
    5.20 +                return s;
    5.21 +            }
    5.22 +            int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
    5.23 +            s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
    5.24 +        }
    5.25 +    }
    5.26 +    
    5.27      private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException, InterruptedException {
    5.28          Process process = (Process)brwsr[0];
    5.29          
     6.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java	Tue Dec 25 13:31:35 2012 +0100
     6.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java	Tue Dec 25 14:07:02 2012 +0100
     6.3 @@ -22,6 +22,8 @@
     6.4  import java.lang.reflect.InvocationTargetException;
     6.5  import java.lang.reflect.Method;
     6.6  import java.net.URL;
     6.7 +import java.net.URLDecoder;
     6.8 +import java.net.URLEncoder;
     6.9  import java.util.Enumeration;
    6.10  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    6.11  
    6.12 @@ -45,7 +47,7 @@
    6.13          "window.document.getElementById(id)[attr] = value;")
    6.14      private static native void setAttr(String id, String attr, Object value);
    6.15      
    6.16 -    @JavaScriptBody(args = {}, body = "window.close();")
    6.17 +    @JavaScriptBody(args = {}, body = "return; window.close();")
    6.18      private static native void closeWindow();
    6.19  
    6.20      private static void log(String newText) {
    6.21 @@ -81,6 +83,9 @@
    6.22                  Object result = invokeMethod(c.getClassName(), c.getMethodName());
    6.23                  
    6.24                  log("Result: " + result);
    6.25 +                
    6.26 +                result = encodeURL("" + result);
    6.27 +                
    6.28                  log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
    6.29                  u = new URL(url + "?request=" + c.getRequestId() + "&result=" + result);
    6.30              }
    6.31 @@ -91,6 +96,23 @@
    6.32          }
    6.33      }
    6.34      
    6.35 +    private static String encodeURL(String r) {
    6.36 +        StringBuilder sb = new StringBuilder();
    6.37 +        for (int i = 0; i < r.length(); i++) {
    6.38 +            int ch = r.charAt(i);
    6.39 +            if (ch < 32 || ch == '%' || ch == '+') {
    6.40 +                sb.append("%").append(("0" + Integer.toHexString(ch)).substring(0, 2));
    6.41 +            } else {
    6.42 +                if (ch == 32) {
    6.43 +                    sb.append("+");
    6.44 +                } else {
    6.45 +                    sb.append((char)ch);
    6.46 +                }
    6.47 +            }
    6.48 +        }
    6.49 +        return sb.toString();
    6.50 +    }
    6.51 +    
    6.52      static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException {
    6.53          final Object r = invokeMethod(clazz, method);
    6.54          return r == null ? "null" : r.toString().toString();