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

3
我正在尝试使用这里提到的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警告:字符串的行为与预期不符,请查看原因。

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运行时,会出现问题。由于异常,终端中的其余代码将无法执行。

有人有解决此问题的任何想法吗?


可能与Java 7中的实现更改不兼容:http://java.dzone.com/articles/changes-stringsubstring-java-7 - Marvin
有没有办法在不改变到Java 1.6或1.8的情况下解决这个问题? - Quantico
1
Java 8无法解决此问题,因为这个改变仍然存在。除此之外,你只能等待库更新或尝试自己修补它。编辑:或者尝试使用不同的库,但我无法推荐或甚至命名一个。 - Marvin
1个回答

2

我知道这是一个老问题,但也许我的解决方案对其他人有用:在此基准测试框架的 StringUtil 类的源代码中,记录了为什么要进行此检查。基于此,如果您有一个JDK,在该JDK中, substring 创建底层字符数组的相关部分的副本,则可以简单地注释掉 stringContructorTrimsGarbage 标志,并在 newString 方法中,您只需返回s。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接