python轻量级性能工具Locust怎么安装和使用
这篇文章主要讲解了“python轻量级性能工具Locust怎么安装和使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python轻量级性能工具Locust怎么安装和使用”吧!
Locust基于python的协程机制,打破了线程进程的限制,可以能够在一台测试机上跑高并发
性能测试基础
1.快慢:衡量系统的处理效率:响应时间
2.多少:衡量系统的处理能力:单位时间内能处理多少个事务(tps)
性能测试根据测试需求最常见的分为下面三类
1 负载测试load testing
不断向服务器加压,值得预定的指标或者部分系统资源达到瓶颈,目的是找到系统最大负载的能力
2 压力测试
通过高负载持续长时间,来验证系统是否稳定
3 并发测试:
同时像服务器提交请求,目的发现系统是否存在事务冲突或者锁升级的现象
性能负载模型
locust安装
安装存在问题,可以通过豆瓣源下载
pipinstalllocust
locust模板
基本上多数的场景我们都可以基于这个模板read.py去做修改
fromlocustimportHttpUser,TaskSet,task,tag,events#启动locust时运行@events.test_start.add_listenerdefsetup(environment,**kwargs):#print("tasksetup")#停止locust时运行@events.test_stop.add_listenerdefteardown(environment,**kwargs):print("taskteardown")classUserBehavor(TaskSet):#虚拟用户启用task运行defon_start(self):print("start")locusts_spawned.wait()#虚拟用户结束task运行defon_stop(self):print("stop")@tag('test1')@task(2)defindex(self):self.client.get('/yetangjian/p/17320268')@task(1)definfo(self):self.client.get("/yetangjian/p/17253215")classWebsiteUser(HttpUser):defsetup(self):print("locustsetup")defteardown(self):print("locustteardown")host="http:/blogs"task_set=task(UserBehavor)min_wait=3000max_wait=5000
注:这里我们给了一个webhost,这样我们可以直接在浏览器中打开locust
集合点lr_rendezvous
当然我们可以把集合点操作放入上述模板的setup中去运行起来
locusts_spawned=Semaphore()locusts_spawned.acquire()defon_hatchplete(**kwargs):"""select_task类的钩子函数:paramkwargs::return:"""locusts_spawned.release()events.spawningplete.add_listener(on_hatchplete)n=0classUserBehavor(TaskSet):deflogin(self):globalnn+=1print(f"第{n}个用户登陆")defon_start(self):self.login()locusts_spawned.wait()@taskdeftest1(self):#catch_response获取返回withself.client.get("/yetangjian/p/17253215",catch_response=True):print("查询结束")classWebsiteUser(HttpUser):host="http:/blogs"task_set=task(UserBehavor)wait_time=between(1,3)if__name__=='__main__':os.system('locust-fread.py--web-host="127.0.0.1"')
比较常见的用法
在上面两个例子中我们已经看到了一些,例如装饰器events.test_start.add_listener;events.test_stop.add_listener用来在负载测试前后进行一些操作,又例如on_start、on_stop,在task执行前后运行,又例如task,可以用来分配任务的权重
等待时间
#waitbetween3.0and10.5secondsaftereachtask#wait_time=between(3.0,10.5)#固定时间等待#wait_time=constant(3)#确保每秒运行多少次constant_throughput(task_runs_per_second)#确保每多少秒运行一次constant_pacing(wait_time)
同样也可以在User类下发重写wait_time来达到自定义
tag标记
@tag('test1')@task(2)defindex(self):self.client.get('/yetangjian/p/17320268')
通过对任务打标记,就可以在运行时候执行运行某一些任务:
#只执行标记test1os.system('locust-fread.py--tagstest1--web-host="127.0.0.1"')#不执行标记过的os.system('locust-fread.py--exclude-tags--web-host="127.0.0.1"')#除去test1执行所有os.system('locust-fread.py--exclude-tagstest1--web-host="127.0.0.1"')
自定义失败
#定义响应时间超过0.1就为失败withself.client.get("/yetangjian/p/17253215",catch_response=True)asresponse:ifresponse.elapsed.total_seconds()>0.1:response.failure("Requesttooktoolong")#定义响应码是200就为失败withself.client.get("/yetangjian/p/17320268",catch_response=True)asresponse:ifresponse.status_code==200:response.failure("响应码200,但我定义为失败")
自定义负载形状
自定义一个shape.py通过继承LoadTestShape并重写tick
这个形状类将以100块为单位,20速率的增加用户数,然后在10分钟后停止负载测试(从运行开始的第51秒开始user_count会round到100)
fromlocustimportLoadTestShapeclassMyCustomShape(LoadTestShape):time_limit=600spawn_rate=20deftick(self):run_time=self.get_run_time()ifrun_time<self.time_limit:#Usercountroundedtonearesthundred.user_count=round(run_time,-2)return(user_count,self.spawn_rate)returnNone
运行图如下所示
通过命令行去触发
os.system('locust-fread.py,shape.py--web-host="127.0.0.1"')
不同时间阶段的例子
fromlocustimportLoadTestShapeclassStagesShapeWithCustomUsers(LoadTestShape):stages=[{"duration":10,"users":10,"spawn_rate":10},{"duration":30,"users":50,"spawn_rate":10},{"duration":60,"users":100,"spawn_rate":10},{"duration":120,"users":100,"spawn_rate":10}]deftick(self):run_time=self.get_run_time()forstageinself.stages:ifrun_time<stage["duration"]:tick_data=(stage["users"],stage["spawn_rate"])returntick_datareturnNone
感谢各位的阅读,以上就是“python轻量级性能工具Locust怎么安装和使用”的内容了,经过本文的学习后,相信大家对python轻量级性能工具Locust怎么安装和使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是主机评测网,小编将为大家推送更多相关知识点的文章,欢迎关注!
上一篇:Python装饰器如何实现