如何在Bean预处理器中使用Java程序

编程入门 行业动态 更新时间:2024-10-22 23:33:17
本文介绍了如何在Bean预处理器中使用Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我在这里有几个问题.

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

  • 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 :

    解压缩附件 uplea/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程序

    本文发布于:2023-11-14 00:42:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1585753.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:器中   程序   如何在   Bean   Java

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!