如何在没有网络传输时间的情况下测量请求的执行时间?
在我的阀门中,我得到了这样的http查询的执行时间:
public void invoke(Request request, Response response)
throws IOException, ServletException {
long t1 = System.currentTimeMillis();
getNext().invoke(request, response);
long t2 = System.currentTimeMillis();
long time = t2 - t1;
...
}
我怀疑我以这种方式得到的时间不仅给了我服务器端执行时间,还给了我网络时间?是否可以 ?
(因为根据执行请求的客户端,测量的平均时间与一个 ip 的平均时间不同,我的平均时间为 70 毫秒,另一个 ip 的平均时间为 270 毫秒)
我在 Filter: 中编写了完全相同的代码
long before = System.currentTimeMillis();
chain.doFilter(request, response);
long after = System.currentTimeMillis();
,但在我的测试中执行时间Valve 和过滤器中的值是相同的...
我更感兴趣的是仅获取 servlet 的执行时间。 但如果我还能得到最后一个字节发送的时间,我会很感兴趣。
非常感谢您向我澄清有关阀门和过滤器的知识。
In my valve I get the execution time of my http query like that :
public void invoke(Request request, Response response)
throws IOException, ServletException {
long t1 = System.currentTimeMillis();
getNext().invoke(request, response);
long t2 = System.currentTimeMillis();
long time = t2 - t1;
...
}
I suspect that the time I get this way, not only give me the server side execution time but also the network time ? Is it possible ?
(Because depending on the client who do the request the average time measured is not the same from one ip I have 70ms average time and another ip 270ms)
I have written the exact same code in a Filter:
long before = System.currentTimeMillis();
chain.doFilter(request, response);
long after = System.currentTimeMillis();
, but in my test the execution time in the Valve and in the filter are the same...
I am more interested in getting the execution time of my servlet only.
But if I could get also the time when the last byte is sent, I would be interested.
Thanks a lot to clarify me what is possible to know with valves and filters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的。这样至少可以将一部分涉及请求体传输的网络时间包含在总时间中。一旦请求标头被完全读取和解析,阀门/过滤器/servlet 代码就会被直接调用。此时请求正文不一定已被完全读取。请求正文包含客户端通过网络发送的作为请求一部分的任何内容(例如提交的表单数据)。仅当过滤器/servlet 开始通过例如
getParameter()
、getReader()
、getInputStream()
、等等,那么请求体的所有字节都会被实际传输。您可能希望重写执行时间测量,使其仅在请求正文完全读取后才开始。这意味着您确实必须在 servlet 内部对其进行测量。
(请注意,我使用了
System#nanoTime()
,因为它具有更好的准确性)That's correct. At least, a partial of the network time involving transferring the request body is included in the total time this way. The valve/filter/servlet code get invoked directly once the request headers have been fully read and parsed. The request body is not necessarily fully read at that point. The request body contains whatever the client sends over network as part of the request (e.g. submitted form data). Only when the filter/servlet starts to read and parse the full request body by e.g.
getParameter()
,getReader()
,getInputStream()
, etc, then all the bytes of the request body will actually be transferred.You may want to rewrite your execution time measurement in such way that it only starts after the request body has been fully read. This means that you really have to measure it right inside the servlet.
(note that I used
System#nanoTime()
as it has a much better accuracy)