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

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

有帮助吗?

解决方案

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

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

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

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

其他提示

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

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

time curl -I 'http://server:3000'

像这样的事情可能会成功

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

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

// 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");

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

@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;
    }
}

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top