DEV Community

Lovepreet Singh
Lovepreet Singh

Posted on

Make Multiple Requests to your Server | JMeter

📍Client Server Architecture Introduction

In the modern Web, we demand and someone supplies, Right? Exactly, I demand Images, Text, Videos, etc and someone serves me. Here, I am a client and the one who is serving is the server. In plain language, the server is nothing but a computer having OS, Disc Storage, CPU, RAM, etc. And clients request things that are being stored on these servers. These servers can reside anywhere in the world.

🤜 In Simple words, This is what it looks like:-

Follow

Now, We are not the only ones who can request a server. Which means there can be multiple clients requesting a server.

Follow

So, whenever you write your server or backend code (API in general) then you must take this point in mind that multiple requests gonna come. So, After you wrote your API or backend code how can you test if your server can handle "x" no. of requests? This is where Performance testing tools come into play. 🔥 JMeter...

📍Server - Example

Here, Below I have written a simple server code that reads a file "index.html" from the storage and sends it to the client whoever making a request.

**Point to Note:-* Whenever you are writing your server-side code make sure your server is concurrent. Concurrency can be achieved by Async Programming, Multithreading, etc.

import asyncio, socket

async def handle_client(client):
    loop = asyncio.get_event_loop()
    print("Client Accepted ", client)
    myfile = 'index.html'    # Take index.html as filename
    try:
        file = open(myfile,'rb') # Read in byte format
        response = file.read()
        file.close()
        head = 'HTTP/1.1 200 OK\n'
        head += 'Content-Type: '+'text/html'+'\n\n'
    except Exception as e:
        head = 'HTTP/1.1 404 Not Found\n\n'
        response = '<html><body><h1>404 Not Found</h1></body></html>'.encode('utf-8')
    res = head.encode('utf-8')
    res += response
    await loop.sock_sendall(client, res)
    client.close()

async def run_server():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('127.0.0.1', 8080))
    print("Server is listening.....")
    server.listen(4000)
    server.setblocking(False)

    loop = asyncio.get_event_loop() # Event loops runs async tasks, perform network IO

    while True:
        client, _ = await loop.sock_accept(server)
        loop.create_task(handle_client(client)) # Create async tasks and run

asyncio.run(run_server())
Enter fullscreen mode Exit fullscreen mode

🫵 The above code is nothing but just a server, based on the AsyncIO library that makes the server a concurrent one, reading a file and sending it to the client. The point to note here is that we used sockets instead of HTTP requests and responses. It's up to the application whether it is using HTTP or TCP Sockets (Here in this case TCP sockets are used).

😇 **It is just an example code, Don't worry much about the detail**

Follow

📍Performance Testing - JMeter

  • Download and Install Jmeter

  • Run your Server

  • Follow the following steps

  • Add Thread Group

Follow

  • Now, select HTTP Request (Incase your API is HTTP Request Response based). In my case, I have selected TCP Sampler because My Server is based on simple TCP Sockets

Follow

  • Select the users and ramp up time. I have selected 2000 Users and 1 sec ramp-up time which means 2000 client requests will be made in under 1 sec.

Follow

  • Now, enter the IP and Port on which your server is running

Follow

  • Now, you can add different listeners for different kinds of results

Follow

🫡 Now, You can start making Multiple Requests to your server and can see if any requests are getting dropped or the server is getting crashed.


😇 I hope you get to learn something new today. If yes, Drop a like, comment with your views and follow for more Interesting content on Software Engineering.

Follow for More....

Oldest comments (4)

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Drop your views Here

Collapse
 
410nidhi profile image
Nidhi Upasani

Helpful :)

Collapse
 
tr11 profile image
Tiago Rangel

Very nice article, thanks!

Collapse
 
lovepreetsingh profile image
Lovepreet Singh

Thanks 😌