20

我正在使用连接到服务器的 Java 套接字。如果我发送 HEADER http 请求,我如何测量服务器的响应时间?我必须使用提供的 java 计时器,还是有更简单的方法?

我正在寻找一个简短的答案,我不想使用其他协议等。显然我也不希望有一个将我的应用程序与特定操作系统联系起来的解决方案。请人们,仅限 IN-CODE 解决方案。

4

7 回答 7

20

curl -s -w "%{time_total}\n" -o /dev/null http://server:3000

于 2012-09-21T08:08:10.983 回答
14

我想说这取决于您尝试测量的确切时间间隔,从您发送的请求的最后一个字节到您收到的响应的第一个字节的时间量?或者直到收到整个回复?还是您只想测量服务器端时间?

如果您尝试仅测量服务器端处理时间,您将很难计算出请求到达和响应返回所花费的网络传输时间量。否则,由于您自己通过 Socket 管理请求,因此您可以通过检查系统计时器并计算差值来测量任意两个时刻之间经过的时间。例如:

public void sendHttpRequest(byte[] requestData, Socket connection) {
    long startTime = System.nanoTime();
    writeYourRequestData(connection.getOutputStream(), requestData);
    byte[] responseData = readYourResponseData(connection.getInputStream());
    long elapsedTime = System.nanoTime() - startTime;
    System.out.println("Total elapsed http request/response time in nanoseconds: " + elapsedTime);
}

此代码将测量从您开始写出请求到完成接收响应的时间,并打印结果(假设您实现了特定的读/写方法)。

于 2008-09-16T03:44:09.577 回答
12

您可以在命令行上使用 time 和 curl 和 time。curl 的 -I 参数指示它仅请求标头。

time curl -I 'http://server:3000'
于 2008-09-16T03:11:13.557 回答
9

这样的事情可能会奏效

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.lang.time.StopWatch;
//import org.apache.commons.lang3.time.StopWatch

public class Main {

    public static void main(String[] args) throws URIException {
        StopWatch watch = new StopWatch();
        HttpClient client = new HttpClient();
        HttpMethod method = new HeadMethod("http://stackoverflow.com/");
        
        try {
            watch.start();
            client.executeMethod(method);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            watch.stop();
        }
        
        System.out.println(String.format("%s %s %d: %s", method.getName(), method.getURI(), method.getStatusCode(), watch.toString()));
        
    }
}
HEAD http://stackoverflow.com/ 200: 0:00:00.404
于 2008-09-16T03:38:21.353 回答
3

也许我错过了一些东西,但你为什么不直接使用:

// open your connection
long start = System.currentTimeMillis();
// send request, wait for response (the simple socket calls are all blocking)
long end = System.currentTimeMillis();
System.out.println("Round trip response time = " + (end-start) + " millis");
于 2008-09-16T03:41:14.483 回答
0

使用 AOP 拦截对套接字的调用并测量响应时间。

于 2008-09-16T04:54:13.200 回答
0
@Aspect
@Profile("performance")
@Component
public class MethodsExecutionPerformance {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("execution(* it.test.microservice.myService.service.*.*(..))")
    public void serviceMethods() {
    }

    @Around("serviceMethods()")
    public Object monitorPerformance(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        StopWatch stopWatch = new StopWatch(getClass().getName());
        stopWatch.start();
        Object output = proceedingJoinPoint.proceed();
        stopWatch.stop();
        logger.info("Method execution time\n{}", stopWatch.prettyPrint());
        return output;
    }
}

通过这种方式,您可以计算出您的服务的真实响应时间,而与网络速度无关。

于 2019-01-24T13:49:42.427 回答