What Is UDP? The Ultimate Testing Guide
September 10, 2021

What Is UDP? The Ultimate Testing Guide

Performance Testing

UDP can be implemented in systems that communicate with multiple users, or in systems packet loss is prioritized over packet waiting. This blog post will explain how to load test UDP with open-source Apache JMeter™.

Back to top

What is UDP?

UDP (User Datagram Protocol) is a message-oriented transport level protocol. Unlike TCP, it does not require a prior connection and doesn't use handshaking. This makes it faster and lighter than TCP and other protocols.

On the other hand, it does not guarantee delivery, maintaining order, or duplicate protection of the transmitted messages.

UDP is widely used in systems that need to communicate with a number of users, or systems in which packet loss is preferred to packet waiting. For example, UDP is used in media streaming applications, the DNS protocol and VPNs.

But despite the fact that the implementation of the protocol implies some loss of data, ideally users should not experience many losses. No one wants to watch a TV stream that looks like a slideshow since the server can’t receive a large number of clients׳ requests.

Back to top

Configuring the UDP Request Sampler

To detect UDP performance issues, it is necessary to test these types of services before going to production. JMeter can be used to generate load over the UDP protocol, with the UDP Protocol Support plugin, which provides the UDP Request Sampler. The plugin can be installed via the Plugin Manager.

Step 1 - Add a thread group to a new JMeter test plan

Right click -> Add -> Threads -> Thread Group

Step 2 - Add the UDP Request Sampler as a child element to the thread group

Right click -> Sampler -> jp@gc - UDP Request

UDP request

The default sampler settings are shown in the screenshot above. We will show specific configurations later on.

UDP Request Configurations:

  • Hostname/IP and UDP Port  - The address and port of the UDP server. The address field can contain a URL.
  • Wait for Response and Response Timeout - These fields should be used  for cases when the server sends the response to a UDP request. If the checkbox is set, the UDP Sampler will be waiting for a response from the recipient. The waiting time (milliseconds) is taken from the Response Timeout field.
  • Close UDP Socket - If set, the UDP Socket will be closed at the end of the sampler work. Otherwise, the socket will be closed at the end of the thread iteration.
  • Request Data - the field for the information to be sent.
  • Bind Local Address and Bind Local Port - The local address and port that should be used to send the UDP request.
  • Data Encode/Decode class - Used for specifying classes for processing the output and input information. The UDP Sampler contains three class implementations:
    • kg.apc.jmeter.samplers.UDPSampler - This implementation does not convert data and is used as default. It is used when the field is empty or does not contain valid class name.
    • kg.apc.jmeter.samplers.HexStringUDPDecoder - This class converts the UDP Request data from Hex String to simple text.  The answer of the server converts from simple text to hex-string too. This implementation is the most useful, because a number of clients work with binary data. For example, if you write the string ‘48656c6c6f’ in Request Data, it will be converted to ‘Hello’.
    • kg.apc.jmeter.samplers.DNSJavaDecoder - This implementation is used for load testing DNS Servers that work via UDP. You need to install additional plugins to work with this implementation/class or just download jar file and add it to the /lib/ext folder of JMeter, because the UDP Request does not contain the class to work with DNS records. Request data must consist of 3 parts: name, type and class, for example “facebook.com A IN”.

You can also implement the kg.apc.jmeter.samplers.UDPTrafficDecoder interface and write your own data decoder/encoder.

Back to top

UDP Request Implementations

Now, let’s consider the embedded implementations. In order to explain how the UDP Request sampler works, it’s necessary to run a simple UDP Server

The Server works in a two-way communication mode. It involves feedback from the receiver to the sender. In our examples the script sends the data to the server via the UDP Request sampler and gets responses from the server.

The Default Class Implementation (UDPSampler)

The UDP sampler has already been added to the demo script.  Now, you need to fill in Hostname (localhost), Port (9876) and Request Data (‘What is love?’ in our example) fields and start the UDP Server. You also need to add Listener to the Thread for the results monitoring, for example View Results Tree.

Right click -> Add -> Listeners-> View Results Tree

view results tree

view results tree #2

view results tree #3

After that, you can run the JMeter script.

As you can see, the UDP Sampler finished successfully. In the Response data tab you can see the Server response.

HexStringUDPDecoder

Now, let’s add kg.apc.jmeter.samplers.HexStringUDPDecoder class to the Data Encode/Decode field and change Request Data to the Hex-string ‘’536f20776861742069732072696768743f20416e6420776861742069732077726f6e673f” (“So what is right? And what is wrong?”).The Configuration of The UDP Request Sampler for this case you can see in the screenshot below.

HexStringUDPDecoder

After the script execution you can see the server response that was converted to the Hex-string “5768617420656c73652063616e2049207361793f204974277320757020746f20796f752e” (“What else can I say? It's up to you.”).

server response with relevant hex string

In addition, all messages are displayed in the console, in which our UDP server was launched.

console with messages displayed

DNSJavaDecoder

The last embedded implementation is the DNSJava Decoder. To demonstrate the work of this class we will send a DNS request to the Google Public DNS for IP searching. Let’s test it.

First change the address and port to 8.8.8.8 and 53 correspondingly. Then, change the class in the Data Encode/Decode field to the  kg.apc.jmeter.samplers.DNSJavaDecoder and add DNS Request (“facebook.com. A IN” in our example) to Data Request field.

DNSJava Decoder

After execution you can see DNS Server’s response. If you put the IP address from the  Response in your browser’s address bar of your browser, you will see the Facebook page.

Back to top

Creating a UDP Testing Scenario

After the UDP Sampler review, let’s try to load test our server. In this scenario, we will use The Default Class Implementation. The settings for the sampler correspond to settings for this implementation, used above. Now, you need to add some new elements to the JMeter script.

Add the Thread Group and UDP Sampler as shown above, with the Default Class Implementation.

Now you’re ready to move on to step 3 - Add the CSV Data Config as a child element to the Test plan to load messages from it to UDP requests.

Right click -> Config elements->  CSV Data Config

CSV data config

The sharing mode of CSV Data config is set to the current thread group. Also, do not forget to enter the variable of data config in the Request Data field of UDP Request Sampler. The CSV file for our server looks like this:

csv file

4. Add a Constant Timer as a child element to the Thread Group to regulate request rates.

Right click -> Timer->  Constant Timer

5. Add a Response Assertion as a child element to the UDP Request to control the server responses

Right click -> Assertions ->  Response Assertion

response assertion

With the help of the Response Assertion we can check the UDP server responses. This assertion validates each response. If a response contains a specific string, the assertion executes successfully. Our server will return an error message when it cannot recognize the input message. In our example the CSV file contains several unrecognizable messages for demonstration.

The final structure and the result of the script execution are shown in the screenshot below.

UDP testing results

As you remember, the Response Assertion was configured to pass all responses of the server that did not contain the error text. The CSV file contains two incorrect request messages and, as you can see on the screenshot above, the server did not recognize the two messages and two Response Assertions finished unsuccessfully.

As you can see, the script works correctly and if you want to scale up the script, you can change the number of threads and ramp-up period in the Thread Group element. To confirm the  Response Assertion works, you can see the server log in console which shows the responses and the requests.

udp load testing responses and requests

Back to top

Bottom Line

That’s it! You now know how to load test a UDP server. As you can see, the UDP Sampler provides an interface for working with different UDP servers. You can also manipulate data conversion with the help of embedded classes, or you can just implement the basic interface and write your own converter. 

START TESTING NOW

Back to top