java如何使用Spring AOP或任何其他方法测量Junit 4测试用例调用的方法执行的时间
我需要通过JUnit 4测试框架中的每个方法来测量方法执行时间。 考虑这个测试:
package org.test.libtest
import org.some.lib.Class
..
public class testMyApp{
@Test
public void testSomething(){
//Asume following method returns a Long value after doing a complex calc
int a=100, b=100;
Long ac=org.some.lib.Class.doSomething(int a, int b);
Assert.assertEquals(100,ac);
}
..
..
}
要求是测量方法org.some.lib.Class.doSomething(..)
执行的时间
我们可以使用Spring AOP和Junit自己的@Rule、Stopwatch类通过测试方法testMyApp获得执行时间。 但是,我们如何通过测试方法测量底层方法调用执行的时间呢
# 1 楼答案
可以使用以下代码测量方法的执行时间
如果你想要更精确的时间测量,你可以用
System.nanoTime();
代替System.currentTimeMillis();