Locust性能-零基础入门系列(2) -重写wait_time

       在虚拟模拟的时候,可能对等待时间有更高的要求,好比假若有这么一个场景要求:某任务要求每被执行1次,那么下次的等待时间就➕1秒钟。这种状况,是能够实现的,这也就体现了Locust的灵活性。可编程性,不少比较棘手的场景模拟难题,均可以经过编程的方式解决掉。

      具体如何解决呢?自定义wait_time函数,实现源代码以下:python

def wait_time(self):
        self.last_wait_time += 1
        return self.last_wait_time

     经过以上,每次执行task的时候 wait_time的源码都会被执行1次,那么咱们的场景模拟的目的就达到了。完整的locust file源码以下:编程



from locust import  User,task
import time
class MyUser(User):
    last_wait_time = 0
    #能够自定义wait time for locust
    def wait_time(self):
        self.last_wait_time += 1
        return self.last_wait_time
    @task
    def my_task(self):
        nowTime = time.strftime("%Y-%m-%d %H:%M:%S")
        print("Executing my task,and the time now is:" + nowTime)

      经过简单分析,若是场景模拟是正确的,那么每次打印的时间差异,应该是1/2/3/4...... 秒,这么递增的,执行完以后,以下所示,是和预想是一致的。app


/locust.runners: All users hatched: MyUser: 1 (0 already running)
Executing my task,and the time now is:2020-09-20 20:21:52
Executing my task,and the time now is:2020-09-20 20:21:53
Executing my task,and the time now is:2020-09-20 20:21:55
Executing my task,and the time now is:2020-09-20 20:21:58

    你们也能够扫描并关注以下公众号“TimTest”,会有更多性能测试相关内容分享。ide

qrcode_for_gh_39009e949117_258-1.jpg