File: BenchParser.java

package info (click to toggle)
jruby 9.3.9.0%2Bds-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,856 kB
  • sloc: ruby: 517,823; java: 260,094; xml: 31,930; ansic: 5,777; yacc: 4,973; sh: 1,163; makefile: 105; jsp: 48; tcl: 40; exp: 11
file content (44 lines) | stat: -rw-r--r-- 1,535 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.jruby.Ruby;

import org.jruby.runtime.load.LoadServiceResourceInputStream;

public class BenchParser {
    public static final int N = 1000;
    public static void main(String[] args) {
        try {
            String file = args[0];
            InputStream in = getContents(file);
            int iterations = getIterations(args);

            System.out.println("Parsing " + file + " " + iterations + " times");
            Ruby runtime = Ruby.getGlobalRuntime();
            long start = System.nanoTime();
            for (int i = 0; i < iterations; i++) {
                long start2 = System.nanoTime();
                runtime.parseFile(file, in, null);
                System.out.println("Parse took: " + seconds(start2, System.nanoTime()) + "ms");
            }
            System.out.println("Total parse took: " + seconds(start, System.nanoTime()) + "ms");
        } catch (Exception e) {
            System.err.println("Parse bench failed: " + e.getMessage());
            e.printStackTrace(System.err);
        }
    }

    private static double seconds(long start, long end) {
        return (end - start) * 1.0 / 1000000.0;
    }

    private static int getIterations(String[] args) {
        return args.length > 1 ? Integer.valueOf(args[1]) : N;
    }

    private static InputStream getContents(String file) throws IOException {
        return new LoadServiceResourceInputStream(new FileInputStream(file));
    }
}