benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/SieveTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Jan 2016 04:36:23 +0100
changeset 1860 4ce38f21f4cd
parent 1857 f1344425bcb1
permissions -rw-r--r--
JavaScript in a browser can be at most three times slower than HotSpot when computing five or ten thousands of prime numbers
jaroslav@381
     1
/**
jaroslav@381
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@381
     4
 *
jaroslav@381
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@381
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@381
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@381
     8
 *
jaroslav@381
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@381
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@381
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@381
    12
 * GNU General Public License for more details.
jaroslav@381
    13
 *
jaroslav@381
    14
 * You should have received a copy of the GNU General Public License
jaroslav@381
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@381
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@381
    17
 */
jaroslav@1854
    18
package org.apidesign.benchmark.sieve;
jaroslav@381
    19
jaroslav@381
    20
import java.io.IOException;
jaroslav@1854
    21
import net.java.html.js.JavaScriptBody;
jaroslav@381
    22
import org.apidesign.bck2brwsr.vmtest.Compare;
jaroslav@381
    23
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@381
    24
import org.testng.annotations.Factory;
jaroslav@381
    25
jaroslav@381
    26
/**
jaroslav@381
    27
 *
jaroslav@381
    28
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@381
    29
 */
jaroslav@1854
    30
public class SieveTest extends Primes {
jaroslav@1854
    31
    public SieveTest() {
jaroslav@1854
    32
    }
jaroslav@1854
    33
jaroslav@1854
    34
    @JavaScriptBody(args = {  }, body = "return new Date().getTime();")
jaroslav@1854
    35
    protected int time() {
jaroslav@1854
    36
        return (int) System.currentTimeMillis();
jaroslav@381
    37
    }
jaroslav@381
    38
jaroslav@1860
    39
    @Compare
jaroslav@1854
    40
    public int oneThousand() throws IOException {
jaroslav@1854
    41
        SieveTest sieve = new SieveTest();
jaroslav@1854
    42
        int now = time();
jaroslav@1854
    43
        int res = sieve.compute(1000);
jaroslav@1854
    44
        int took = time() - now;
jaroslav@1854
    45
        log("oneThousand in " + took + " ms");
jaroslav@1854
    46
        return res;
jaroslav@836
    47
    }
jaroslav@1857
    48
jaroslav@1860
    49
    @Compare(slowdown = 3.0)
jaroslav@1857
    50
    public int fiveThousand() throws IOException {
jaroslav@1857
    51
        SieveTest sieve = new SieveTest();
jaroslav@1857
    52
        int now = time();
jaroslav@1857
    53
        int res = sieve.compute(5000);
jaroslav@1857
    54
        int took = time() - now;
jaroslav@1857
    55
        log("oneThousand in " + took + " ms");
jaroslav@1857
    56
        return res;
jaroslav@1857
    57
    }
jaroslav@1860
    58
jaroslav@1860
    59
    @Compare(slowdown = 3.0)
jaroslav@1860
    60
    public int tenThousand() throws IOException {
jaroslav@1860
    61
        SieveTest sieve = new SieveTest();
jaroslav@1860
    62
        int now = time();
jaroslav@1860
    63
        int res = sieve.compute(10000);
jaroslav@1860
    64
        int took = time() - now;
jaroslav@1860
    65
        log("oneThousand in " + took + " ms");
jaroslav@1860
    66
        return res;
jaroslav@1860
    67
    }
jaroslav@836
    68
    
jaroslav@381
    69
    @Factory
jaroslav@381
    70
    public static Object[] create() {
jaroslav@1854
    71
        return VMTest.create(SieveTest.class);
jaroslav@1854
    72
    }
jaroslav@1854
    73
jaroslav@1854
    74
    @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg);")
jaroslav@1854
    75
    @Override
jaroslav@1854
    76
    protected void log(String msg) {
jaroslav@1857
    77
        System.err.println(msg);
jaroslav@381
    78
    }
jaroslav@381
    79
}