import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.*;
public class createFXRates_Files {
public static void main(String[] args)throws IOException
{
int totalrecords = Integer.parseInt(args[0]);
int randomrow;
String FileName = null;
//Creating a file
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
FileName = "FXrates."+sdf.format(date);
File outfile = new File("C:\\SAMIR\\Projects\\DataHub_BMO\\JAVATEST\\"+FileName);
if(outfile.exists()) {
outfile.delete();
}
outfile.createNewFile();
FileWriter fw = new FileWriter(outfile);
//Reading a sampler file
Scanner sc = new Scanner(new File("C:\\SAMIR\\Projects\\DataHub_BMO\\FXrates.20190903"));// sample file path to scanner
ArrayList<String> rows= new ArrayList<String>();
while(sc.hasNext()){
String data = sc.nextLine();
rows.add(data);
}
Random random = new Random();
for(int i=0; i<totalrecords; i++) {
randomrow = random.nextInt(10000);
System.out.println(""+Integer.toString(randomrow));
fw.write(""+rows.get(randomrow)+"\n");
System.out.println(rows.get(randomrow));
}
sc.close();
fw.close();
}
}
我需要在jmeter的bean shell采样器中转换上述Java代码。尝试这样做导致“ In file:内联评估:``import java.io. *; import java.text.SimpleDateFormat; import java.util.ArrayLis。。。''在第27行遇到” =“,第39栏。”错误。似乎代码编译器无法解析Collection类。即使代码包含“ import java.util.ArrayList;”
的导入,Beanshell不特别支持diamond operators,通常卡在Java SE 1.5 language level上,因此您需要删除这些<String>
位以使代码正常工作。
请注意,用于编写脚本的since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language,在这种情况下,您将不必进行任何更改,而且Groovy has much better performance comparing with Beanshell并在Java SDK的顶部添加了一些"syntax sugar"
最好使用JSR223 Sampler而不是Beanshell Sampler。在JSR223 Sampler中:将以下代码放入脚本区域
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.*;
int totalrecords = Integer.parseInt(args[0]);
int randomrow;
String FileName = null;
//Creating a file
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
FileName = "FXrates."+sdf.format(date);
File outfile = new File("C:\\SAMIR\\Projects\\DataHub_BMO\\JAVATEST\\"+FileName);
if(outfile.exists()) {
outfile.delete();
}
outfile.createNewFile();
FileWriter fw = new FileWriter(outfile);
//Reading a sampler file
Scanner sc = new Scanner(new File("C:\\SAMIR\\Projects\\DataHub_BMO\\FXrates.20190903"));// sample file path to scanner
ArrayList<String> rows= new ArrayList<String>();
while(sc.hasNext()){
String data = sc.nextLine();
rows.add(data);
}
Random random = new Random();
for(int i=0; i<totalrecords; i++) {
randomrow = random.nextInt(10000);
log.info(""+Integer.toString(randomrow));
fw.write(""+rows.get(randomrow)+"\n");
log.info(rows.get(randomrow));
}
sc.close();
fw.close();