How to use java program in bean pre-processor

前端 未结 2 1427
灰色年华
灰色年华 2021-01-24 09:51

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

I have a couple of questions here.

  1. Can I use the following program directly in JMeter BeanS

相关标签:
2条回答
  • 2021-01-24 10:04

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

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

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

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

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

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

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

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

    Unzip attachments http://uplea.com/dl/9F734367B43FB93 :

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

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

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

    Hope to help you...

    0 讨论(0)
  • 2021-01-24 10:13

    GZIP compression is rather "heavy" operation, if you will have lots of virtual users - it may become a bottleneck so consider using other approaches:

    1. There is __base64Encode() function available via JMeter plugins
    2. If for any reason it is not enough - go for JSR223 Sampler and Groovy language, Groovy is mostly compatible with Java so your code should work fine (just remove main method)
    0 讨论(0)
提交回复
热议问题