<p>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?</p>

<p>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. </p>

[{'answer_id': 69105, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 4, 'selected': False, 'text': "<p>You can use time and curl and time on the command-line.  The -I argument for curl instructs it to only request the header.</p>\n\n<pre><code>time curl -I 'http://server:3000'\n</code></pre>\n"}, {'answer_id': 69195, 'author': 'Dave Cheney', 'author_id': 6449, 'author_profile': 'https://Stackoverflow.com/users/6449', 'pm_score': 3, 'selected': False, 'text': '<p>Something like this might do the trick</p>\n<pre class="lang-java prettyprint-override"><code>import java.io.IOException;\n\nimport org.apache.commons.httpclient.HttpClient;\nimport org.apache.commons.httpclient.HttpMethod;\nimport org.apache.commons.httpclient.URIException;\nimport org.apache.commons.httpclient.methods.HeadMethod;\nimport org.apache.commons.lang.time.StopWatch;\n//import org.apache.commons.lang3.time.StopWatch\n\npublic class Main {\n\n    public static void main(String[] args) throws URIException {\n        StopWatch watch = new StopWatch();\n        HttpClient client = new HttpClient();\n        HttpMethod method = new HeadMethod(&quot;http://stackoverflow.com/&quot;);\n        \n        try {\n            watch.start();\n            client.executeMethod(method);\n        } catch (IOException e) {\n            e.printStackTrace();\n        } finally {\n            watch.stop();\n        }\n        \n        System.out.println(String.format(&quot;%s %s %d: %s&quot;, method.getName(), method.getURI(), method.getStatusCode(), watch.toString()));\n        \n    }\n}\n</code></pre>\n<pre class="lang-text prettyprint-override"><code>HEAD http://stackoverflow.com/ 200: 0:00:00.404\n</code></pre>\n'}, {'answer_id': 69210, 'author': 'Kevin Day', 'author_id': 10973, 'author_profile': 'https://Stackoverflow.com/users/10973', 'pm_score': 2, 'selected': False, 'text': '<p>Maybe I\'m missing something, but why don\'t you just use:</p>\n\n<pre><code>// open your connection\nlong start = System.currentTimeMillis();\n// send request, wait for response (the simple socket calls are all blocking)\nlong end = System.currentTimeMillis();\nSystem.out.println("Round trip response time = " + (end-start) + " millis");\n</code></pre>\n'}, {'answer_id': 69223, 'author': 'Dave L.', 'author_id': 3093, 'author_profile': 'https://Stackoverflow.com/users/3093', 'pm_score': 5, 'selected': True, 'text': '<p>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?</p>\n\n<p>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 timer and computing the difference.  For example:</p>\n\n<pre><code>public void sendHttpRequest(byte[] requestData, Socket connection) {\n    long startTime = System.nanoTime();\n    writeYourRequestData(connection.getOutputStream(), requestData);\n    byte[] responseData = readYourResponseData(connection.getInputStream());\n    long elapsedTime = System.nanoTime() - startTime;\n    System.out.println("Total elapsed http request/response time in nanoseconds: " + elapsedTime);\n}\n</code></pre>\n\n<p>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).</p>\n'}, {'answer_id': 69488, 'author': 'Adisesha', 'author_id': 11092, 'author_profile': 'https://Stackoverflow.com/users/11092', 'pm_score': 0, 'selected': False, 'text': '<p>Use AOP to intercept calls to the socket and measure the response time.</p>\n'}, {'answer_id': 12526594, 'author': 'A B', 'author_id': 167362, 'author_profile': 'https://Stackoverflow.com/users/167362', 'pm_score': 4, 'selected': False, 'text': '<p><code>curl -s -w "%{time_total}\\n" -o /dev/null http://server:3000</code></p>\n'}, {'answer_id': 54348183, 'author': 'Sudabe-Neirizi', 'author_id': 9820210, 'author_profile': 'https://Stackoverflow.com/users/9820210', 'pm_score': 0, 'selected': False, 'text': '<pre><code>@Aspect\n@Profile("performance")\n@Component\npublic class MethodsExecutionPerformance {\n    private final Logger logger = LoggerFactory.getLogger(getClass());\n\n    @Pointcut("execution(* it.test.microservice.myService.service.*.*(..))")\n    public void serviceMethods() {\n    }\n\n    @Around("serviceMethods()")\n    public Object monitorPerformance(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {\n        StopWatch stopWatch = new StopWatch(getClass().getName());\n        stopWatch.start();\n        Object output = proceedingJoinPoint.proceed();\n        stopWatch.stop();\n        logger.info("Method execution time\\n{}", stopWatch.prettyPrint());\n        return output;\n    }\n}\n</code></pre>\n\n<p>In this way, you can calculate the real response time of your service independent of network speed. </p>\n'}]
['https://Stackoverflow.com/questions/68999', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10889/']