Java 从J单元的功能计时中删除设置时间
所以我需要测试从不同顺序的排序数组中获取第k个元素需要多长时间。我希望运行时间是O(N),但它看起来更像O(2^N)。这向我表明J-Unit正在计时我的setup()函数,即使它是用@Before注释的。我做错了什么Java 从J单元的功能计时中删除设置时间,java,junit,Java,Junit,所以我需要测试从不同顺序的排序数组中获取第k个元素需要多长时间。我希望运行时间是O(N),但它看起来更像O(2^N)。这向我表明J-Unit正在计时我的setup()函数,即使它是用@Before注释的。我做错了什么 import org.junit.Before; import org.junit.Test; public class GetKthTest { int[] sorted1; BubbleSort sort = new BubbleSort(); private int k =
import org.junit.Before;
import org.junit.Test;
public class GetKthTest {
int[] sorted1;
BubbleSort sort = new BubbleSort();
private int k = 50;
@Before
public void setUp() {
RandomListGen gen1 = new RandomListGen(80000);
sorted1 = sort.sort(gen1.getArray());
}
@Test
public void hundredThous() {
System.out.println(sorted1[k]);
}
}
事实上,我不认为你做错了什么,我认为JUnit中有一个bug 作为参考,我几乎有
@AfterClass
的确切问题
我已经用Eclipse中的泛型代码/JUnit4复制了您的问题
public class GetKthTest {
int[] sorted1;
int nbr = 8000000;
private int k = 50;
@Before
public void setUp() {
sorted1 = new int[nbr];
Random rand = new Random();
for (int i = 0; i < nbr; i++) {
sorted1[i] = rand.nextInt();
}
Arrays.sort(sorted1);
}
@Test
public void hundredThous() {
long start = System.currentTimeMillis();
System.out.println(sorted1[k]);
System.out.println("Time in ms: " + (System.currentTimeMillis() - start));
}
}
,
你没有做错什么。Junit在执行@before之前发送TestStart,在执行@after之后发送testFinished 如果可以将设置代码移动到@BeforeClass方法中,那么您确实可以使用自己的RunListener解决方案,例如:
public class RunJunitTestRunListener {
private static class MyListener extends RunListener {
private long runStart = 0L;
private long testStart = 0L;
@Override
public void testRunStarted(Description description) throws Exception {
System.out.println("runStarted");
runStart = System.currentTimeMillis();
super.testRunStarted(description);
}
@Override
public void testRunFinished(Result result) throws Exception {
System.out.println("runFinished " + (System.currentTimeMillis() - runStart) + "ms");
super.testRunFinished(result);
}
@Override
public void testStarted(Description description) throws Exception {
System.out.println("testStarted");
testStart = System.currentTimeMillis();
super.testStarted(description);
}
@Override
public void testFinished(Description description) throws Exception {
System.out.println("testFinished " + (System.currentTimeMillis() - testStart) + "ms");
super.testFinished(description);
}
}
public static void main(String[] args) {
JUnitCore core= new JUnitCore();
core.addListener(new MyListener());
core.run(TimingTest.class);
}
}
testRunStarted在@BeforeClass之前调用,testRunFinished在@AfterClass之后调用
另外,请参见我对的回答
作为一般建议,我会使用基准库来进行测量,因为这非常困难。junit没有告诉你运行了多长时间
100个小时
而不管其他函数(在测试树中)?Eran他可能是对的,我有一个类似的问题,没有正确的答案,但有@AfterClass
JUnit report 1,399s
public class RunJunitTestRunListener {
private static class MyListener extends RunListener {
private long runStart = 0L;
private long testStart = 0L;
@Override
public void testRunStarted(Description description) throws Exception {
System.out.println("runStarted");
runStart = System.currentTimeMillis();
super.testRunStarted(description);
}
@Override
public void testRunFinished(Result result) throws Exception {
System.out.println("runFinished " + (System.currentTimeMillis() - runStart) + "ms");
super.testRunFinished(result);
}
@Override
public void testStarted(Description description) throws Exception {
System.out.println("testStarted");
testStart = System.currentTimeMillis();
super.testStarted(description);
}
@Override
public void testFinished(Description description) throws Exception {
System.out.println("testFinished " + (System.currentTimeMillis() - testStart) + "ms");
super.testFinished(description);
}
}
public static void main(String[] args) {
JUnitCore core= new JUnitCore();
core.addListener(new MyListener());
core.run(TimingTest.class);
}
}