Extending LoadUI - Part 1

In case you haven’t noticed on our websites, we’ve released LoadUI 1.0, a milestone for us as a company and for the load testing community in general. But instead of giving you all its goodies once again (which you can read about here) I’m going to dive in and show you one of the cooler (and as yet undocumented) features; the possibility to dynamically create your own components using the groovy language. I’ll explain more as we go along, so let’s get started!

Short overview

LoadUI provides its load testing functionality through a library of components which can be wired together to generate the desired load. Components interact by sending messages to each other, for example a generator component sends a trigger message that can be used to trigger a runner component to execute a request, which in turn can output the results as a message that can be sent to a log, statistics component, or anything else you might need.

Internally LoadUI provides a groovy-based infrastructure for creating components in the groovy scripting language. All components in LoadUI except those related to soapUI are created as script-components and can be found in your LoadUI installations "script-components" folder. LoadUI monitors this folder for changes to your components and will automatically reload them to reflect any changes made to them. Instantly!

Let’s see this in action, we’ll create a simple component that propagates messages from an input to an output randomly dropping a configurable amount on the way (for example we can put this between two web page runners to simulate random drops of users between them) :

1) Open the scripts folder and add an empty text file named “MyComponent.groovy” to it. LoadUI will detect this file and load it as an (empty) component and add it to the Misc category in the component toolbar:

Already now you can “use” the component by dragging it onto the project canvas (although it can’t do much):

createdmycomponentinstance

2) The first we have to do is add an input and an output to the component, add the following to the MyComponent.groovy file:

createInput( "input", "Incoming Messages" )

createOutput( "output", "Outgoing Messages" )

Now save this and switch back to LoadUI; if you have created a MyComponent component in your project you will see it update to reflect the additions above:

(This happens without us having to restart anything! We could even have distributed the component to a LoadUI Agent on another machine; LoadUI would have updated the distributed version as well!)

You can now connect components to both the input and output; for example a generator to the input and a web page runner to the output:

connectedtocomponents

But running this won’t propagate any messages from the input to the output since that is still missing. Let’s get right to it!

3) Add a message handler as follows to the script file and save it:

onMessage = { incoming, outgoing, message ->

send( output, message);

}

This just propagates incoming message to the created output terminal; as soon as you save this LoadUI will detect the change and update the component; you can even do this while the test is running and you will see that messages coming from the generator will now be propagated to the runner:

addedeventhandler

Obviously the event handler needs to be extended to not propagate all messages as it does now, but for this we first need to add a property controlling the drop-ratio.

4) Add a dropRate property at the start of the script file as follows:

createProperty('dropRate', Integer, 0)

and since we need to be able to change its value add a layout to the end of the script:

layout {

property( property:dropRate, label:"Drop Rate (%)", min:0, max:100 )

}

Now when you save you will suddenly a knob on your component, allowing you to change the dropRate value as desired:

addedpropertyknob

All that is left now is to actually use the value of the property, do this by modifying the handler as follows:

onMessage = { incoming, outgoing, message ->

if( Math.random()*100 > dropRate.value )

send( output, message);

}

Awesome! Save the file and you now have a “DropRate” component that allows you to drop messages however required! Try turning the knob while your test is running and you will see that only a limited number of messages get propaged (depending on the setting of course).

Recap

Here comes the complete script component:

createProperty('dropRate', Integer, 0)

createInput( "input", "Incoming Messages" )

createOutput( "output", "Outgoing Messages" )

onMessage = { incoming, outgoing, message ->

if( Math.random()*100 > dropRate.value )

send( output, message);

}

layout {

property( property:dropRate, label:"Drop Rate (%)", min:0, max:100 )

}

Wow! Just 14 lines of code gave us a new load testing component that can be used together with all other LoadUI components, can be distributed to LoadUI agents, etc. And as you can see this is all quite straight-forward:

  • Define the properties required to configure your component
  • Define the input(s) and output(s) used to interact with other components
  • Add event-handler logic to actually do something
  • Define a layout that exposes the desired properties

Of course there is a bunch of stuff we can do but didn’t since this was the first part in a series of several. More namely we didn’t:

  • Respond to any control-level events triggered by the framework
  • Create any custom settings tabs
  • Add any display values
  • Define a compact-mode layout
  • Control the LED lamp in the top-left corner of the component
  • Modify the message going through the component
  • Add any custom content to the generated summary report
  • Put the component in one of the existing categories
  • Add a custom icon for the component

All this lies ahead of us, but if you can’t wait just have a look at all the other existing script components to see how they are coded.

Some suggestions for improvements to the above component:

  • Add another output and use the knob to control the ratio for dividing incoming requests between them
  • Add displays showing how many messages that have been dropped (or propagated)
  • etc

And please don't hesitate to comment, either here or in our forum at http://www.eviware.com/forum

As always, thank you for your time!

/Ole


Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox