azurejmeterazureservicebusjmeter-pluginsservicebus

How to reuse the same connection for azure service bus sampler in jmeter


I am using an Azure Service Bus Sampler in jmeter and using plugin jmeter-plugins-azure-servicebus-0.3.4.jar. I am using jmeter version 5.6.3. Below is the snapshot for the jmeter sampler I am using, Azure service bus sampler

I am running with a load test and this sampler creates a new connection every time to send the messages. This is causing issues due to a lot of connections being created. is there a way we can use the same connection for further requests ?

There is an option in the sampler itself i.e 'Use defined connection', but how to use this ? How to save a connection to use it again and again ? Can someone please help on this ! Thanks in advance !


Solution

  • is there a way we can use the same connection for further requests ?

    You can reuse the same connection for multiple requests in JMeter by storing the connection object in a JMeter variable using vars.putObject and retrieving it with vars.getObject in a JSR223 Sampler to send messages to the Azure Service Bus Topic.

    Jmeter :

    enter image description here

    I have successfully sent messages to the Azure Service Bus Topic using the JSR223 sampler with the below test codes.

    sampler-1 :

    I have saved the connection using vars.putObject and retrieved it using vars.getObject in the code below, reusing it in other scripts.

    import com.azure.messaging.servicebus.*;
    
    if (vars.getObject("azureSenderClient") == null) {
        String connectionString = "<ServiceBusConneString>";
        String topicName = "<TopicName>";  
    
        ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
            .connectionString(connectionString)
            .sender()
            .topicName(topicName)
            .buildClient();
        
        vars.putObject("azureSenderClient", senderClient);  
        log.info("Azure Service Bus Connection Created and Stored");
    } else {
        log.info("Azure Service Bus Connection Already Exists");
    }
    

    sampler-2 :

    import com.azure.messaging.servicebus.*;
    
    ServiceBusSenderClient senderClient = vars.getObject("azureSenderClient");
    if (senderClient == null) {
        log.error("Service Bus connection not found! Make sure the Setup Thread Group ran first.");
    } else {
        ServiceBusMessage message = new ServiceBusMessage("Hello Kamali from JMeter!");
        senderClient.sendMessage(message);
        log.info("Message Sent Successfully to Topic: kamtopic");
    }
    

    sampler-3 :

    import  com.azure.messaging.servicebus.*;  
      
    ServiceBusSenderClient  senderClient  =  vars.getObject("azureSenderClient");  
    if  (senderClient  !=  null)  {  
      senderClient.close();  
      vars.remove("azureSenderClient");  
      log.info("Azure Service Bus Connection Closed Successfully");  
    }  else  {  
      log.warn("No Service Bus connection found to close.");  
    }
    

    enter image description here

    Azure Service Bus :

    The messages sent successfully to the Azure Service Bus Topic as shown below.

    enter image description here