2017-01-03 3 views
2

JMH을 사용하여 벤치 마크를 수행하려는 응용 프로그램이 있습니다. 이 통합에 대한 모든 참조가 유용 할 것입니다.JMH 벤치마킹 스프링 부트 응용 프로그램

+0

IMHO'jmh'는 벤치마킹 응용 프로그램에는 적합하지 않지만 특정 메서드에만 적합합니다. –

+1

스프링 MVC 애플리케이션 용으로 bechmarking을 수행 할 권한이 있습니다. 스프링 부트 애플리케이션 용으로 사용해야합니다. –

답변

5

솔루션은 생각보다 쉽습니다. 중요한 부분은 벤치 마크가 초기화 될 때 스프링 - 부트 애플리케이션을 시작하는 것이다. 구성 컨텍스트에 대한 클래스 수준 변수를 정의하고 벤치 마크를 설정하는 동안 참조를 제공하십시오. 벤치 마크에서 bean 메소드를 호출하십시오.

import java.io.File; 
import java.net.URL; 
import java.net.URLClassLoader; 
import java.util.concurrent.TimeUnit; 
import org.openjdk.jmh.annotations.Benchmark; 
import org.openjdk.jmh.annotations.BenchmarkMode; 
import org.openjdk.jmh.annotations.Level; 
import org.openjdk.jmh.annotations.Mode; 
import org.openjdk.jmh.annotations.OutputTimeUnit; 
import org.openjdk.jmh.annotations.Scope; 
import org.openjdk.jmh.annotations.Setup; 
import org.openjdk.jmh.annotations.State; 
import org.openjdk.jmh.infra.Blackhole; 
import org.openjdk.jmh.runner.Runner; 
import org.openjdk.jmh.runner.options.Options; 
import org.openjdk.jmh.runner.options.OptionsBuilder; 
import org.springframework.boot.SpringApplication; 
import org.springframework.context.ConfigurableApplicationContext; 

@BenchmarkMode(Mode.Throughput) @OutputTimeUnit(TimeUnit.MINUTES) 
@State(Scope.Thread) 
public class ProcessFeedBenchMark { 

    public static void main(String args[]) throws Exception { 
     URLClassLoader classLoader = (URLClassLoader) ProcessFeedBenchMark.class.getClassLoader(); 
     StringBuilder classpath = new StringBuilder(); 
     for(URL url : classLoader.getURLs()) 
      classpath.append(url.getPath()).append(File.pathSeparator); 
     classpath.append("/D:/work/zymespace/benchmark/src/main/resources/").append(File.pathSeparator); 
     System.out.print(classpath.toString()); 
     System.setProperty("java.class.path", classpath.toString()); 

     Options opt = new OptionsBuilder() 
     .include(ProcessFeedBenchMark.class.getName() + ".*") 
     .timeUnit(TimeUnit.MILLISECONDS) 
     .threads(1) 

     .shouldFailOnError(true) 
     .shouldDoGC(true) 
     .build(); 

     new Runner(opt).run(); 
    } 
    static ConfigurableApplicationContext context; 

    private BenchmarkTestService service; 

    @Setup (Level.Trial) 
    public synchronized void initialize() { 
     try { 
      String args = ""; 
      if(context == null) { 
       context = SpringApplication.run(BenchmarkSpringBootStater.class, args); 
      } 
      service = context.getBean(BenchmarkTestService.class); 
      System.out.println(service); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Benchmark 
    public void benchmark1 (ProcessFeedBenchMark state, Blackhole bh) { 
     try { 
      service.li(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+3

이 벤치 마크에서 벗어난 수치는 신뢰할 수 없습니다. 서비스 구성과 프록시 메커니즘에 의존하고있을 것입니다. 또한 대부분의 시간은 결과를 인쇄하는 데 소요됩니다. 오히려 메서드에서 서비스 값을 반환하고 JMH가 이스케이프를 처리하도록해야합니다. 이것은 JMH를 사용하는 방법이 아닙니다. –

+0

li() 메서드에 적합한 벤치 마크를 얻으려면 System.out.println 문이 제거됩니다. –

+0

환상적입니다. 예를 들어 주셔서 감사합니다. –

관련 문제