天天看點

python locust 性能測試:嵌套

TaskSet類和TaskSequence類可用于嵌套<可以在TaskSequences中嵌套TaskSets,反之亦然>;

from locust import TaskSet, task, HttpLocust, TaskSequence, seq_task
import subprocess


class WebUser(TaskSet):
    @task(5)
    def first_task(self):
        print('執行5次;')

    @task(2)
    class IosUser(TaskSet):
        @task(1)
        def second_task(self):
            print('1次')

        @task(2)
        def three_task(self):
            print('2次')
            self.interrupt()

    @task(2)
    class AndroidUser(TaskSequence):
        @seq_task(2)
        @task(1)
        def android_task(self):
            print('這是android使用者;')
            self.interrupt()

        @seq_task(1)
        @task(1)
        def ios_task(self):
            print('這是ios使用者;')


class LocustFun(HttpLocust):
    host = 'https://passport.cnblogs.com'
    task_set = WebUser
    max_wait = 6000
    min_wait = 3000


if __name__ == '__main__':
    subprocess.check_call(
        'locust -f G:\Interface_framework_pytest\\tmp\\test2.py --no-web -c 10 -r 1')      
self.interrupt()函數用于将執行移交給父TaskSet,需要在嵌套中使用,在Locust類的task_set屬性指向的主TaskSet中使用會報錯:InterruptTaskSet exception具有中斷功能,可以與任務權重一起定義模拟使用者離開論壇的可能性;      
TaskSequence類,裝飾器@seq_task(1),控制任務的執行順序,參數:number;      

轉載于:https://www.cnblogs.com/changqing8023/p/10204951.html