如何准确测量 API POST 请求的响应时间
How do I accurately measure response time from an API POST request
我编写了发送 POST 请求并等待服务器响应的代码。
我需要测量在服务器上处理信息所需的确切时间(响应时间)。
我注意到当我向服务器发送请求时,使用 rest assured 那里的时间比我通过 POSTMAN
所以我的问题是,我怎样才能最准确地测量时间,并接近我通过 POSTMAN
得到的结果
网上找了几天也没有找到解决方法
在此先感谢所有帮助者!!!
编辑
我用的放心图书馆
我有为我执行 POST 操作的功能
public static Response postSubTotal (String var_baseURI, String FileLocation){
RestAssured.baseURI =var_baseURI;
RequestSpecification request = RestAssured.given();
// Add a header stating the Request body is a JSON
request.header("Content-Type", "application/json");
// Add the Json to the body of the request
request.body(BaseJSON.getString(FileLocation));
// Post the request and check the response
Response response = request.post("/Transaction/SubTotal");
return response;
我需要从响应中取出准确的时间。
那么我该如何使用过滤器来做到这一点
我需要发送给此函数以从总时间中过滤处理时间,以便 我只剩下响应时间
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
}
如果您需要测量在 JVM 中花费的时间,您可以使用 Filter 来记录此信息:
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.
Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, and a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.
Filters are configured in the deployment descriptor of a web application.
Examples that have been identified for this design are:
- Authentication Filters
- Logging and Auditing Filters
- Image conversion Filters
- Data compression Filters
- Encryption Filters
- Tokenizing Filters
- Filters that trigger resource access events
- XSL/T filters
- Mime-type chain Filter
这种过滤器将测量接收到的请求和构建的响应之间的时间。它不考虑在网络上花费的时间,这不在您的直接控制之下,因为它受互联网连接的影响。
过滤器可让您集中记录每个传入请求在服务器上花费的时间。
过滤器记录时间的基本示例可以是
public class LogTimeFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
long before = System.currentTimeMillis();
chain.doFilter(request, response);
long after = System.currentTimeMillis();
long elapsedTime = after - before;
// Do what you need with the elapsed time
}
}
我编写了发送 POST 请求并等待服务器响应的代码。
我需要测量在服务器上处理信息所需的确切时间(响应时间)。 我注意到当我向服务器发送请求时,使用 rest assured 那里的时间比我通过 POSTMAN
所以我的问题是,我怎样才能最准确地测量时间,并接近我通过 POSTMAN
得到的结果网上找了几天也没有找到解决方法
在此先感谢所有帮助者!!!
编辑
我用的放心图书馆 我有为我执行 POST 操作的功能
public static Response postSubTotal (String var_baseURI, String FileLocation){
RestAssured.baseURI =var_baseURI;
RequestSpecification request = RestAssured.given();
// Add a header stating the Request body is a JSON
request.header("Content-Type", "application/json");
// Add the Json to the body of the request
request.body(BaseJSON.getString(FileLocation));
// Post the request and check the response
Response response = request.post("/Transaction/SubTotal");
return response;
我需要从响应中取出准确的时间。 那么我该如何使用过滤器来做到这一点 我需要发送给此函数以从总时间中过滤处理时间,以便 我只剩下响应时间
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
}
如果您需要测量在 JVM 中花费的时间,您可以使用 Filter 来记录此信息:
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, and a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks. Filters are configured in the deployment descriptor of a web application. Examples that have been identified for this design are:
- Authentication Filters
- Logging and Auditing Filters
- Image conversion Filters
- Data compression Filters
- Encryption Filters
- Tokenizing Filters
- Filters that trigger resource access events
- XSL/T filters
- Mime-type chain Filter
这种过滤器将测量接收到的请求和构建的响应之间的时间。它不考虑在网络上花费的时间,这不在您的直接控制之下,因为它受互联网连接的影响。
过滤器可让您集中记录每个传入请求在服务器上花费的时间。
过滤器记录时间的基本示例可以是
public class LogTimeFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
long before = System.currentTimeMillis();
chain.doFilter(request, response);
long after = System.currentTimeMillis();
long elapsedTime = after - before;
// Do what you need with the elapsed time
}
}