benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/SieveTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 25 Jan 2016 06:40:40 +0100
changeset 1857 f1344425bcb1
parent 1854 826eb936c9a8
child 1860 4ce38f21f4cd
permissions -rw-r--r--
Some operations are faster when included in the generated code rather than dispatched to Number.prototype
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@537
    39
    @Compare(scripting = false) 
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@1857
    49
    @Compare(scripting = false)
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@836
    58
    
jaroslav@381
    59
    @Factory
jaroslav@381
    60
    public static Object[] create() {
jaroslav@1854
    61
        return VMTest.create(SieveTest.class);
jaroslav@1854
    62
    }
jaroslav@1854
    63
jaroslav@1854
    64
    @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg);")
jaroslav@1854
    65
    @Override
jaroslav@1854
    66
    protected void log(String msg) {
jaroslav@1857
    67
        System.err.println(msg);
jaroslav@381
    68
    }
jaroslav@381
    69
}