使用Ellipticgroup / Brent Boyer进行java基准测试

时间:2015-06-13 17:49:46

标签: java benchmarking

我正在尝试使用此处提到的java基准测试: https://www.ibm.com/developerworks/java/library/j-benchmark2/ 这可以在这里下载: http://www.ellipticgroup.com/html/benchmarkingArticle.html

我尝试使用上面文章中的基本示例:

import bb.util.Benchmark;

public class ShortIndexesLoop {

    public static void main(String[] args) throws Exception {
        Callable<Integer> task = 
            new Callable<Integer>() { public Integer call() { return fibonacci(35); } };
            System.out.println("fibonacci(35): " + new Benchmark(task));
    }

    protected static int fibonacci(int n) throws IllegalArgumentException {
        if (n < 0) throw new IllegalArgumentException("n = " + n + " < 0");
        if (n <= 1) return n;
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

但是,每次执行此基准测试都会在运行基准测试之前以异常开始。

2015年6月13日下午1:45:37 StringUtil 警告:String的行为不符合预期;看原因

java.lang.Exception: substring does NOT share the same underlying char[] with its parent String
    at bb.util.StringUtil.inspectStringConstructor(StringUtil.java:84)
    at bb.util.StringUtil.<clinit>(StringUtil.java:75)
    at bb.io.ConsoleUtil.<clinit>(ConsoleUtil.java:81)
    at bb.util.Benchmark.sendUserMsg(Benchmark.java:1002)
    at bb.util.Benchmark.osSpecificPreparation(Benchmark.java:579)
    at bb.util.Benchmark.perform(Benchmark.java:541)
    at bb.util.Benchmark.<init>(Benchmark.java:464)
    at bb.util.Benchmark.<init>(Benchmark.java:439)
    at ShortIndexesLoop.main(fib.java:13)

似乎任何对基准测试的调用都会在StringUtil

中使用以下方法结束
private static final boolean stringContructorTrimsGarbage = inspectStringConstructor();

private static boolean inspectStringConstructor() {
    try {
            // first prove that substring shares the same underlying char[] as the parent String:
        String s1 = "abc123def";
        String s2 = s1.substring(3, 6);
        char[] value1 = (char[]) ReflectUtil.get(s1, "value");
        char[] value2 = (char[]) ReflectUtil.get(s2, "value");
        if (value1 != value2) throw new Exception("substring does NOT share the same underlying char[] with its parent String");
        if (value2.length != s1.length()) throw new Exception("value2.length = " + value2.length + " != s1.length() = " + s1.length());

            // second prove that the String(String) constructor trims garbage chars:
        String s3 = new String(s2);
        char[] value3 = (char[]) ReflectUtil.get(s3, "value");
        if (value3 == value2) throw new Exception("new String shares the same underlying char[] with its String arg");
        if (!(value3.length < value2.length)) throw new Exception("value3.length = " + value3.length + " is not < value2.length = " + value2.length);

        return true;
    }
    catch (Exception e) {
        LogUtil.getLogger2().logp(Level.WARNING, "StringUtil", "<clinit>", "String does not behave as expected; see cause", e);
        return false;
    }
}

我不介意例外,因为基准库确实给出了结果。但是,尝试将此代码编译为jar并将其作为jar运行是一个问题。 由于例外,其余代码不会在终端中执行

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我知道这是一个老问题,但是mabye我的解决方案对其他人也很有用:在这个基准框架的substring类的源代码中,记录了这个检查完成的原因。基于此,如果您有一个JDK stringContructorTrimsGarbage创建基础char数组的相关部分的副本,您只需注释newString标志,并在fread("\\Path\\file_name.csv",sep="|") Expecting 23 cols, but line 3747410 contains text after processing all cols. It is very likely that this is due to one or more fields having embedded sep='|' and/or (unescaped) '\n' characters within unbalanced unescaped quotes. fread cannot handle such ambiguous cases and those lines may not have been read in as expected. Please read the section on quotes in ?fread. In addition: Warning message: In fread("Path\\File_name.csv",sep="|") : Bumped column 5 to type character on data row 3422329, field contains 'NULL'. Coercing previously read values in this column from logical, integer or numeric back to character which may not be lossless; e.g., if '00' and '000' occurred before they will now be just '0', and there may be inconsistencies with treatment of ',,' and ',NA,' too (if they occurred in this column before the bump). If this matters please rerun and set 'colClasses' to 'character' for this column. Please note that column type detection uses the first 5 rows, the middle 5 rows and the last 5 rows, so hopefully this message should be very rare. If reporting to datatable-help, please rerun and include the output from verbose=TRUE. 方法中,你只需返回s。