I have a Java program that uses OAuth for communication with a server to retrieve XML data.
It makes use of the Signpost OAuth library to connect with the source, and uses a standard way of reading the InputStream to access the XML that is returned.
Of late, I've noticed the slow time it's taken to retrieve the information and tests have revealed that some requests can take anywhere from 2000 ms up to 10000 ms (if it matters, the source server is in Europe, I am in Australia).
I added a timestamp after the OAuth communication ( request.connect()
) and again after the reading of the InputStream and here's the output:
Request #1: Communication: [6351ms] Data process: [403ms] Total: [6754ms]
Request #2: Communication: [1ms] Data process: [3121ms] Total: [3122ms]
Request #3: Communication: [1ms] Data process: [1297ms] Total: [1298ms]
Request #4: Communication: [0ms] Data process: [539ms] Total: [539ms]
My question: is the InputStream
returned to the HttpURLConnection
object as part of the connect()
method, or is it streamed back as I read from it (as the name suggests) and part of the actual connection process?
Secondary question: With the timing above, is the slow time most likely to be a problem with the server or my method of reading the InputStream?
For reference, here is the code in question:
long startTime = System.currentTimeMillis();
URL url = new URL(urlString);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
request.connect();
long connectionTime = System.currentTimeMillis();
InputStream is = request.getInputStream();
if (is != null) {
final BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
final StringBuffer s2 = new StringBuffer();
String line;
line = bufferedreader.readLine();
if (line != null) {
s2.append(line);
while ((line = bufferedreader.readLine()) != null) {
s2.append('\n');
s2.append(line);
}
}
bufferedreader.close();
rv = s2.toString();
}
long finishTime = System.currentTimeMillis();
long timeTaken = finishTime - startTime;
long totalConnectionTime = connectionTime - startTime;
long processDataTime = finishTime - connectionTime;
String info = "Communication: [" + totalConnectionTime +
"ms] Data process: [" + processDataTime +
"ms] Total: [" + timeTaken + "ms]";
Thanks in advance.
Based on the information provided, here are few observations and suggestions.
Bufferedreader.close()
and s2.toString();
) the delay appears to be in server BUT just for being sure, if possible, hit the URL using any browser and see the time taken to fetch the request. (from the code I see that you are just fetching the data from URL and hence should be easy to access the same using browser) InputStream
. openConnection()
does create the TCP connection, but unless you use a non-default streaming mode no data is sent until you either get the input stream or the reader or the response code. So sending the request is seen as part of getInputStream()
in your case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.