Spring启动应用程序:http响应时间的负值

Nis*_*obi 7 java spring servlet-filters spring-boot undertow

我根据这项技术开发了一个Web应用程序.

  1. 弹簧靴(底部起动器):1.4.2
  2. Java 7
  3. 操作系统:Centos
  4. Servlet过滤器:org.spring.web.filter.OncePerRequestFilter

在步骤4中,过滤器用于打印http请求/响应信息日志.这些数据包括:

  • (a)http url
  • (b)请求机构
  • (c)回应机构
  • (d)回应时间.

问题出现在(d)响应时间.但是,大部分响应时间都可以.但是对于少数http请求和反应,我发现具有负响应时间的日志(例如-1024ms).

这里我在Servlet Filter中手动计算了http响应时间

protected void doFilterInternal(...){

  long startTime = System.currentTimeMillis();
  ..
  some code
  filterChain.doFilter(httpRequest,httpResonse);
  ...
  long endTime = System.currentTimeMillis();
  long responseTime = endTime  - startTime
  log.debug(responseTIme); [SLF4J logger,logback.xml]

}
Run Code Online (Sandbox Code Playgroud)

Nis*_*obi 0

我使用了 google guava 库的 StopWatch api,它使用 System.nanoTime() 、java 时间 api。

https://github.com/google/guava/blob/master/guava/src/com/google/common/base/Stopwatch.java

import com.google.common.base.Stopwatch;
import java.util.concurrent.TimeUnit;


Stopwatch stopwatch = new Stopwatch();
stopwatch.start();

... 
do some code
....

stopwatch.stop();
long value = stopwatch.elapsedTime(TimeUnit.MICROSECONDS);
Run Code Online (Sandbox Code Playgroud)

它帮助我解决问题

谢谢@flopcodder @Jim Garrison