ChatGPT Use cases for Performance Engineers
Tags: ai, testing, performance, machinelearning
Published: 2023-01-23 16:00:00


<p>ChatGPT is storming the internet. I cannot pass my feed without a single mention of ChatGPT in my socials. So, it is a sin if I am not writing about ChatGPT. I have been using ChatGPT since the beta version was announced. I have been playing around and sharing feedback from the console about its responses. It is fun and helps you to learn at the same time. In this blog article, you will learn about ChatGPT and how to use ChatGPT to solve performance engineers' daily questions. </p>



<h2>What is ChatGPT?</h2>



<p>First let us understand the basics of GPT. The acronym of GPT is Generative Pre-trained Transformer. That is a lot to digest. Let us simply put it. </p>



<p><em>It is a type of <strong>artificial intelligence model</strong> that is <strong>trained </strong>to generate <strong>human-like text</strong>. The model is trained on a large dataset of text, such as books, articles, and websites, and learns the patterns and structure of language. Once it is trained, it can be used to generate new text that is similar to the text it was trained on. It can be used for a variety of tasks such as language translation, text summarization, question answering, and text completion.</em></p>



<p>I have highlighted the critical terms in <strong>bold</strong>. Basically, you feed a humongous amount of information into an AI model to train. So that, it can generate new info based on what it is trained for. </p>



<p>You may ask, is it like a search engine? The answer is NO. A search engine browses the internet and lists the results based on a query you enter. But GPT generates new text based on the training it had from the dataset. <strong>It cannot browse the internet (at this moment). </strong></p>



<h2>ChatGPT Signup</h2>



<p>Head to https://chat.openai.com/auth/login to sign up for a free account. It is a simple and straightforward process.</p>



<h2>Use Cases for Performance Engineers</h2>



<p>There are umpteen use cases floating around for ChatGPT e.g., advertising, writing, screenplay, poems, website status update, development, testing, and more. </p>



<figure class="wp-block-image aligncenter size-full"><a href="https://qainsights.com/wp-content/uploads/2023/01/image-2.png"><img src="https://qainsights.com/wp-content/uploads/2023/01/image-2.png" alt="ChatGPT Status Screenplay - ChatGPT Use Cases for Performance Engineers" class="wp-image-9860"/></a><figcaption class="wp-element-caption">ChatGPT Status Screenplay</figcaption></figure>



<p>Let us see the critical ChatGPT use cases for performance engineers.</p>



<ol>
<li>Ask ChatGPT to explain performance testing to your child, parents, manager, CTO, CEO, Prime Minister and more.</li>



<li>Ask ChatGPT to write a performance test plan for your application.</li>



<li>Ask ChatGPT to compare tools' features.</li>



<li>Ask ChatGPT to analyze the performance testing results.</li>



<li>Ask ChatGPT to write code for performance testing</li>



<li>Ask ChatGPT to list the risks involved in performance testing.</li>



<li>Ask ChatGPT to build a performance testing tool (insane, right?) </li>
</ol>



<p>Even Einstein asked questions. Let us ask ChatGPT now.</p>



<h2>Explain performance testing to a 6-year-old kid</h2>



<p>I asked "explain performance testing to a 6-year-old kid". Below is the ChatGPT's response. </p>



<p>It took a toy as an example and connected it with the performance testing. Not bad, eh?</p>



<p><em>Performance testing is like testing a toy before you play with it. It helps make sure the toy works well and doesn't break easily. For example, if you were testing a toy car, you would want to see how fast it can go, how well it turns, and how long the battery lasts. Similarly, performance testing is done on computer programs and websites to make sure they work well and don't crash or slow down. It's like a check-up for a computer program to make sure it's healthy and runs smoothly.</em></p>



<p>Let us shift gears to ask a complex request. </p>



<h2>Write a performance test plan for your application</h2>



<p>I asked ChatGPT to write a performance test plan for your application. We should not ask it literally as-is. We need to feed information as below.</p>



<pre class="wp-block-code"><code>Write a performance test plan for my application for the below specifications:

