• Locust:深入使用 参数 类 方法


    ###

    前面的文章,对locust进行了简单介绍和使用,

    现在深入一下,

    ###

    Locust no-web模式

    熟悉 Apache ab 工具的同学都知道,它是没有界面的,通过命令行执行。 Locust 同样也提供的命令行运行,好处就是更节省客户端资源。

    locust -f ./locustfile.py --host=http://127.0.0.1:5000 --headless -u 1 -r 1 -t 1m

    --headless 表示不使用Web界面运行测试。

    -u 设置虚拟用户数。

    -r 设置每秒启动虚拟用户数。

    -t 设置设置运行时间。

    如果不知道怎么使用,就使用locust --help 查看一下帮助文档,

    #### 

    Locust 参数说明

    是时候该讲一下 locust 工具的参数了,虽然前面几节我们已经使用了不少参数,例如 “-f” 、“–host” 等。

    打开命令提示符(或Linux终端),输入 locust --help 。

    Usage: locust [OPTIONS] [UserClass ...]
    
    Common options:
      -h, --help            show this help message and exit  # 这是查看帮助文档
      -f LOCUSTFILE, --locustfile LOCUSTFILE     # 这是指定运行脚本
                            Python module file to import, e.g. '../other.py'. Default: locustfile
      --config CONFIG       Config file path
      -H HOST, --host HOST  Host to load test in the following format: http://10.21.32.33  # 这是指定host
      -u NUM_USERS, --users NUM_USERS  # 这是指定用户数
                            Peak number of concurrent Locust users. Primarily used together with --headless or --autostart. Can be changed during a test by keyboard inputs w, W (spawn 1, 10 users) and s, S (stop 1, 10 users)
      -r SPAWN_RATE, --spawn-rate SPAWN_RATE  # 这是指定每秒增加的用户数 可以和 --headless一起使用 
                            Rate to spawn users at (users per second). Primarily used together with --headless or --autostart
      -t RUN_TIME, --run-time RUN_TIME  # 这是运行时间
                            Stop after the specified amount of time, e.g. (300s, 20m, 3h, 1h30m, etc.). Only used together with --headless or --autostart. Defaults to run forever.
      -l, --list            Show list of possible User classes and exit
    
    Web UI options:
      --web-host WEB_HOST   Host to bind the web interface to. Defaults to '*' (all interfaces)
      --web-port WEB_PORT, -P WEB_PORT
                            Port on which to run web host
      --headless            Disable the web interface, and start the test immediately. Use -u and -t to control user count and run time  # 无页面启动
      --autostart           Starts the test immediately (without disabling the web UI). Use -u and -t to control user count and run time
      --autoquit AUTOQUIT   Quits Locust entirely, X seconds after the run is finished. Only used together with --autostart. The default is to keep Locust running until you shut it down using CTRL+C
      --web-auth WEB_AUTH   Turn on Basic Auth for the web interface. Should be supplied in the following format: username:password
      --tls-cert TLS_CERT   Optional path to TLS certificate to use to serve over HTTPS
      --tls-key TLS_KEY     Optional path to TLS private key to use to serve over HTTPS
    
    Master options:
      Options for running a Locust Master node when running Locust distributed. A Master node need Worker nodes that connect to it before it can run load tests.
    
      --master              Set locust to run in distributed mode with this process as master  # Locust 分布式模式使用,当前节点为 master 节点。
      --master-bind-host MASTER_BIND_HOST
                            Interfaces (hostname, ip) that locust master should bind to. Only used when running with --master. Defaults to * (all available interfaces).
      --master-bind-port MASTER_BIND_PORT
                            Port that locust master should bind to. Only used when running with --master. Defaults to 5557.
      --expect-workers EXPECT_WORKERS
                            How many workers master should expect to connect before starting the test (only when --headless/autostart is used).
      --expect-workers-max-wait EXPECT_WORKERS_MAX_WAIT
                            How long should the master wait for workers to connect before giving up. Defaults to wait forever
    
    Worker options:
      Options for running a Locust Worker node when running Locust distributed.    # 这是分布式的一个节点,
      Only the LOCUSTFILE (-f option) needs to be specified when starting a Worker, since other options such as -u, -r, -t are specified on the Master node.
    
      --worker              Set locust to run in distributed mode with this process as worker
      --master-host MASTER_NODE_HOST
                            Host or IP address of locust master for distributed load testing. Only used when running with --worker. Defaults to 127.0.0.1.
      --master-port MASTER_NODE_PORT
                            The port to connect to that is used by the locust master for distributed load testing. Only used when running with --worker. Defaults to 5557.
    
    Tag options:
      Locust tasks can be tagged using the @tag decorator. These options let specify which tasks to include or exclude during a test.
    
      -T [TAG ...], --tags [TAG ...]
                            List of tags to include in the test, so only tasks with any matching tags will be executed
      -E [TAG ...], --exclude-tags [TAG ...]
                            List of tags to exclude from the test, so only tasks with no matching tags will be executed
    
    Request statistics options:
      --csv CSV_PREFIX      Store current request stats to files in CSV format. Setting this option will generate three files: [CSV_PREFIX]_stats.csv, [CSV_PREFIX]_stats_history.csv and [CSV_PREFIX]_failures.csv
      --csv-full-history    Store each stats entry in CSV format to _stats_history.csv file. You must also specify the '--csv' argument to enable this.
      --print-stats         Print stats in the console
      --only-summary        Only print the summary stats   # 只打印摘要统计
      --reset-stats         Reset statistics once spawning has been completed. Should be set on both master and workers when running in distributed mode
      --html HTML_FILE      Store HTML report file  # 这是输出html报告
    
    Logging options:
      --skip-log-setup      Disable Locust's logging setup. Instead, the configuration is provided by the Locust test or Python defaults.
      --loglevel LOGLEVEL, -L LOGLEVEL
                            Choose between DEBUG/INFO/WARNING/ERROR/CRITICAL. Default is INFO.
      --logfile LOGFILE     Path to log file. If not set, log will go to stderr
    
    Other options:
      --show-task-ratio     Print table of the User classes' task execution ratio
      --show-task-ratio-json
                            Print json data of the User classes' task execution ratio
      --version, -V         Show program's version number and exit  # 查看版本
      --exit-code-on-error EXIT_CODE_ON_ERROR
                            Sets the process exit code to use when a test result contain any failure or error
      -s STOP_TIMEOUT, --stop-timeout STOP_TIMEOUT
                            Number of seconds to wait for a simulated user to complete any executing task before exiting. Default is to terminate immediately. This parameter only needs to be specified 
                   for the master process when running Locust distributed. --equal-weights Use equally distributed task weights, overriding the weights specified in the locustfile. --enable-rebalancing Allow to automatically rebalance users if new workers are added or removed during a test run. User classes: UserClass Optionally specify which User classes that should be used (available User classes can be listed with -l or --list)

    ### 

    分布式运行Locust

    一旦单台机器不够模拟足够多的用户时,Locust支持运行在多台机器中进行压力测试。

    为了实现这个,你应该在 master 模式中使用--master标记来启用一个 Locust 实例。这个实例将会运行你启动测试的 Locust 交互网站并查看实时统计数据。master 节点的机器自身不会模拟任何用户。

    相反,你必须使用 --worker 标记启动一台到多台 Locust worker 机器节点,与标记 --master-host 一起使用(指出master机器的IP/hostname)。

    常用的做法是在一台独立的机器中运行master,在worker机器中每个处理器内核运行一个worker实例。

    注意:master 和每一台 worker 机器,在运行分布式测试时都必须要有 locust 的测试文件。

    示例

    在 master 模式下启动 Locust:

    locust -f my_loucstfile.py --master

    在每个 worker 中执行(192.168.0.14 替换为你 msater 的IP):

    locust -f my_locustfile.py --worker --master-host=192.168.0.14

    参数说明

    参数

    –master   设置 Locust 为 master 模式。网页交互会在这台节点机器中运行。

    –worker   设置 Locust 为 slave 模式。

    –master-host=X.X.X.X  可选项,与 --slave 一起结合使用,用于设置 master 模式下的 master 机器的IP/hostname(默认设置为127.0.0.1)

    –master-port=5557  可选项,与 --slave 一起结合使用,用于设置 master 模式下的 master 机器中 Locust 的端口(默认为5557)。注意,locust 将会使用这个指定的端口号,同时指定端口+1的号也会被占用。因此,5557 会被使用,Locust将会使用 5557 和 5558。

    –master-bind-host=X.X.X.X`   可选项,与 --master 一起结合使用。决定在 master 模式下将会绑定什么网络接口。默认设置为*(所有可用的接口)。

    –master-bind-port=5557   可选项,与 --master 一起结合使用。决定哪个网络端口 master 模式将会监听。默认设置为 5557。注意 Locust 会使用指定的端口号,同时指定端口+1的号也会被占用。因此,5557 会被使用,Locust 将会使用 5557 和 5558。

    –expect-worker=X   在 no-web 模式下启动 master 时使用。master 将等待X连接节点在测试开始之前连接。

    ####

    locust的类和方法

    针对你的业务,你如何进行测试测试,需要通过编写性能测试脚本实现。所以,我们要熟悉 Locust 提供了哪些类和方法,它们分别实现什么操作。

    还是要看官网的例子:https://docs.locust.io/en/stable/writing-a-locustfile.html

    Now, lets look at a more complete/realistic example of what your tests might look like:

    一个更复杂,更真实的例子

    import time
    from locust import HttpUser, task, between
    
    class QuickstartUser(HttpUser):
        wait_time = between(1, 5)
    
        @task
        def hello_world(self):
            self.client.get("/hello")
            self.client.get("/world")
    
        @task(3)
        def view_items(self):
            for item_id in range(10):
                self.client.get(f"/item?id={item_id}", name="/item")
                time.sleep(1)
    
        def on_start(self):
            self.client.post("/login", json={"username":"foo", "password":"bar"})

    ##

    1,继承HttpUser,然后就会有一个client方法

    2,wait_time = between(1, 5),Our class defines a wait_time that will make the simulated users wait between 1 and 5 seconds after each task (see below) is executed.

    3,view_items(self): In the view_items task we load 10 different URLs by using a variable query parameter. 

    4,还有很多,后面要细细研究一下,

     https://docs.locust.io/en/stable/api.html

    这是完整的官网的locust的类和方法的文档,


    ###

    ####

  • 相关阅读:
    刷题-力扣-112. 路径总和
    刷题-力扣-1137. 第 N 个泰波那契数
    刷题-力扣-101. 对称二叉树
    刷题-力扣-99. 恢复二叉搜索树
    刷题-力扣-109. 有序链表转换二叉搜索树
    刷题-力扣-108. 将有序数组转换为二叉搜索树
    刷题-力扣-111. 二叉树的最小深度
    刷题-力扣-110. 平衡二叉树
    2019春Python程序设计练习6(0423--0429)
    2019春Python程序设计练习5(0416--0422)
  • 原文地址:https://www.cnblogs.com/andy0816/p/15618766.html
Copyright © 2020-2023  润新知