Using ChatGPT for Performance Testing: Data Generation, Cookie Debugging, and JMeter Scripts
This article walks through creating performance‑test data with Excel and a Python script, troubleshooting cookie handling issues in Java web applications, and building a complete JMeter test plan—including thread groups, CSV data sets, and assertions—to validate an e‑business login flow.
Donald Knuth’s warning that “premature optimization is the root of all evil” frames the discussion of performance testing in this chapter.
5.1 Generating Performance‑Test Data
A simple Excel sheet is used to create 1,000 rows of user credentials (e.g., User000,Password@000,13681730000,[email protected]). A Python script ( test_for_data.py) reads the CSV, connects to a MySQL instance at 192.168.31.184, hashes each password with SHA‑256, and inserts the rows into the user table.
#-*-coding:utf-8-*-
import pymysql
import hashlib
class ClassDB:
def initDB(self):
host='192.168.31.184'
user='root'
password='123456'
database='ebusiness'
connection=pymysql.connect(host=host,user=user,password=password,database=database,charset='utf8')
return connection
def closedb(self,connection):
connection.close()
def init_login(self):
connection=self.initDB()
try:
with connection.cursor() as cursor:
for i in range(1000):
username="User%03d"%i
password=self.hash_password("Password@%03d"%i)
phone="13681730%03d"%i
email="xianggu%[email protected]"%i
sql="INSERT INTO user(username,password,phone,email) VALUES(\"%s\",\"%s\",\"%s\",\"%s\")"%(username,password,phone,email)
cursor.execute(sql)
connection.commit()
finally:
self.closedb(connection)
def hash_password(self,password):
return hashlib.sha256(password.encode('utf-8')).hexdigest()
if __name__=="__main__":
db=ClassDB()
db.init_login()5.2 Building the Performance‑Test Script
During script development two problems were encountered.
5.2.1 Cookie Issues on LAN IPs
When setting a cookie with new Cookie(name,value) in a Java servlet, the cookie works on 127.0.0.1 but not on a LAN address such as 192.168.1.164. ChatGPT explains the likely causes:
Domain attribute : without an explicit domain the cookie is scoped to the host that set it, so it is not sent when the request originates from a different IP.
Path attribute : the cookie is only sent to the path where it was created and its sub‑paths.
SameSite attribute : some browsers restrict cross‑site cookie transmission.
Protocol mismatch : using HTTP on localhost and HTTPS on the LAN address can block the cookie.
Browser settings/extensions : may block cookie storage.
Suggested solution snippets include setting the domain to the LAN IP, ensuring the path is /, and using a consistent protocol.
public class CookieExampleServlet extends HttpServlet{
private String cookies_name="myCookie";
private String cookies_value="myValue";
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
Cookie cookie=new Cookie(this.cookies_name,this.cookies_value);
cookie.setPath(request.getContextPath());
cookie.setDomain("192.168.31.184"); // match the IP
cookie.setMaxAge(60*60); // 1 hour
response.addCookie(cookie);
response.getWriter().println("Cookie has been set.");
}
}Unit‑test adjustments for CookiesManager verify that the cookie name, value and max‑age are correct, and that clearing the cookie sets maxAge=0.
@Test
void testSetCookie(){
cookiesManager.setCookie();
verify(response).addCookie(argThat(cookie->"testCookie".equals(cookie.getName()) && "testValue".equals(cookie.getValue()) && cookie.getMaxAge()==60*60*24));
}
@Test
void testClearCookie(){
cookiesManager.clearCookie();
verify(response).addCookie(argThat(cookie->"testCookie".equals(cookie.getName()) && cookie.getMaxAge()==0));
}5.2.2 JMeter Cannot Access Remote Database
When JMeter tries to connect to the MySQL server at 192.168.31.184, the driver reports Access denied for user 'root'@'192.168.31.184'. ChatGPT lists possible reasons and remedies:
Missing user account for that host.
Insufficient privileges for ALTER USER or other operations.
Host mismatch between the account and the connecting IP.
MySQL version‑specific restrictions.
Solution steps include checking the mysql.user table, creating or updating the user, granting privileges, and flushing privileges:
SELECT User,Host FROM mysql.user WHERE User='root';
CREATE USER 'root'@'192.168.31.184' IDENTIFIED BY 'new_password';
GRANT ALL PRIVILEGES ON ebusiness.* TO 'root'@'192.168.31.184';
FLUSH PRIVILEGES;5.2.3 Constructing the JMeter Test Plan
The final JMeter script ( ChatGPT.jmx) defines a test plan with a thread group of 4,062 users, a 1‑second ramp‑up, a 10‑minute duration, and a CSV data set that feeds the usernames, passwords, phones and emails generated earlier. It includes a CookieManager, HeaderManager, HTTP samplers for the login page and the post‑login page, a SyncTimer to simulate concurrent users (10 % of online users), assertions on the response title, and result collectors for summary, graph and tree views.
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.6.3">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="测试计划">
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量">
<collectionProp name="Arguments.arguments"/>
</elementProp>
</TestPlan>
<hashTree>
<CookieManager guiclass="CookiePanel" testclass="CookieManager" testname="HTTP Cookie管理器">
<collectionProp name="CookieManager.cookies"/>
<boolProp name="CookieManager.clearEachIteration">false</boolProp>
<boolProp name="CookieManager.controlledByThreadGroup">false</boolProp>
</CookieManager>
<hashTree/>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP信息头管理器" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="Accept-Language" elementType="Header">
<stringProp name="Header.name">Accept-Language</stringProp>
<stringProp name="Header.value">zh-CN,zh;q=0.9</stringProp>
</elementProp>
... (other headers omitted for brevity) ...
</collectionProp>
</HeaderManager>
<hashTree/>
<ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="HTTP请求默认值" enabled="true">
<stringProp name="HTTPSampler.domain">192.168.31.184</stringProp>
<stringProp name="HTTPSampler.port">8080</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding">utf-8</stringProp>
</ConfigTestElement>
<hashTree/>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="线程组">
<intProp name="ThreadGroup.num_threads">4062</intProp>
<intProp name="ThreadGroup.ramp_time">1</intProp>
<longProp name="ThreadGroup.duration">600</longProp>
<boolProp name="ThreadGroup.scheduler">true</boolProp>
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器">
<intProp name="LoopController.loops">-1</intProp>
<boolProp name="LoopController.continue_forever">false</boolProp>
</elementProp>
</ThreadGroup>
<hashTree>
<CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV 数据文件设置" enabled="true">
<stringProp name="filename">testdata.csv</stringProp>
<...> (CSV configuration omitted) </...>
</CSVDataSet>
<...> (HTTP samplers, timers, assertions, result collectors omitted for brevity) ...
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>The article concludes that by generating realistic test data, correctly configuring cookie attributes, ensuring proper MySQL user permissions, and assembling a comprehensive JMeter plan, developers can obtain reliable performance metrics for the e‑business login workflow.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Woodpecker Software Testing
The Woodpecker Software Testing public account shares software testing knowledge, connects testing enthusiasts, founded by Gu Xiang, website: www.3testing.com. Author of five books, including "Mastering JMeter Through Case Studies".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
