简体   繁体   English

如何使用HTTPURLConnection在Java中获取TTFB(第一个字节的时间)

[英]How to get TTFB (Time To First Byte) in Java using a HTTPURLConnection

We are working in a Tool that measures performance of web sites in a not intrusive way (not modifying the web site source code). 我们正在使用一种工具,以一种非侵入性的方式(不修改网站源代码)来衡量网站的性能。 We have a small application in Java that fires request over the internet to our customer web sites and save the ResponseCode, LoadTime, Amount Of Bytes Loaded, etc. 我们有一个Java小应用程序,可以通过Internet向我们的客户网站发送请求,并保存ResponseCode,LoadTime,加载的字节数等。

One of the main metrics we measure is the TTFB. TTFB是我们衡量的主要指标之一。 I wonder if we are doing that the right way. 我想知道我们是否做对了。

We do a HttpURLConnection and save the difference of two time stamps as TTFB like bellow. 我们执行HttpURLConnection并将两个时间戳之差像下面这样保存为TTFB。

Calendar before = Calendar.getInstance();

HttpURLConnection connection = (HttpURLConnection)new URL( url ).openConnection();
connection.getResponseCode();

Calendar end = Calendar.getInstance();

//read the content

Calendar endContent = Calendar.GetInstance();

long TTFB = end.getTimeInMillis() - before().getTimeInMillis();
long justLoadTime = endContent.getTimeInMillis() - end.getTimeInMillis();

It is correct? 它是正确的? Getting the time till ResponseCode is the TTFB? 得到时间,直到ResponseCode是TTFB? Or until that some bytes of content already arrived? 还是直到内容的某些字节到达?

Is there more easy way to get these information? 有没有更简单的方法来获取这些信息?

Not in rare times, the difference between TTFB and justLoadTime is so small, in heavy weight pages. 在重磅页面中,TTFB和justLoadTime之间的差异并不是那么罕见。

I'm not sure you can get an accurate measure with Http Url connection. 我不确定使用Http Url连接能否获得准确的度量。 This sounds something much lower level and you probably need socket programming. 这听起来有些低级,您可能需要套接字编程。 The first byte that arrives will be a single TCP packet. 到达的第一个字节将是一个TCP数据包。 Operating at HTTP level you would have likely received multiple packets already. 在HTTP级别运行,您可能已经收到多个数据包。 So maybe clarify what "first byte" means for your metric. 因此,也许要弄清楚“第一个字节”对于您的指标意味着什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM