2012-11-08 106 views
6

我想測量調用RestTemplate.getForObject調用的HTTP GET請求的時間,而不需要解析響應所需的時間。所以只要遠程HTTP調用需要。我已經嘗試設置ClientHttpRequestInterceptor但我不認爲這是做正確的方式隨着時間似有不妥:測量Spring RestTemplate HTTP請求時間

public class PerfRequestSyncInterceptor implements ClientHttpRequestInterceptor { 
private Logger log = Logger.getLogger(this.getClass()); 

@Override 
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, 
     ClientHttpRequestExecution execution) throws IOException { 

    long start = System.nanoTime(); 
    ClientHttpResponse resp = execution.execute(request, body); 

    log.debug("remote request time: " 
      + ((System.nanoTime() - start) * Math.pow(10, -9))); 
    return resp; 
    } 
} 


電話:

RestTemplate rest = new RestTemplate(); 
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(); 
interceptors.add(new PerfRequestSyncInterceptor()); 
rest.setInterceptors(interceptors); 

Response inob = rest.getForObject(xmlURL, Response.class); 

我如何測量的時間RestTemplate HTTP請求?

+0

我有同樣的問題。您需要對響應對象進行方法調用以阻止執行,直到響應可用。 – jaydfwtx

回答

4

您可以使用AOP和Spring中內置的PerformanceMonitorInterceptor。您需要正確定義哪些方法需要攔截,然後才能測量。你可以這樣配置它:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 
    <bean id="springMonitoredService" 
     class="com.myorg.service.springmon.MyServiceSpringImpl"/> 


    <bean id="springMonitoringAspectInterceptor"   
class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor"> 
     <property name="loggerName"  
       value="com.myorg.SPRING_MONITOR"/>  
    </bean> 


    <aop:config> 
      <aop:pointcut id="springMonitoringPointcut" 
        expression="execution(* java.net.HttpURLConnection.connect(..))"/> 




       <aop:advisor pointcut-ref="springMonitoringPointcut" 
      advice-ref="springMonitoringAspectInterceptor"/>  
    </aop:config> 

</beans> 
+0

我應該配置哪種方法進行測量? – Mannaz

+0

內部RestTemplate使用java.net.HttpURLConnection來測量connect()方法。另外,您也可以嘗試getDoOutput()方法 –

+0

我可以請求您更新您的答案以指向此方法嗎? – Mannaz

1

你可以使用StopWatch來做到這一點。

public class PerfRequestSyncInterceptor implements ClientHttpRequestInterceptor { 

    private final static Logger LOG = LoggerFactory.getLogger(PerfRequestSyncInterceptor.class); 

    @Override 
    public ClientHttpResponse intercept(HttpRequest hr, byte[] bytes, ClientHttpRequestExecution chre) throws IOException { 
     Stopwatch stopwatch = Stopwatch.createStarted(); 
     ClientHttpResponse response = chre.execute(hr, bytes); 
     stopwatch.stop(); 

     LOG.info("method=" + hr.getMethod() + ", uri="+hr.getURI() + ", response_time=" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + ", response_code=" + response.getStatusCode().value()); 

     return response; 
    } 
} 

而且在restTemplate被實例

private final List<ClientHttpRequestInterceptor> requestInterceptors = new ArrayList<>(); 
requestInterceptors.add(new PerfRequestSyncInterceptor()); 
this.restTemplate.setInterceptors(requestInterceptors); 
0

我想衡量一個 RestTemplate.getForObject調用的HTTP GET請求的時間,而不需要解析 時間類迴應

我有同樣的要求。我想知道服務器響應時間,以確定服務器在沒有任何RestTemplate響應處理的情況下響應需要多長時間。我用地圖向HttpClientBuilder添加了兩個攔截器,這樣我就可以測量低層請求和響應之間的時間。

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); 

// Attach interceptors 
ResponseTimeInterceptor interceptor = new ResponseTimeInterceptor(); 
httpClientBuilder.addInterceptorFirst((HttpRequestInterceptor) interceptor); 
httpClientBuilder.addInterceptorFirst((HttpResponseInterceptor) interceptor); 

// Use client with RestTemplate or on its own 
HttpClient client = httpClientBuilder.build(); 

這裏是最小的雙重職責攔截:

public class ResponseTimeInterceptor implements HttpRequestInterceptor, HttpResponseInterceptor { 
    private final Map<HttpContext, Long> requestMap = new MaxSizeHashMap<>(50); 

    @Override 
    public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException { 
     requestMap.put(httpContext, System.currentTimeMillis()); 
    } 

    @Override 
    public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException { 
     long startTime = requestMap.getOrDefault(httpContext, 0L); 
     long diff = System.currentTimeMillis() - startTime; 
     System.out.println("Response time: " + diff + "ms"); 
    } 
} 

響應攔截返回後,響應數據繼續進入RestTemplate響應處理。

注意:MaxSizeHashMap取自https://stackoverflow.com/a/5601377

相關問題