简体   繁体   English

从JMeter采样器读取二进制数据

[英]Reading the Binary Data from JMeter sampler

Trying to read the Binary data response from JMeter sampler and deserialize it in the BeanShlll Post Processor. 尝试从JMeter采样器读取Binary数据响应,然后在BeanShlll Post Processor中反序列化它。

The flow is, controller first sends a request to a webservice, which returns binary data. 流程是,控制器首先将请求发送到Web服务,该服务返回二进制数据。 When I view that in the JMeter, I see some junk chars. 当我在JMeter中查看时,我看到了一些垃圾字符。

I need to convert this to the actual object (my custom object) in the BeanShell script. 我需要将其转换为BeanShell脚本中的实际对象(我的自定义对象)。

This is what I tried. 这就是我尝试过的。 But no luck. 但是没有运气。 Or may be I am trying something dumb. 还是我正在尝试一些愚蠢的事情。 PS: Newbie with IO stuff. PS:刚接触IO的新手。

 import java.io.ObjectOutputStream;
 import com.package.MyClass;

 MyClass myObj = new MyClass();
 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));

 myObj = ois.readObject();

It does not work. 这是行不通的。 It does not show anything. 它什么也没显示。 Any help? 有什么帮助吗?

Are you sure that readObject method works on that binary data? 您确定readObject方法可用于该二进制数据吗? For example don't ie StreamCorruptedException occur and your post-processor stops working. 例如,不要发生StreamCorruptedException并且您的后处理器停止工作。

As per How to use BeanShell guide 按照如何使用BeanShell指南

As Beanshell is executed within Rhino engine there is no other option to see what's wrong apart from inspecting jmeter.log file for something like: Error invoking bsh method and using System.out.println(“something”) or log.info(“something”) to get to the bottom of where the script fails. 由于Beanshell是在Rhino引擎中执行的,因此除了检查jmeter.log文件是否有类似问题之外,没有其他选择可以看到问题所在:调用bsh方法并使用System.out.println(“ something”)或log.info(“ something ”)进入脚本失败的底部。

you don't have a way to verify whether your post-processor is successful, you'll have to look into the logs and/or add some debug output statement to the end. 您没有办法验证后处理器是否成功,则必须查看日志和/或在末尾添加一些调试输出语句。

Another technique which can be useful is surrounding your code with try/catch 另一种有用的技术是用try/catch包围代码

See example below on how to use. 有关使用方法,请参见下面的示例。 In case of "everything is fine" the post processor will print Successfully read data into myObj into STDOUT. 如果“一切都很好”,则后处理器将把Successfully read data into myObj打印Successfully read data into myObj并输出到STDOUT中。 In opposite case you'll see a relevant error stacktrace there. 在相反的情况下,您会在此处看到相关的错误stacktrace。

    import java.io.ByteArrayInputStream;
    import java.io.ObjectInputStream;
    try {
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
        Object myObj = ois.readObject();
        ois.close();
        System.out.println("Successfully read data into myObj"); 
    } catch (Exception ex) {
        ex.printStackTrace();
    }

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM