I am using Spring Feign clients. Is there a way to get the time it took for a response to be received after a request was sent? I have a lot of requests mapped and would like a clean way to assert in tests that a response was received within a certain time.
Thanks
You can use System.nanoTime():
long startTime = System.nanoTime();
// make your request
long elapsedTime = System.nanoTime()-startTime;
You can do this with a generic method as well. Please find snippet below:
myMethod() {
Date startDate = new Date();
.... body
return timedReturn(LOGGER, new Object() {}.getClass().getEnclosingMethod().getName(), startDate.getTime(), response);
}
public class MY_Utilities {
public static <T> T timedReturn(final Logger LOGGER, String method, long start, T object) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Execution of method %s took %05d ms", method, System.currentTimeMillis() - start));
}
return object;
}
}
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.