I have async method where I use DeferredResult as return type. I want write junit test for that method where I call this method in loop, e.g 100 times, and I need measure execution time for every call of that method.
Here is method sample:
@Transactional(readOnly = true)
@Override
public DeferredResult foo() {
DeferredResult dr = new DeferredResult(5000L, "timeout");
dr.onCompletion(() -> {
// do some stuff
});
deferredResults.add(dr);
return dr;
}
created deferredResult I add into collection, and I iterate that collection in another method where I set some result, and then is dr returned.
Can you show my how should looks like test where I will be able measure execution time of multiple calls of that method?
@Test
public void executionTimeTest() {
for (int i = 0; i < 100; i++) {
asyncService.foo();
}
// here I need get execution time for each call
}
Thanks.
I think the best way to solve this question is adding additional data in DeferredResult class as it is recommended by spring docs. Precisely, the following sentence in spring docs points out this possibility.
Given this possibility, you can extend the DeferredResult and add a start and end time:
Then, your Async Service can be similar to this one:
Finally, in your test, you can retrieve the total time of each execution: