使用JMH进行JMH Micro Benchmarking for java静态方法返回OutOfBounds异常

时间:2018-05-28 11:09:20

标签: 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(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));

} 

因为我想看看getPropertyById的工作速度有多快,我想用1000个随机整数元素填充Lists id / property / email,然后从同一个索引(n)的每个数组中获取一个随机元素。

这是实际的方法,但我拿出扫描仪才能使用随机数据,因此基准测试不必等待用户输入

public static void getPropertybyId(){
   Scanner reader = new Scanner(System.in);  // Reading from System.in
   System.out.println("Enter an id number to search properties: ");
   int n = reader.nextInt(); // Scans the next token of the input as an int.
  reader.close();
  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)

0 个答案:

没有答案