如何在Bean预处理器中使用Java程序 [英] How to use java program in bean pre-processor

查看:143
本文介绍了如何在Bean预处理器中使用Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的程序读取XML文件并压缩为gzip.

The below program reads the XML file and compress into gzip.

我在这里有几个问题.

  1. 我可以在JMeter BeanShell预处理器中直接使用以下程序吗?

  1. Can I use the following program directly in JMeter BeanShell pre-processor?

我想使用输出变量作为JSON请求的输入.在Jmeter中可以吗?

I want to use the output variable as input to JSON request. Is it possible in Jmeter?

屏幕截图和详细信息将不胜感激.

Screen shot and details will be appreciated.

公共静态void main(String [] args)引发异常{

public static void main(String[] args) throws Exception {

String line = null; 
String sb = "";
File f=new File("D:\\RetailTransactionLog_9419_001_590.xml");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);   

while((line=br.readLine())!=null)
{
     sb= sb + line;
}   
br.close();


ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = new GZIPOutputStream(baos);
gzos.write(sb.getBytes("UTF-8"));
gzos.close();

String base64CompressedString = Base64.getEncoder().encodeToString(baos.toByteArray());
System.out.println(base64CompressedString);

推荐答案

当然,您可以将Java代码直接放入JMeter BeanShell预处理器等等!

Of course, you can put your piece of java code directly in a JMeter BeanShell pre-processor and much more !

将预处理器组件作为JSON请求的子项插入(如附件中的脚本示例所示).

Insert you pre-processor component as child of your JSON request (as in my script example in attachment).

您无需导入BufferedReader,ByteArrayOutputStream,File,FileReader,IOException,Base64等java.io包……

You don’t need to import java.io package like BufferedReader, ByteArrayOutputStream, File, FileReader, IOException, Base64…

还删除主要签名public static void main(String [] args)…

Remove also the main signature public static void main(String[] args) …

您只需导入"java.util.zip.GZIPOutputStream"(A)

You only have to import "java.util.zip.GZIPOutputStream" (A)

我还用log.info(base64CompressedString)替换了您的System.out.println(base64CompressedString),只是为了在jmeter控制台中可视化您的输出(B)…

I’ve also replace your System.out.println(base64CompressedString) by log.info(base64CompressedString) just to visualize in the jmeter console your output (B)…

最后在代码末尾添加代码(C),以将您的结果引用到您选择的变量中(在我的示例中为"a").

And finally add at the end, the code (C) to reference your result in the variable of your choice ("a" in my example).

您只需在json请求中使用$ {a}之后调用变量,就像在我的JMX脚本中一样:

You just have to call your variable after with ${a} in your json request like in my JMX script :

解压缩附件 http://uplea.com/dl/9F734367B43FB93 :

在/bin下的"ReadAndCompressMyFile.jmx",然后在C:下放置"test.xml",或更改代码中的路径.

"ReadAndCompressMyFile.jmx" under /bin and put "test.xml" under C: or change the path in your code.

我使用了虚拟采样器代替了您的json请求.

I’ve used a dummy sampler instead of your json request.

运行脚本后,您可以在视图结果树"(请求"选项卡)和控制台中看到"a"的值(对应于base64CompressedString).

After running my script, you can see in View Result Tree (Request tab) and in the console, the value of "a" (corresponding to base64CompressedString).

PS:要使用虚拟采样器"运行我的脚本,您需要在jmeter目录的/lib/ext下添加jmeter-plugins-dummy-0.1.jar.

PS : To run my script with the "dummy sampler", you need to add jmeter-plugins-dummy-0.1.jar under /lib/ext of your jmeter directory.

希望能帮助您...

这篇关于如何在Bean预处理器中使用Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