Make Multiple Requests to your Server | JMeter
Tags: webdev, beginners, java, python
Published: 2022-12-02 10:51:28

### 📍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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyad9v4qrbp7j057n7qr.png)

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


![Follow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3svev9rqv2wz64apqm8l.png)



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**](https://www.geeksforgeeks.org/concurrency-in-operating-system/#:~:text=Concurrency%20is%20the%20execution%20of,shared%20memory%20or%20message%20passing.) can be achieved by [Async Programming](https://www.bmc.com/blogs/asynchronous-programming/), [Multithreading](https://totalview.io/blog/multithreading-multithreaded-applications), etc.
    

```python
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())
```

🫵 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jidm3zx1mh3hjf0lgp2.png)


### 📍Performance Testing - JMeter

*   Download and Install [Jmeter](https://www.guru99.com/guide-to-install-jmeter.html)
    
*   Run your Server
    
*   Follow the following steps

- **Add Thread Group**

![Follow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2hdnozg2qbr21m9z4co.png)

- **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93vh3e8rwou3zvfk4lvq.png)

- **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyqp1v371vhgzndjd0ca.png)

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

![Follow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acmxgkrtd6eswuu0x1le.png)

- **Now, you can add different listeners for different kinds of results**  

![Follow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kdblvk7e82q9bfxrf9y.png)

🫡 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....](https://lovepreet.hashnode.dev/)**