ai-test, ai-agent, llm, mcp-ai,

AI write script - Performance Test with Locust

Donald Donald Follow May 30, 2024 · 7 mins read
AI write script - Performance Test with Locust
Share this

Use AI (Chatgpt) to write test script in Data Driven in Performance test

Overview

In order to help us understanding the ways that I am taking advantages of powerful capabilities of AI (ChatGPT) to boost my productivities. Although, I have to validate all suggestions from ChatGPT, how to make sure whether it is useful or not (Accuracy and Testing Mindset).

Scenarios in this post: I have a simple service (Allows End-User login from Username and Password, then they can get the list of new feeds from Backend).

I expect that I can write quickly the performance test script with the list of users saved in a JSON file

Here is the full of pictures I already worked with ChatGPT.

I requested ChatGPT help me write a test script for performance test in Locust from a CURL ass below:

curl --location 'http://127.0.0.1:5000/login' \
--header 'Content-Type: application/json' \
--data '{
  "username": "admin",
  "password": "admin123"
}'

And here is the list of users, I have in my example system

[
    {
        "username": "admin",
        "password": "admin123"
    },
    {
        "username": "john.doe",
        "password": "JohnsPassword1"
    },
    {
        "username": "jane.smith",
        "password": "Jane1234!"
    },
    {
        "username": "alice.wonder",
        "password": "Wonderland@5"
    },
    {
        "username": "bob.builder",
        "password": "CanWeFixIt$2"
    },
    {
        "username": "charlie.brown",
        "password": "Peanuts123"
    }   
]

After I login with the valid credentials, I will received the token, and use it to get the list of feeds in the API request as CURL information as below:

curl --location 'http://127.0.0.1:5000/feed' \
--header 'token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIn0.vb1PIGc2ZoxpcJ0sPAYCjc4-D1o3nCzCyNZiWWYZy3E'

1 - Write a Locust Python script with Chatgpt

Firstly, I asked Chatgpt AI to help me write a Locust python script to test Login

Hey, Could you help me write a Locust python script to test login api as curl information ?
curl --location 'http://127.0.0.1:5000/login' \
--header 'Content-Type: application/json' \
--data '{
  "username": "admin",
  "password": "admin123"
}'

Here is the response from Chatgpt

walking

Here is the locust script content that Chatgpt returned:

from locust import HttpUser, TaskSet, task, between

class LoginTaskSet(TaskSet):
    @task
    def login(self):
        self.client.post("/login", json={"username": "admin", "password": "admin123"})

class WebsiteUser(HttpUser):
    tasks = [LoginTaskSet]
    wait_time = between(1, 5)  # Wait time between tasks

if __name__ == "__main__":
    import os
    os.system("locust")

We also follow the steps to save the script as file: locustfile.py

walking

I already validate again, to make sure the test script work correctly 🙂 (Test the test script ⇒ validation and Accuracy mindset)

walking

And here is the result on Locust report:

walking

And log of server (System-under-Test):

walking

It is proved that, the example script from Chatgpt is working well.

2 - Request Chatgpt a test script reading data from JSON file

However, it still need to be updated more, how it allows us generate requests with dynamic data of users login. Therefore, I requested Chatgpt update the test script, how we can read data from a JSON file, where we saved all users data for performance testing as bellow

[
    {
        "username": "admin",
        "password": "admin123"
    },
    {
        "username": "john.doe",
        "password": "JohnsPassword1"
    },
    {
        "username": "jane.smith",
        "password": "Jane1234!"
    },
    {
        "username": "alice.wonder",
        "password": "Wonderland@5"
    },
    {
        "username": "bob.builder",
        "password": "CanWeFixIt$2"
    },
    {
        "username": "charlie.brown",
        "password": "Peanuts123"
    }   
]

I raised the request as below:

walking

And Chatgpt responded to me as below:

  • it suggested me to save all data as a JSON file

walking

Modify the test script as below:

walking

Here is the content of modified script:

import json
import random
from locust import HttpUser, TaskSet, task, between

# Load user data from JSON file
with open('users.json', 'r') as file:
    users = json.load(file)

class LoginTaskSet(TaskSet):
    @task
    def login(self):
        # Select a random user from the list
        user = random.choice(users)
        username = user['username']
        password = user['password']
        
        # Perform login request
        self.client.post("/login", json={"username": username, "password": password})

class WebsiteUser(HttpUser):
    tasks = [LoginTaskSet]
    wait_time = between(1, 5)  # Wait time between tasks

if __name__ == "__main__":
    import os
    os.system("locust")

We run again with this configuration:

walking

Here is the result on Locust report

walking

and log response on service

walking

In conclusion, we refer to supports from ChatGPT only, we should NOT depend on and NOT trust in all information completely. As above information and steps I did, we have to modify and test all information that Chatgpt suggested us.

Join Newsletter
Get the latest news right in your inbox. We never spam!
Donald
Written by Donald
Hi, I am Donald (Doai Tran), the author of Testing4Everyone. I hope you like it!