- Start with an objectives of performance testing
- Banking web application 
- Expected number of peak users is 5000 during normal business hours
- Expected response time is between 1-3 seconds, CPU and Memory - up to 60%
- Scenarios to be tested: Creating an account, login, add payee, transfer funds, check balance, applying for a loan, modifying profile, and logout
- Use any Java, Python or Go based open-source or commercial performance testing tool
- Timeline: Requirements gathering and test environment validation - 1 week, Test scripting - 2 weeks, Test execution - 3 weeks, Results analysis and signoff - 1 week
- Types of testing - load, stress, and endurance
- Add a section for defects lifecycle 
- Add a section for known issues
- Add a section for test data management
- Add a section for references</code></pre>



<p>Below is the ChatGPT's response. As you can see, ChatGPT took the information from the above question and added the appropriate context to it e.g. it added test data management, defects lifecycle, and more. I agree it is a high level. You cannot use this as-is in your project. I suggest you build this up based on your requirements. </p>



<p><strong>Performance Test Plan:</strong></p>



<p><em><strong>Objectives:<br></strong>To determine the performance and scalability of the banking web application under different load and stress conditions.<br>To ensure that the application can handle the expected number of peak users of 5000 during normal business hours<br>To verify that the application meets the expected response time of between 1–3 seconds and CPU and Memory usage of up to 60%<br>To identify and resolve any potential performance bottlenecks in the application</em></p>



<p><em><strong>Scope:<br></strong>The banking web application will be tested using load, stress, and endurance testing methods.<br>The following scenarios will be tested: Creating an account, login, add payee, transfer funds, check balance, applying for a loan, modifying profile, and logout.<br>The testing will be conducted using any Java, Python or Go-based open-source or commercial performance testing tool.</em></p>



<p><em><strong>Timeline:<br></strong>Requirements gathering and test environment validation: 1 week<br>Test scripting: 2 weeks<br>Test execution: 3 weeks<br>Results analysis and sign off: 1 week</em></p>



<p><em><strong>Types of testing:<br></strong>Load testing: To test the application's performance under normal and peak loads<br>Stress testing: To test the application's performance under abnormal and extreme loads.<br>Endurance testing: To test the application's performance over a prolonged period of time</em></p>



<p><em><strong>Defects Lifecycle:<br></strong>All identified defects will be reported and tracked using a defect tracking tool<br>Defects will be prioritized based on their impact on the application's performance<br>The development team will be responsible for fixing the defects<br>The test team will be responsible for verifying the fixes</em></p>



<p><em><strong>Known Issues:<br></strong>Any known issues with the application that may affect its performance will be documented and communicated to the relevant parties<br>Measures will be taken to mitigate the impact of these known issues on the application's performance<br></em></p>



<p><em><strong>Test Data Management:<br></strong>Test data will be created and managed in a separate environment to ensure that it does not affect the production data<br>Test data will be created in a way that represents the actual data and usage patterns of the application</em></p>



<p><em><strong>References:<br></strong>Performance Testing Methodology by XYZ<br>Performance Testing Best Practices by ABC<br>Performance Testing Tools Comparison by DEF<br>Note: The referenced books or articles are hypothetical, you can use any actual references that you have used or planning to use.</em></p>



<h2>Compare tools' features</h2>



<p>I often get questions about tools <em>this or that?. </em>Well, it depends on your requirements, so many factors influence the decision. Let us ask ChatGPT to compare tools' features.</p>



<p><strong>My question:</strong></p>



<p><em>Compare JMeter, LoadRunner, k6, Locust, NeoLoad features. I do not have a budget to invest in a tool. But my engineers are excellent at writing Java or Go or Python code. Suggest which tool I can select for performance testing. My application is web based. I need to inject load from multiple machines easily.</em></p>



<p><strong>ChatGPT's response:</strong></p>



<p>It didn't give any solid conclusion in selecting an open-source tool. It is playing nicely. But it added the tools' features as shown below. </p>



<p><em>JMeter, LoadRunner, k6, Locust, and NeoLoad are all popular performance testing tools that can be used for web-based applications. Here is a comparison of their features:</em></p>



<ul>
<li><em>JMeter: It is an open-source tool that is written in Java and can be used for both load and functional testing. It supports distributed testing and can be run on multiple machines. It has a rich set of built-in samplers and plugins, and allows for easy creation of custom scripts. It also has good reporting capabilities.</em></li>



