there nice libraries this apache, that's little bit complex purpose. need best estimate of http latency (the time takes get connected server, regardless of transfer speed).
i tried http connection code this answer:
private void doping() { //remember time before connection long millis = system.currenttimemillis(); try (bufferedreader reader = new bufferedreader(new inputstreamreader(url.openstream(), "utf-8"))) { //we don't need data reader.close(); //times array store our results times.add((int)(system.currenttimemillis()-millis)); //i see lot's of these in console, it's working system.out.println("request done..."); } //if internet dead, throw exception? catch(exception e) { times.add(-1); } }
the thing not sure measuring. looping through values gave me results:
testing connection http://www.seznam.cz threads: 5 loops per thread: 50 given waiting results. average time connection: 53.8 [ms] failures: 0.0% testing connection http://www.seznam.cz threads: 5 loops per thread: 100 average time connection: 43.58 [ms] failures: 0.0% testing connection http://www.seznam.cz threads: 5 loops per thread: 400 average time connection: 30.145 [ms] failures: 0.0% testing connection http://www.stackoverflow.com threads: 5 loops per thread: 30 given waiting results. average time connection: 4006.1111111111113 [ms] failures: 0.0% testing connection http://www.stackoverflow.com threads: 5 loops per thread: 80 given waiting results. average time connection: 2098.695652173913 [ms] failures: 0.0% testing connection http://www.stackoverflow.com threads: 5 loops per thread: 200 given waiting results. average time connection: 0.0 [ms] //whoops, connection dropped again failures: 100.0% //some random invalid url testing connection http://www.sdsfdser.tk/ threads: 4 loops per thread: 20 average time connection: 0.0 [ms] failures: 100.0%
not not sure if calculated wanted (though reflects experience), not sure happes in non standard cases.
- does url handle timeouts?
- will allways throw exception on timeout?
while keeping in mind project supposed simple , lightweight, tell me if i'm doing right?
i think hailin suggested create raw socket
, connect server instead of using urlconnection
. tried both, , i'm getting higher latency version. think opening urlconnection
must doing additional stuff in background, though i'm not sure what.
anyway, here's version using socket
(add exception handling needed):
socket s = new socket(); socketaddress = new inetsocketaddress("www.google.com", 80); int timeoutmillis = 2000; long start = system.currenttimemillis(); try { s.connect(a, timeoutmillis); } catch (sockettimeoutexception e) { // timeout } catch (ioexception e) { // other exception } long stop = system.currenttimemillis(); times.add(stop - start); try { s.close(); } catch (ioexception e) { // closing failed }
this resolves hostname (www.google.com in example), establishes tcp connection on port 80 , adds milliseconds took times
. if don't want time dns resolution in there, can create inetaddress
inetaddress.getbyname("hostname")
before start timer , pass inetsocketaddress
constructor.
edit: inetsocketaddress
's constructor resolves host name right away, constructing resolved ip address shouldn't make difference.