Locustfile中的User类和HttpUser类

[[399412]]

本文转载自微信公众号「dongfanger」,作者dongfanger。转载本文请联系dongfanger公众号。

创新互联是一家集网站建设,攸县企业网站建设,攸县品牌网站建设,网站定制,攸县网站建设报价,网络营销,网络优化,攸县网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

locustfile是什么?

locustfile是Locust性能测试工具的用户脚本,描述了单个用户的行为。

locustfile是个普通的Python模块,如果写作locustfile.py,那么路径切换到文件所在目录,直接执行命令就能运行:

 
 
 
 
  1. $ locust 

如果换个名字,那么只能通过-f参数指定文件名运行:

 
 
 
 
  1. $ locust -f locust_files/my_locust_file.py 

与一般Python模块不同的是:locustfile必须至少定义一个类,且继承自User类。

User类

User类表示性能测试的模拟用户,Locust会在运行时创建User类的实例。

wait_time属性

设置等待时间,默认值不等待,立即执行。

Locust支持4种方式设置wait_time属性。

为了便于理解实际意义,我把源码贴在了下面。

  • constant函数,常量,任务执行完毕等待X秒开始下一任务。
 
 
 
 
  1. def constant(wait_time): 
  2.     """ 
  3.     Returns a function that just returns the number specified by the wait_time argument 
  4.  
  5.     Example:: 
  6.  
  7.         class MyUser(User): 
  8.             wait_time = constant(3) 
  9.     """ 
  10.     return lambda instance: wait_time 
  • between函数,区间随机值,任务执行完毕等待X-Y秒(中间随机取值)开始下一任务。
 
 
 
 
  1. def between(min_wait, max_wait): 
  2.     """ 
  3.     Returns a function that will return a random number between min_wait and max_wait. 
  4.  
  5.     Example:: 
  6.  
  7.         class MyUser(User): 
  8.             # wait between 3.0 and 10.5 seconds after each task 
  9.             wait_time = between(3.0, 10.5) 
  10.     """ 
  11.     return lambda instance: min_wait + random.random() * (max_wait - min_wait) 
  • constant_pacing函数,自适应,若任务耗时超过该时间,则任务结束后立即执行下一任务;若任务耗时不超过该时间,则等待达到该时间后执行下一任务。
 
 
 
 
  1. def constant_pacing(wait_time): 
  2.     """ 
  3.     Returns a function that will track the run time of the tasks, and for each time it's 
  4.     called it will return a wait time that will try to make the total time between task 
  5.     execution equal to the time specified by the wait_time argument. 
  6.  
  7.     In the following example the task will always be executed once every second, no matter 
  8.     the task execution time:: 
  9.  
  10.         class MyUser(User): 
  11.             wait_time = constant_pacing(1) 
  12.             @task 
  13.             def my_task(self): 
  14.                 time.sleep(random.random()) 
  15.  
  16.     If a task execution exceeds the specified wait_time, the wait will be 0 before starting 
  17.     the next task. 
  18.     """ 
  19.  
  20.     def wait_time_func(self): 
  21.         if not hasattr(self, "_cp_last_run"): 
  22.             self._cp_last_wait_time = wait_time 
  23.             self._cp_last_run = time() 
  24.             return wait_time 
  25.         else: 
  26.             run_time = time() - self._cp_last_run - self._cp_last_wait_time 
  27.             self._cp_last_wait_time = max(0, wait_time - run_time) 
  28.             self._cp_last_run = time() 
  29.             return self._cp_last_wait_time 
  30.  
  31.     return wait_time_func 
  • 自定义wait_time方法,比如每次等待时间1秒2秒3秒递增:
 
 
 
 
  1. class MyUser(User): 
  2.     last_wait_time = 0 
  3.  
  4.     def wait_time(self): 
  5.         self.last_wait_time += 1 
  6.         return self.last_wait_time 
  7.  
  8.     ... 

weight属性