<li><em>LoadRunner: It is a commercial tool that is widely used for load testing. It supports a wide range of protocols and has a user-friendly interface. It also has advanced features like real-time monitoring and automatic correlation. However, it is quite expensive.</em></li>



<li><em>k6: It is an open-source tool that is written in Go and is focused on load testing. It has a simple script syntax and a built-in performance metrics engine. It supports distributed testing and can be run on multiple machines. It also has a cloud service for running test at scale.</em></li>



<li><em>Locust: It is an open-source tool that is written in Python and is focused on load testing. It uses a simple script syntax, allowing you to easily define user behavior. It supports distributed testing and can be run on multiple machines.</em></li>



<li><em>NeoLoad: It is a commercial tool that is used for load and performance testing. It supports a wide range of protocols and has a user-friendly interface. It also has advanced features like real-time monitoring and automatic correlation. It also has a cloud service for running test at scale.</em></li>
</ul>



<p><em>Since you have a team that is excellent at writing Java or Go or Python code and you do not have a budget to invest in a tool, I would suggest using an open-source tool like JMeter, k6 or Locust. These are all great options and have a wide range of features. They also have active communities and a wealth of resources available online. Among these three, if you have specific requirement for test script creation and handling, you can pick the one that you are comfortable with.</em></p>



<h2>Analyze the performance testing results</h2>



<p>Now, let us ask ChatGPT to analyze the raw performance testing results from JMeter. </p>



<p>Caution: DO NOT SEND ANY CONFIDENTIAL INFO IN THE CHAT.</p>



<p>I am using a dummy data for testing purpose.</p>



<p>My question: <em>Analyze the below performance test results</em>. Then I pasted the below sample data into the chat.</p>



<script src="https://gist.github.com/QAInsights/1251ba40e164482115295ec3404f9db9.js"></script>



<p><strong>ChatGPT Response</strong></p>



<p><em>The performance test results show a series of HTTP requests sent to the URL <a href="http://example.com/">http://example.com/</a> with a mix of 200 and 503 status codes. The elapsed time for each request ranges from 24 to 95 milliseconds. The response message is "OK" for all requests. </em></p>



<p><em>The thread name indicates that the requests were sent from two different thread groups, "Thread Group 1-1" and "Thread Group 1-2". The success column indicates that all requests were successful and no failure message was recorded. </em></p>



<p><em>The bytes and sent bytes columns show that 1591 bytes were received and 109 bytes were sent for each request. The grpThreads and allThreads columns show that there were 2 threads in each group and 2 threads in total. </em></p>



<p><em>The latency, idle time, and connect columns show that the latency for each request was between 0 and 33 milliseconds, there was no idle time, and the connect time was between 0 and 56 milliseconds. Overall, these results indicate that the application performed well, with fast response times and no failures. </em></p>



<p><em>However, it's worth noting that there are a lot of 503 status codes which indicates that the server is returning a service unavailable error. It's also worth noting that this data is only a sample and additional testing will be required to have a better understanding of the application performance.</em></p>



<p>It extracted the correct number of threads, HTTP status codes, bytes, elapsed time, and more. It added its analysis for each parameter and concluded with a generic suggestion. </p>



<h2>Write code for performance testing</h2>



<p>Let us ask ChatGPT to write a code for performance testing for various tools.</p>



<pre class="wp-block-code"><code>Write a LoadRunner script to test https://api.example.com for Web HTTP/HTML protocol</code></pre>



<pre class="wp-block-code"><code>Here is an example of a LoadRunner script that can be used to test the https://api.example.com endpoint using the Web HTTP/HTML protocol:
</code></pre>



<pre class="wp-block-code"><code>// Declare the web_custom_request function
web_custom_request("api_example",
    // Define the request method and endpoint
    "URL=https://api.example.com",
    "Method=GET",
    // Define any headers that need to be sent with the request
    "Header=Content-Type:application/json",
    "Header=Accept:application/json",
    // Define any body data that needs to be sent with the request
    "Body=",
    // Define the response handling function
    "ResponseFormat=JSON",
    "Snapshot=t1.inf",
    LAST);</code></pre>



<pre class="wp-block-code"><code>This script uses the web_custom_request function to send a GET request to the https://api.example.com endpoint. The request includes a "Content-Type" and "Accept" header set to "application/json" in order to indicate that the request and response should be in JSON format. The ResponseFormat is set to "JSON" to handle the response correctly. The Snapshot=t1.inf is used to save the response data to a file named "t1.inf" in the script's results directory.

This script is a basic example and can be modified according to the test scenario. As an example, you can add a parameterization for different inputs, add correlation and validation rules, and add think time, looping, and conditions to the script. Additionally, to load test this, you will have to add Vusers, Ramp up and Ramp down time, and define the test scenario using the controller.</code></pre>



<p>My question on k6:</p>



<pre class="wp-block-code"><code>Write a k6 script to test https://api.example.com with sleep of 2 seconds between the requests and upload the results to k6 cloud
</code></pre>



<p>ChatGPT's response:</p>



<pre class="wp-block-code"><code>Here is an example of a k6 script that can be used to test the https://api.example.com endpoint with a 2-second sleep between requests and upload the results to the k6 cloud:
</code></pre>



<pre class="wp-block-code"><code>import http from "k6/http";
import { sleep } from "k6";
import { Trend } from "k6/metrics";

let myTrend = new Trend("my_trend");

export let options = {
    stages: &#91;
        { duration: "5s", target: 1 },
        { duration: "10s", target: 10 },
        { duration: "5s", target: 0 },
    ],
    thresholds: {
        "http_req_duration": &#91;"p(95)&lt;200"],
    },
    ext: {
        loadimpact: {
            name: "my test",
        },
    },
};

export default function() {
    let response = http.get("https://api.example.com");
    myTrend.add(response.timings.duration);
    sleep(2);
}</code></pre>



<pre class="wp-block-code"><code>This script uses the http.get() method from the k6/http package to send a GET request to the https://api.example.com endpoint. The sleep(2) method is used to wait for 2 seconds between requests. A trend metric "my_trend" is created to track the response duration of the requests.

The stages option is used to define the load test scenario, where it starts with 1 VU for 5 seconds, then increases to 10 VU for 10 seconds and finally decreases to 0 VU for 5 seconds. The thresholds option is used to define the acceptable response time, in this case, it's set to 95 percentile of the response time should be less than 200ms. Finally, the ext option is used to configure the test name and upload the results to the k6 cloud.

You will have to provide your cloud API key in the command line while running the test like this: k6 run -e API_KEY=YOUR_API_KEY script.js

You can also add more options, like adding more requests, passing parameters, adding validation and assertion rules to this script according to your test scenario.
</code></pre>



<p>My question on JMeter:</p>



<pre class="wp-block-code"><code>Write a JMeter script to test https://api.example.com with sleep of 2 seconds between the requests and upload the results to Grafana cloud.
</code></pre>



<p>ChatGPT's response:</p>



<pre class="wp-block-code"><code>Here is an example of a JMeter script that can be used to test the https://api.example.com endpoint with a 2-second sleep between requests and upload the results to Grafana cloud:

1. Start by adding the following plugins to your JMeter instance:

The Backend Listener for sending metrics to Grafana Cloud.
The JMeter Plugins Manager for installing additional plugins.
Create a Thread Group and add a HTTP Request sampler.

2. Set the Server Name or IP to https://api.example.com.
Set the Method to GET.
3. Add a Constant Timer to the HTTP Request sampler and set the Thread Delay to 2000 milliseconds. This will add a 2-second delay between requests.

4. Add a Backend Listener to the Thread Group.

Set the InfluxDB URL to the URL of your Grafana Cloud instance.
Set the InfluxDB User and InfluxDB Password to the appropriate credentials for your Grafana Cloud instance.</code></pre>



<h2>List the risks involved in performance testing</h2>



<p>Let us ask ChatGPT to list the risks involved in performance testing.</p>



<p>ChatGPT's response is insane.</p>



<p><em>Performance testing can have several risks involved, such as:</em></p>



<ol>
<li><em>Incorrect test design: If the test design is not appropriate or does not accurately reflect the real-world usage of the application, the results of the performance test may be unreliable and may not identify the true performance bottlenecks of the application.</em></li>



<li><em>Insufficient data: If the test data is not representative of the real-world usage of the application, the results of the performance test may not be accurate and may not identify the true performance bottlenecks of the application.</em></li>



<li>Inadequate test environment: If the test environment is not representative of the production environment, the results of the performance test may not be accurate and may not identify the true performance bottlenecks of the application.</li>



