传递stdout(jmeter)的输出并将其作为变量字符串传递给另一个采样器

时间:2017-11-03 07:30:17

标签: java jmeter beanshell

这是我的场景我正在j meter中的bean shell sampler中运行一个java程序,并在STD OUT控制台中获取所需的字符串。即使我已成功将所需的字符串写入输出文件。我需要从文件或控制台中提取字符串并将其传递给其他样本(所需的字符串与输出中的大量信息一起出现,因此我需要提取字符串,如“必需的字符串:跟随字符”,提前感谢

2 个答案:

答案 0 :(得分:0)

至少有两个选项:

  1. 使用OS Process Sampler代替Beanshell Sampler运行您的程序,这样您就可以从任何其他采样器获取输出并提取"有趣"使用Regular Expression Extractor的部分回复。
  2. 如果您不能或不想更改当前实现,可以使用__FileToString() function将文件内容读入JMeter变量,并再次使用上述正则表达式提取器从变量中获取所需的数据。

答案 1 :(得分:0)

何时需要指示输出使用以下代码

System.setOut(new PrintStream(new BufferedOutputStream(new
FileOutputStream("path/to/file/output.txt")), true));

说我正在调用一个包含在测试计划中的外部java文件(作为一些jar文件)

在Beanshell Sampler中编写此代码

import userdir.UserClass;

UserClass obj1 =new UserClass(); obj1.method1();

System.setOut(new PrintStream(new BufferedOutputStream(new
FileOutputStream("path/to/file/output.txt")), true));

现在,在STD OUT中由Userclass产生的任何输出将被定向到ouput.txt

从文件ouput.txt中提取Beanshell Post Processor中所需字符串的代码

import java.io.FileNotFoundException;   
import java.util.Scanner;   
import org.apache.jmeter.samplers.SampleResult;

File file = new File("path/to/file/output.txt");  
String word = "Req String1";  
String word2 = "Req String2";  
Scanner scanner = null;  
String line;   
String s1;  
String s2;     
try {
   scanner = new Scanner(file); }    
  catch(FileNotFoundException e) {     //handle this }

 //now read the file line by line     

 while (scanner.hasNextLine()) {  
line = scanner.nextLine();

if(line.contains(word)) {   
   log.info(line);  
   s1=line;    
  //now the entire line along with Req String1 will be stored in s1
}

if(line.contains(word2)) { 
  log.info(line);
   s2=line;
}
 }

log.info("The value in Encrypt str  ="+s1);  
log.info("The value in Signatr str  ="+s2);  
String[] s1 = s1.split(":");  
String[] s2 =s2.split(":");  
//since my value in the line is Req String1:needed data

props.put("s1",s1[1]);  
props.put("s2",s2[1]);

log.info("The value in ORIGINAL Req String1  ="+props.get("s1"));
log.info("The value in ORIGINAL Req String2  ="+props.get("s2"));
scanner.close();

希望这对所有

都有用