// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.text.NumberFormat;
import java.text.DecimalFormat;

public class JvmTest {
    private static NumberFormat nf = new DecimalFormat("0.0000");
    private static void printres(long t1, long t2, int n1, int n2, String ops) {
        double xperf = (double)n1 * (double)n2 / ((t2 - t1) / 1000.0) ;
        String sperf = nf.format(xperf / 1000000);
        System.out.println(sperf + " million " + ops + " per second");
    }
    private final static int NINT = 10000;
    private final static int NFP = 1000;
    private final static int NSTR = 1000;
    private final static int N = 1000000;
    public static void testint(int scale) {
        int nintscale = NINT / scale;
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < nintscale; i++) {
            int sum = i;
            for(int j = 0; j < N; j++) {
                sum = ((sum + 1) * 2 + 1) / 2;
            }
            if(sum != (i + N)) {
                System.out.println("Integer test error");
                System.exit(1);
            }
        }
        long t2 = System.currentTimeMillis();
        printres(t1, t2, nintscale, N, "integer operations");
    }
    public static void testfp(int scale) {
        int nfpscale = NFP / scale;
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < nfpscale; i++) {
            double sum = i;
            for(int j = 0; j < N; j++) {
                sum = ((sum + 1) * 2 + 1) / 2;
            }
            if(Math.abs(sum - (i + 1.5 * N)) > 1) {
                System.out.println("Floating point test error");
                System.exit(1);
            }
        }
        long t2 = System.currentTimeMillis();
        printres(t1, t2, nfpscale, N, "floating point operations");
    }
    private final static String ALFA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private final static int VMS_FACTOR = 100;
    public static void teststr(int scale) {
        int nstrscale = NSTR / scale;
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < nstrscale * VMS_FACTOR; i++) {
            StringBuffer sb = new StringBuffer("");
            for(int j = 0; j < N / VMS_FACTOR; j = j + 10) {
                String s = ALFA + ALFA;
                int ix = (i + j) % ALFA.length();
                sb.append(s.substring(ix, ix + 1) + s.substring(ix + 1, ix + 3) + s.substring(ix + 3, ix + 6) + s.substring(ix + 6, ix + 10));
            }
            int ix1 = (N / VMS_FACTOR) / 3;
            int ix2 = 2 * (N / VMS_FACTOR) / 3; 
            if(sb.length() != (N / VMS_FACTOR) || sb.charAt(ix1) != ALFA.charAt((i + ix1) % ALFA.length()) || sb.charAt(ix2) != ALFA.charAt((i + ix2) % ALFA.length())) {
                System.out.println("String test error");
                System.exit(1);
            }
        }
        long t2 = System.currentTimeMillis();
        printres(t1, t2, nstrscale, N / 10, "string operations");
    }
    private final static int REP = 10;
    public static void main(String[] args) {
        System.out.println("Java:");
        int scale = 1;
        if(args.length > 0) {
            scale = Integer.parseInt(args[0]);
        }
        for(int i = 0; i < REP; i++) {
            testint(scale);
        }
        for(int i = 0; i < REP; i++) {
            testfp(scale);
        }
        for(int i = 0; i < REP; i++) {
            teststr(scale);
        }
    }
}
