locust===官方说明文档,关于tasks

安装:

>>> pip  install locust

locust在官方simple_code中如下:

from locust import HttpLocust, TaskSet

def login(l):
    l.client.post("/login", {"username":"ellen_key", "password":"education"})

def index(l):
    l.client.get("/")

def profile(l):
    l.client.get("/profile")

class UserBehavior(TaskSet):
    tasks = {index: 2, profile: 1}      #注意这一行
    def on_start(self):
        login(self)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

 Here we define a number of Locust tasks, which are normal Python callables that take one argument (a Locust class instance). These tasks are gathered under a TaskSet class in the tasks attribute. Then we have a HttpLocust class which represents a user, where we define how long a simulated user should wait between executing tasks, as well as what TaskSet class should define the user’s “behaviour”. :py:class:`TaskSet <locust.core.TaskSet>`s can be nested.

The HttpLocust class inherits from the Locust class, and it adds a client attribute which is an instance of HttpSession that can be used to make HTTP requests.

from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):
    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        self.login()

    def login(self):
        self.client.post("/login", {"username":"ellen_key", "password":"education"})

    @task(2)
    def index(self):
        self.client.get("/")

    @task(1)
    def profile(self):
        self.client.get("/profile")

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

 The Locust class (as well as HttpLocust since it’s a subclass) also allows one to specify minimum and maximum wait time—per simulated user—between the execution of tasks (min_wait and max_wait) as well as other user behaviours.

Start Locust

在cmd窗口,进入到执行py文件的路径下,进行start,默认的启用方法如下:

>>> locust -f 需要执行的脚本.py --host=http://需要用到的url

官网针对启用,专门的介绍如下:

To run Locust with the above Locust file, if it was named locustfile.py and located in the current working directory, we could run:

locust --host=http://example.com

If the Locust file is located under a subdirectory and/or named different than locustfile.py, specify it using -f:

locust -f locust_files/my_locust_file.py --host=http://example.com

To run Locust distributed across multiple processes we would start a master process by specifying --master:

locust -f locust_files/my_locust_file.py --master --host=http://example.com

and then we would start an arbitrary number of slave processes:

locust -f locust_files/my_locust_file.py --slave --host=http://example.com

If we want to run Locust distributed on multiple machines we would also have to specify the master host when starting the slaves (this is not needed when running Locust distributed on a single machine, since the master host defaults to 127.0.0.1):

locust -f locust_files/my_locust_file.py --slave --master-host=192.168.0.100 --host=http://example.com

You may wish to consume your Locust results via a csv file. In this case, there are two ways to do this.

First, when running the webserver, you can retrieve a csv from localhost:8089/stats/requests/csv and localhost:8089/stats/distribution/csv. Second you can run Locust with a flag which will periodically save the csv file. This is particularly useful if you plan on running Locust in an automated way with the --no-web flag:

locust -f locust_files/my_locust_file.py --csv=foobar --no-web -n10 -c1

You can also customize how frequently this is written if you desire faster (or slower) writing:


import locust.stats
locust.stats.CSV_STATS_INTERVAL_SEC = 5 # default is 2 seconds



To see all available options type:

locust --help


Open up Locust’s web interface


Once you’ve started Locust using one of the above command lines, you should open up a browser and point it to http://127.0.0.1:8089 (if you are running Locust locally)

#默认调用的端口是8089
原文地址:https://www.cnblogs.com/botoo/p/7542905.html