Queues and JMeter: Exchange with Publisher and Subscriber

    Hello, Habr!

    This is a sequel to my previous publication , in which I will talk about the options for placing messages in queues using JMeter.

    We make a data bus for a large federal company. Various query formats, conversions, intricate routing. For testing, you need to send a lot of messages in the queue. Manually - a pain that not every manual engineer will cope with.



    Introduction


    Although I had to put up with this pain at first. It all started with RFHUtil. Powerful, but uncomfortable and scary: Well, you know Rus.



    Indispensable in some cases, but stably falling in case of active use.
    Convenient testing with him is impossible.

    With JMeter, things got easier. After the first stage with mastering and getting used to it, the hope of a happy testing came.

    I actively use the JMS Publisher and JMS Subscriber samplers. Unlike JMS Point-to-Point, this couple seemed more convenient to work with. For example, in Subscriber in JMS Selector you can specify a variable, in Point-to-Point - no (or this method is not too obvious).

    Sampler Preparation


    JMS Publisher


    • Setup - Each Sample. Apache recommends using this option if queues / topics are specified through variables.
    • Expiration (ms) = 120000. In case of failure, test requests will disappear from the queue after 2 minutes.
    • Use non-persistent delivery mode? - true. IBM claims that persistent mode provides reliable persistence of messages in the event of a sudden failure. And faster sharing in non-persistent mode. For test purposes, speed is more important.

    In each Publisher, I set the jms property that Subscriber will use in the JMS Selector. For each dispatch, a random value is generated in the User Parameters test plan element:



    This way you can be sure that the correct message has been read.

    The final "blank" of the pre-configured JMS Publisher:



    Jms subscriber


    • Setup - Each Sample. Well, you get the point.
    • Timeout (ms) = 100000. If the request does not come into the queue after 100 seconds of waiting, then something went wrong.
    • Stop between sample? - true.


    JMS Selector is a pretty handy thing . Final JMS Subscriber:



    How to deal with Cyrillic in transmitted messages. In JMeter, by default, after subtraction, it is displayed crookedly. To avoid this and enjoy the great and the powerful always and everywhere, you need:

    1. Add the JVM argument to the JMeter “launcher”:
      -Dfile.encoding=UTF-8
    2. Add JSR223 PostProcessor to Subscriber with a line on groovy:
      prev.setDataEncoding("UTF-8")

    Text transfer


    The laziest option. Suitable for debugging freshly written tests. Or for cases when you need to send at least something small. Select the option Message source - Textarea and place the message body in a text block:



    File transfer


    The most common option. Suitable for most scenarios. Select the option Message source - From file and specify the path to the message in the File - Filename field :



    Transfer file to text field


    The most versatile option. Suitable for most scenarios + can be used in JMS Point-to-Point, in which there is no second sending option:



    Byte array transfer


    The most difficult option. Suitable for checking the infallibly accurate transfer of requests to the byte, without distortion, SMS and perturbation. I ca n’t do this in the default JMeter, here they definitely told me about it.

    Therefore, I had to download the sources and modify the JMS Subscriber code .

    Replaced the extractContent(..)line in the method :

    buffer.append(bytesMessage.getBodyLength() + " bytes received in BytesMessage");

    on the:

    byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
    bytesMessage.readBytes(bytes);
    try {
    	buffer.append(new String(bytes, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
    	throw new RuntimeException(e);
    }

    and rebuilt JMeter.

    It remains to add a couple of JSR223 Sampler. The first is before the Publisher / Subscriber pair to create a .dat file containing random bytes:

    import org.apache.commons.lang3.RandomUtils;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    vars.put("PATH_TO_BYTES", "C:\\temp\\randomBytes.dat");
    File RESULT_FILE = new File(vars.get("PATH_TO_BYTES"));
    byte[] arr = RandomUtils.nextBytes((int)(Math.random()*10000));
            try {
                FileOutputStream fos = new FileOutputStream(RESULT_FILE);
                fos.write(arr);
                fos.close();
            } catch (IOException e) {
                System.out.println("file not found");
            }

    The second one, at the end of the script, deletes the file:

    import java.io.File;
    File RESULT_FILE = new File(vars.get("PATH_TO_BYTES"));
    RESULT_FILE.delete();

    And do not forget to add the path to the file from Publisher:



    As well as checking in JSR223 Assertion for Subscriber - compare the source bytes with those that come in the recipient's queue:

    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Arrays;
    Path path = Paths.get(vars.get("PATH_TO_BYTES"), new String[0]);
    byte[] originalArray = Files.readAllBytes(path);
    byte[] changedArray = ctx.getPreviousResult().getResponseData();
    System.out.println(changedArray.length);
    if (Arrays.equals(originalArray, changedArray))
    	{
         	SampleResult.setResponseMessage("OK");
    	} else {
    	   SampleResult.setSuccessful(false);
         	   SampleResult.setResponseMessage("Comparison failed");
    	   SampleResult.setResponseData("Bytes have changed","UTF-8");
         	   IsSuccess=false;
    	}

    Conclusion


    He described four ways of sending messages in a queue, which I use in practice every day. I hope this information makes your life easier. In the sequel, I plan to talk about my experience in testing exchanges, where on one end there is a queue, and on the other there is a database or file system.

    Take care of your time. And thanks for watching.

    image

    Also popular now: