I'm using a Java socket, connected to a server. If I send a HEADER http request, how can I measure the response time from the server? Must I use a provided java timer, or is there an easier way?
我正在使用连接到服务器的Java套接字。如果我发送HEADER http请求,我该如何测量服务器的响应时间?我必须使用提供的java计时器,还是有更简单的方法?
I'm looking for a short answer, I don't want to use other protocols etc. Obviously do I neither want to have a solution that ties my application to a specific OS. Please people, IN-CODE solutions only.
我正在寻找一个简短的答案,我不想使用其他协议等。显然我不想要一个将我的应用程序与特定操作系统联系起来的解决方案。请人,仅限IN-CODE解决方案。
6 个解决方案
#1
11
I would say it depends on what exact interval you are trying measure, the amount of time from the last byte of the request that you send until the first byte of the response that you receive? Or until the entire response is received? Or are you trying to measure the server-side time only?
我会说这取决于您尝试测量的确切间隔,从您发送的请求的最后一个字节到您收到的响应的第一个字节的时间量?或者直到收到整个回复?或者您是否只尝试测量服务器端时间?
If you're trying to measure the server side processing time only, you're going to have a difficult time factoring out the amount of time spent in network transit for your request to arrive and the response to return. Otherwise, since you're managing the request yourself through a Socket, you can measure the elapsed time between any two moments by checking the System clock and computing the difference. For example:
如果您只是尝试测量服务器端处理时间,那么您将很难计算出您的请求到达的网络传输所花费的时间以及返回的响应。否则,由于您是通过套接字自行管理请求,因此可以通过检查系统时钟和计算差异来测量任意两个时刻之间的经过时间。例如:
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);
}
This code would measure the time from when you begin writing out your request to when you finish receiving the response, and print the result (assuming you have your specific read/write methods implemented).
此代码将测量从开始写入请求到完成接收响应的时间,并打印结果(假设您已实现特定的读/写方法)。
#2
19
curl -s -w "%{time_total}\n" -o /dev/null http://server:3000
curl -s -w“%{time_total} \ n”-o / dev / null http:// server:3000
#3
12
You can use time and curl and time on the command-line. The -I argument for curl instructs it to only request the header.
您可以在命令行上使用时间和卷曲和时间。 curl的-I参数指示它仅请求标头。
time curl -I 'http://server:3000'
#4
7
Something like this might do the trick
像这样的东西可能会成功
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://*.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://*.com/ 200: 0:00:00.404
#5
2
Maybe I'm missing something, but why don't you just use:
也许我错过了什么,但你为什么不用它:
// 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");
#6
0
Use AOP to intercept calls to the socket and measure the response time.
使用AOP拦截对套接字的调用并测量响应时间。
#1
11
I would say it depends on what exact interval you are trying measure, the amount of time from the last byte of the request that you send until the first byte of the response that you receive? Or until the entire response is received? Or are you trying to measure the server-side time only?
我会说这取决于您尝试测量的确切间隔,从您发送的请求的最后一个字节到您收到的响应的第一个字节的时间量?或者直到收到整个回复?或者您是否只尝试测量服务器端时间?
If you're trying to measure the server side processing time only, you're going to have a difficult time factoring out the amount of time spent in network transit for your request to arrive and the response to return. Otherwise, since you're managing the request yourself through a Socket, you can measure the elapsed time between any two moments by checking the System clock and computing the difference. For example:
如果您只是尝试测量服务器端处理时间,那么您将很难计算出您的请求到达的网络传输所花费的时间以及返回的响应。否则,由于您是通过套接字自行管理请求,因此可以通过检查系统时钟和计算差异来测量任意两个时刻之间的经过时间。例如:
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);
}
This code would measure the time from when you begin writing out your request to when you finish receiving the response, and print the result (assuming you have your specific read/write methods implemented).
此代码将测量从开始写入请求到完成接收响应的时间,并打印结果(假设您已实现特定的读/写方法)。
#2
19
curl -s -w "%{time_total}\n" -o /dev/null http://server:3000
curl -s -w“%{time_total} \ n”-o / dev / null http:// server:3000
#3
12
You can use time and curl and time on the command-line. The -I argument for curl instructs it to only request the header.
您可以在命令行上使用时间和卷曲和时间。 curl的-I参数指示它仅请求标头。
time curl -I 'http://server:3000'
#4
7
Something like this might do the trick
像这样的东西可能会成功
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://*.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://*.com/ 200: 0:00:00.404
#5
2
Maybe I'm missing something, but why don't you just use:
也许我错过了什么,但你为什么不用它:
// 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");
#6
0
Use AOP to intercept calls to the socket and measure the response time.
使用AOP拦截对套接字的调用并测量响应时间。