0

Trying to read the Binary data response from JMeter sampler and deserialize it in the BeanShlll Post Processor.

The flow is, controller first sends a request to a webservice, which returns binary data. When I view that in the JMeter, I see some junk chars.

I need to convert this to the actual object (my custom object) in the BeanShell script.

This is what I tried. But no luck. Or may be I am trying something dumb. PS: Newbie with IO stuff.

 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?

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173

1 Answers1

1

Are you sure that readObject method works on that binary data? For example don't i.e. StreamCorruptedException occur and your post-processor stops working.

As per How to use BeanShell guide

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.

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

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. In opposite case you'll see a relevant error stacktrace there.

    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.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • I used readObjext for similar thing and it works. Just not in this case. Thanks – Kevin Rave Feb 02 '14 at 13:12
  • What does jmeter.log say? Any beanshell-related errors? Any exceptions in stdout when your code wrapped into try/catch block? Is `MyClass` jar in JMeter classpath? – Dmitri T Feb 02 '14 at 14:24
  • It just does not show anything. Even with try/catch. For now, I just save the response to a file and read that file with InputStream. It works. – Kevin Rave Feb 03 '14 at 16:27