设置创建类实例的权重,默认每个类创建相同数量的实例。

locustfile中可以有多个继承了User类的类。

命令行可以指定运行哪些类:

 
 
 
 
  1. $ locust -f locust_file.py WebUser MobileUser 

通过weight属性可以让类更大概率创建实例,比如:

 
 
 
 
  1. class WebUser(User): 
  2.     weight = 3 
  3.     ... 
  4.  
  5. class MobileUser(User): 
  6.     weight = 1 
  7.     ... 

WebUser类比MobileUser类多三倍概率创建实例。

host属性

设置URL前缀。

一般是在Locust的Web UI或者命令行,通过--host指定URL前缀。如果没有通过--host指定,并且类中设置了host属性,那么类的host属性才会生效。

environment属性

对用户运行环境的引用。

比如在task方法中通过environment属性终止运行:

 
 
 
 
  1. self.environment.runner.quit() 

注意,单机会终止所有运行,分布式只会终止单个worker节点。

on_start和on_stop方法

测试前初始化和测试后清理。

HttpUser类

开篇文章的示例脚本,没有继承User类,而是继承了它的子类HttpUser:

它比User类更常用,因为它添加了一个client属性,用来发送HTTP请求。

client属性/HttpSession

HttpUser类的client属性是HttpSession类的一个实例:

HttpSession是requests.Session的子类,requests就是常用来做接口测试的那个requests库:

HttpSession没有对requests.Session做什么改动,主要是传递请求结果给Locust,比如success/fail,response time,response length,name。

示例:

 
 
 
 
  1. response = self.client.post("/login", {"username":"testuser", "password":"secret"}) 
  2. print("Response status code:", response.status_code) 
  3. print("Response text:", response.text) 
  4. response = self.client.get("/my-profile") 

由于requests.Session会暂存cookie,所以示例中登录/login请求后可以继续请求/my-profile。

断言响应结果

可以使用with语句和catch_response参数对响应结果进行断言:

 
 
 
 
  1. with self.client.get("/", catch_response=True) as response: 
  2.     if response.text == "Success": 
  3.         response.success() 
  4.     elif response.text != "Success": 
  5.         response.failure("Got wrong response") 
  6.     elif response.elapsed.total_seconds() > 0.5: 
  7.         response.failure("Request took too long") 

或者直接抛出异常:

 
 
 
 
  1. from locust.exception import RescheduleTask 
  2. ... 
  3. with self.client.get("/does_not_exist/", catch_response=True) as response: 
  4.     if response.status_code == 404: 
  5.         raise RescheduleTask() 

name参数

name参数用于把不同api按同一分组进行统计,比如:

 
 
 
 
  1. for i in range(10): 
  2.     self.client.get("/blog?id=%i" % i, name="/blog?id=[id]") 

会按/blog/?id=[id]统计1条数据,而不是分成10条数据。

HTTP代理

Locust默认设置了requests.Session的trust_env为False,不查找代理,以提高运行性能。如果需要可以设置locust_instance.client.trust_env为True。

示例代码

请求REST API并断言:

 
 
 
 
  1. from json import JSONDecodeError 
  2. ... 
  3. with self.client.post("/", json={"foo": 42, "bar": None}, catch_response=True) as response: 
  4.     try: 
  5.         if response.json()["greeting"] != "hello": 
  6.             response.failure("Did not get expected value in greeting") 
  7.     except JSONDecodeError: 
  8.         response.failure("Response could not be decoded as JSON") 
  9.     except KeyError: 
  10.         response.failure("Response did not contain expected key 'greeting'") 

小结

locustfile是个普通Python模块,必须继承User类或其子类HttpUser等。本文对User类和HttpUser类的属性和方法进行了介绍,使用它们可以编写性能测试的用户脚本。locustfile还有另外一个重要组成元素,@task。

分享名称:Locustfile中的User类和HttpUser类
当前网址:http://www.shufengxianlan.com/qtweb/news35/223285.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联