对电商网站的查询功能进行性能测试,以上是查询的关键字,通过爬虫技术取出以上关键字并存入数据文件data.csv
import requests
from lxml import etree
file=open("data.csv","w")
response=requests.get("http://localhost/iwebshop/index.php?controller=site&action=sitemap").text
#print(response)
doc=etree.HTML(response)
for i in range(1,6):
keyword=doc.xpath("/html/body/div[1]/div[6]/div[2]/div[3]/table["+str(i)+"]/tbody/tr[2]/td/a/text()")
for ele in keyword:
file.write(str(ele)+"\n")
file.close()
import csv
import os
from locust import HttpUser,task,TaskSet
import datetime
#定义测试类:用户行为
class UseTasker(TaskSet):
@task
def test_search(self):
file=open("data.csv","r")
file1=open("search_result.csv","a")
row=csv.reader(file)
for word in row:
response=self.client.get("/index.php?controller=site&action=search_list&word="+str(word)).text
loc=response.find(str(word))
te = datetime.datetime.now()
if loc>=0:
r=(str(te)+str(word)+'测试成功')
file1.write(r+"\n")
else:
r=(str(te)+str(word)+"测试失败")
file1.write(r+"\n")
file1.close()
class WebUser(HttpUser):
host="http://localhost/iwebshop/"
tasks=[UseTasker]
#若已存在search_result.csv文件,先执行删除
result=os.path.exists("search_result.csv")
if result:
os.remove("search_result.csv")
min_wait=2000
max_wait=5000
命令行输入:locust -f 脚本名+回车
打开浏览器,打开locust页面,输入虚拟用户总数和每秒并发用户数,点击开始测试。
当要停止测试,现在命令行按ctrl+c,
再在网页按右上角的stop.此时,测试数据已经存入文件: