传递 stdout(jmeter) 的输出并将其作为变量字符串传递给另一个采样器
Pass an output of stdout(jmeter) and pass it as variable string to another sampler
这是我的场景,我是 运行 一个 java bean shell 采样器中的程序,在 j meter 中,并在 STD OUT 控制台中获取所需的字符串。即使我已经成功地将所需的字符串写入输出文件。我需要从文件或控制台中提取字符串并将其传递给其他示例(所需的字符串与输出中的大量信息一起存在,因此我需要提取字符串,如 "Required String:following Character" 提前致谢
至少有2个选项:
- 运行 您的程序使用 OS Process Sampler instead of Beanshell Sampler, this way you will be able to get the output like from any other sampler and extract "interesting" part of the response using Regular Expression Extractor。
- 如果您不能或不想更改当前实现,您可以使用 __FileToString() function 将文件内容读入 JMeter 变量,然后再次使用上述正则表达式提取器从变量中获取所需数据.
当你需要直接输出时使用下面的代码
System.setOut(new PrintStream(new BufferedOutputStream(new
FileOutputStream("path/to/file/output.txt")), true));
假设我正在调用包含在测试计划中的外部 java 文件(作为一些 jar 文件)
在 Beanshell 采样器中编写此代码
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 中产生的任何输出都将被定向到 ouput.txt
在 Beanshell 中提取所需字符串的代码 Post 从文件处理器 ouput.txt
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();
希望对大家有用
这是我的场景,我是 运行 一个 java bean shell 采样器中的程序,在 j meter 中,并在 STD OUT 控制台中获取所需的字符串。即使我已经成功地将所需的字符串写入输出文件。我需要从文件或控制台中提取字符串并将其传递给其他示例(所需的字符串与输出中的大量信息一起存在,因此我需要提取字符串,如 "Required String:following Character" 提前致谢
至少有2个选项:
- 运行 您的程序使用 OS Process Sampler instead of Beanshell Sampler, this way you will be able to get the output like from any other sampler and extract "interesting" part of the response using Regular Expression Extractor。
- 如果您不能或不想更改当前实现,您可以使用 __FileToString() function 将文件内容读入 JMeter 变量,然后再次使用上述正则表达式提取器从变量中获取所需数据.
当你需要直接输出时使用下面的代码
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("path/to/file/output.txt")), true));
假设我正在调用包含在测试计划中的外部 java 文件(作为一些 jar 文件)
在 Beanshell 采样器中编写此代码
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 中产生的任何输出都将被定向到 ouput.txt
在 Beanshell 中提取所需字符串的代码 Post 从文件处理器 ouput.txt
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();
希望对大家有用