<li>Unforeseen system behavior: Performance testing may reveal unexpected system behavior, such as increased resource usage, capacity issues, and other scalability problems that were not anticipated during development.</li>



<li>Overload and crash: Performance testing can cause unexpected overloads and crashes of the application or the testing infrastructure, resulting in application downtime, data loss, and other negative consequences.</li>



<li>Risk to data security: Performance testing may expose sensitive data and pose a risk to data security and compliance.</li>



<li>Lack of test Automation: If you are not automating your performance tests, it will be time-consuming, and manual testing will be prone to errors, and it will be difficult to reproduce the test results.</li>



<li>Inadequate monitoring and analysis: If the monitoring and analysis of the performance test results is not adequate, the true performance bottlenecks of the application may not be identified, and the test results may be unreliable.</li>



<li>Cost: Performance testing can be a resource-intensive process that requires significant investment in terms of hardware, software, and personnel.</li>



<li>Lack of standardization: Without a standardized process and test methodology, performance testing can be prone to errors and inconsistencies, resulting in unreliable test results and an inability to compare results across different tests or test runs.</li>
</ol>



<h2>Build a performance testing tool</h2>



<p>Let us build a performance testing tool in Python (slow, isn't it?) using ChatGPT. Here is my request:</p>



<pre class="wp-block-code"><code>Help me to build a performance testing tool in Python using HTTPX and Panda libraries. I need the following features: 
- ability to test the http requests using multiple threads
- handle the errors
- parameterize the input test data
- analyze the test results
- generate HTML report </code></pre>



<p>ChatGPT's response:</p>



<p>Here is an example of how you can build a performance testing tool in Python using the HTTPX and Pandas libraries:</p>



<ol>
<li>Start by installing the necessary libraries:</li>
</ol>



<pre class="wp-block-code"><code>pip install httpx pandas matplotlib
</code></pre>



<ol start="2">
<li>Create a function that makes HTTP requests using the <code>httpx</code> library. This function should take the input parameters (URL, method, headers, and body) as arguments and return the response.</li>
</ol>



<pre class="wp-block-preformatted">import httpx

def make_request(url, method, headers, body):
    try:
        response = httpx.request(method, url, headers=headers, json=body)
        return response
    except Exception as e:
        print("Error: ", e)
</pre>



<ol start="3">
<li>Create a function that reads the test data from a CSV file and parameterizes it. This function should return the test data in the form of a list of dictionaries.</li>
</ol>



<pre class="wp-block-preformatted">import pandas as pd

def read_test_data(file_path):
    df = pd.read_csv(file_path)
    test_data = df.to_dict(orient='records')
    return test_data</pre>



<ol start="4">
<li>Create a function that runs the performance test using multiple threads. This function should take the test data and the request function as arguments and return the test results in the form of a list of dictionaries.</li>
</ol>



<pre class="wp-block-preformatted">import concurrent.futures

def run_performance_test(test_data, request_function):
    results = []
    with concurrent.futures.ThreadPoolExecutor() as executor:
        futures = [executor.submit(request_function, data['url'], data['method'], data['headers'], data['body']) for data in test_data]
        for future in concurrent.futures.as_completed(futures):
            response = future.result()
            results.append({'url': response.url, 'status_code': response.status_code, 'elapsed_time': response.elapsed.total_seconds()})
    return results
</pre>



<ol start="5">
<li>Create a function to analyze the test results using the Pandas library. This function should take the test results as an argument and return the statistics such as mean, median, and standard deviation of the response time.</li>
</ol>



<pre class="wp-block-preformatted">import pandas as pd

def analyze_results(results):
    df = pd.DataFrame(results)
    statistics = df.describe()
    return statistics
</pre>

<h2>Final Thoughts</h2>
<p>Personally, I have not tested the above snippets. But I have been using ChatGPT to write simple bash, Python, and Groovy snippets. Sometimes, ChatGPT throws incorrect responses, and you can easily trick it. We cannot expect a baby to run on the day it is born. </p>

<p>Let us embrace the power of AI and use it to build a brighter, more prosperous future for ourselves and for generations to come. This revolutionary AI chatbot website is here to change the world, and we must embrace it with all our strength and determination.</p>