2014-01-06 40 views
1

我想通過slf4j使用兩種不同log4j模式佈局的性能基準。我正在使用卡尺(目前的HEAD版本)。卡尺基準中的非確定性分配行爲

這裏是我的標杆類:

public class PatternLayoutBenchmark { 

    // fully qualified names wherever there would otherwise be confusion btw 
    // log4j and slf4j 
    private static final org.apache.log4j.Level LEVEL = INFO; 

    @Param 
    private LayoutProvider layoutProvider; 
    private org.slf4j.Logger logger; 

    @BeforeExperiment 
    public void setup() throws Exception { logger = layoutProvider.getLogger(); } 

    @Benchmark 
    public int timeLogging(final int repetitions) { 
     int ct = 0; 
     for (int i = 0; i < repetitions; i++) { 
      logger.info("Now let's see the big difference this makes"); 
      ct++; 
     } 
     return ct; 
    } 

    enum LayoutProvider { 
     WITH_LINE_INFO { 
      @Override 
      PatternLayout createLayout() { 
       return new PatternLayout("%d %x %p %t %l: %m%n"); 
      } 
     }, 
     WITH_RECOMMENDED_PATTERN { 
      @Override 
      PatternLayout createLayout() { 
       return new PatternLayout("%d %x %p %t %c: %m%n"); 
      } 
     }; 
     abstract PatternLayout createLayout(); 

     public final org.slf4j.Logger getLogger() throws Exception { 
      RootLogger log4jLogger = new RootLogger(LEVEL); 
      Hierarchy h = new Hierarchy(log4jLogger); 
      new DefaultRepositorySelector(h); 
      log4jLogger.addAppender(new WriterAppender(createLayout(), new StringWriter())); 

      final Constructor<Log4jLoggerAdapter> constructor = 
        Log4jLoggerAdapter.class.getDeclaredConstructor(org.apache.log4j.Logger.class); 
      constructor.setAccessible(true); 
      return constructor.newInstance(log4jLogger); 
     } 
    } 

    public static void main(final String[] args) { 
     CaliperMain.main(PatternLayoutBenchmark.class, args); 
    } 

} 

當我運行它,我得到這個錯誤:

This selection yields 4 experiments. 
Starting experiment 1 of 4: {instrument=allocation, benchmarkMethod=timeLogging, vm=default, parameters={layoutProvider=WITH_LINE_INFO}}… 
com.google.caliper.runner.ProxyWorkerException: An exception occurred in a worker process. The stack trace is as follows: 
An unexpected exception has been thrown by the caliper runner. 
    java.lang.IllegalStateException: Your benchmark appears to have non-deterministic allocation behavior. Observed 1 instance(s) of char[376] (768 bytes) 
Please see https://sites.google.com/site/caliperusers/issues 
    at java.util.Arrays.copyOf(Arrays.java:2367) 
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130) 
    at org.apache.log4j.Category.callAppenders(Category.java:206) 
    ... 
    at org.apache.log4j.Category.forcedLog(Category.java:391) 
    at org.apache.log4j.Category.log(Category.java:856) 
    at org.slf4j.impl.Log4jLoggerAdapter.info(Log4jLoggerAdapter.java:304) 
    at some.pckg.name.PatternLayoutBenchmark.timeLogging(PatternLayoutBenchmark.java:37) in the baseline but only 0 in the actual measurement 

有一些配置我需要添加到我的標杆?或者我該如何解決這個問題?

+1

這似乎請在https://groups.google.com/forum/#!topic/caliper/Sv5POyNubME – gk5885

+0

上詢問同樣的問題。謝謝。在發佈到該組後,我只看到了在SO上發佈這些問題的建議。 –

回答

1

我得到了正確的答案from the caliper forum

的解決方案是通過添加標誌禁用分配儀器

-i runtime 

另一項建議,還曾是:

-Cinstrument.allocation.trackAllocations=false