使用JMH for java静态方法的JMH微基准测试返回OutOfBounds异常
我设置了这个基准测试使用JMH for java静态方法的JMH微基准测试返回OutOfBounds异常,java,list,benchmarking,microbenchmark,jmh,Java,List,Benchmarking,Microbenchmark,Jmh,我设置了这个基准测试 static Random rand = new Random(); static int n = rand.nextInt(999) + 1; @Setup public static final void setup(){ int x = 1000; for (int i = 0; i < x; i++){ id.add(rand.nextInt(500) + 1); property_address.add(rand.nextInt(10
static Random rand = new Random();
static int n = rand.nextInt(999) + 1;
@Setup
public static final void setup(){
int x = 1000;
for (int i = 0; i < x; i++){
id.add(rand.nextInt(500) + 1);
property_address.add(rand.nextInt(1000) + 1);
email.add(rand.nextInt(1000) + 1);
owner_address.add(rand.nextInt(1000) + 1);
price.add(rand.nextInt(1000) + 1);
date_sold.add(rand.nextInt(1000) + 1);
}
System.out.println("Setup Complete");
}
@Benchmark
public static void getPropertybyId(){
System.out.println(id.get(n) +", "+ property_address.get(n) +", "+
first_name.get(n) +", "+ last_name.get(n) +", "+
email.get(n) +", "+
owner_address.get(n) +", "+
price.get(n)+", "+
date_sold.get(n));
}
我得到以下错误
java.lang.IndexOutOfBoundsException: Index: 902, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.sample.MyBenchmark.getPropertybyId(MyBenchmark.java:80)
您在设置方法中填写了6个不同的列表,但在基准测试方法中使用了8个不同的列表。当然,这8个列表中有2个是零元素的,就像你的异常所说的(大小:0):你能告诉我为什么我的基准预热迭代会无限期运行吗?我有一个建议:不要在JMH测试中打印。您将主要测试控制台的IO性能。相反,使用
return
:return id.get(n)+“、“+property\u address.get(n)+”、“+first\u name.get(n)+”、“+last\u name.get(n)+”、“+email.get(n)+”、“+owner\u address.get(n)+”、“+price.get(n)+”、“+date\u sall.get(n)代码>我刚刚做了,而且成功了,非常感谢你花时间帮助我
java.lang.IndexOutOfBoundsException: Index: 902, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.sample.MyBenchmark.getPropertybyId(MyBenchmark.java:80)