zoukankan      html  css  js  c++  java
  • 高并发压力测试工具Locust(蝗虫)

    What is Locust?

    Locust is an easy-to-use, distributed, user load testing tool. It is intended for load-testing web sites (or other systems) and figuring out how many concurrent users a system can handle.

    The idea is that during a test, a swarm of locusts will attack your website. The behavior of each locust (or test user if you will) is defined by you and the swarming process is monitored from a web UI in real-time. This will help you battle test and identify bottlenecks in your code before letting real users in.

    Locust is completely event-based, and therefore it’s possible to support thousands of concurrent users on a single machine. In contrast to many other event-based apps it doesn’t use callbacks. Instead it uses light-weight processes, through gevent. Each locust swarming your site is actually running inside its own process (or greenlet, to be correct). This allows you to write very expressive scenarios in Python without complicating your code with callbacks.

    蝗虫是什么?

    蝗虫是一个易于使用、分发、用户负载测试工具。它是用于负载测试的网站(或其他系统),弄清楚有多少并发用户系统可以处理。

    我们的想法是,在测试期间,一群蝗虫会攻击你的网站。每个蝗虫的行为(或测试用户如果你愿意)被定义为你和聚集过程实时监控从web UI。这将帮助你战斗测试和代码中识别瓶颈之前让真正的用户。

    蝗虫是完全基于事件,因此可以支持在单个机器上成千上万的并发用户。与许多其他的基于事件的应用程序不使用回调。相反,它使用轻量级进程,通过gevent。每个蝗虫爬你的网站实际上是运行在它自己的进程(或一种绿色小鸟,是正确的)。这允许您编写非常富有表现力的场景在Python中没有复杂的代码和回调。

    Installation

    Locust is available on PyPI and can be installed through pip or easy_install

    pip install locustio

    or:

    easy_install locustio

    When Locust is installed, a locust command should be available in your shell (if you’re not using virtualenv—which you should—make sure your python script directory is on your path).

    To see available options, run:

    locust --help
    Options:
      -h, --help            show this help message and exit
      -H HOST, --host=HOST  Host to load test in the following format:
                            http://10.21.32.33
      --web-host=WEB_HOST   Host to bind the web interface to. Defaults to '' (all
                            interfaces)
      -P PORT, --port=PORT, --web-port=PORT
                            Port on which to run web host
      -f LOCUSTFILE, --locustfile=LOCUSTFILE
                            Python module file to import, e.g. '../other.py'.
                            Default: locustfile
      --master              Set locust to run in distributed mode with this
                            process as master
      --slave               Set locust to run in distributed mode with this
                            process as slave
      --master-host=MASTER_HOST
                            Host or IP address of locust master for distributed
                            load testing. Only used when running with --slave.
                            Defaults to 127.0.0.1.
      --master-port=MASTER_PORT
                            The port to connect to that is used by the locust
                            master for distributed load testing. Only used when
                            running with --slave. Defaults to 5557. Note that
                            slaves will also connect to the master node on this
                            port + 1.
      --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. Note that
                            Locust will also use this port + 1, so by default the
                            master node will bind to 5557 and 5558.
      --no-web              Disable the web interface, and instead start running
                            the test immediately. Requires -c and -r to be
                            specified.
      -c NUM_CLIENTS, --clients=NUM_CLIENTS
                            Number of concurrent clients. Only used together with
                            --no-web
      -r HATCH_RATE, --hatch-rate=HATCH_RATE
                            The rate per second in which clients are spawned. Only
                            used together with --no-web
      -n NUM_REQUESTS, --num-request=NUM_REQUESTS
                            Number of requests to perform. Only used together with
                            --no-web
      -L LOGLEVEL, --loglevel=LOGLEVEL
                            Choose between DEBUG/INFO/WARNING/ERROR/CRITICAL.
                            Default is INFO.
      --logfile=LOGFILE     Path to log file. If not set, log will go to
                            stdout/stderr
      --print-stats         Print stats in the console
      --only-summary        Only print the summary stats
      -l, --list            Show list of possible locust classes and exit
      --show-task-ratio     print table of the locust classes' task execution
                            ratio
      --show-task-ratio-json
                            print json data of the locust classes' task execution
                            ratio
      -V, --version         show program's version number and exit
     

    Quick start

    Example locustfile.py

    Below is a quick little example of a simple locustfile.py:

    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

    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.

    Another way we could declare tasks, which is usually more convenient, is to use the @task decorator. The following code is equivalent to the above:

    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

    Start Locust

    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

    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). Then you should be greeted with something like this:



    from locust import HttpLocust, TaskSet, task
    
    class WebSite(TaskSet):
        def on_start(self):
            self.login()
    
        def login(self):
            login_data = {'log':'admin','pwd':'admin'}
            res = self.client.post('/wordpress/wp-login.php',login_data)
    
    
            @task()
            def create_task(self):
                self.client.get("/")
    
         
            @task(1)
            def profile(self):
                res = self.client.get("/wordpress/wp-admin")
                   print res.url
    
    
    class Wordpress(HttpLocust): task_set = WebSite min_wait = 1000 max_wait = 1000

    蝗虫比Jmeter好的一点就是 高并发,但是相对的不好的地方也有,就是需要另外的工具去监控服务器,而且需要去编写代码,
    locust还需要再去深入研究琢磨,Locust使用好了 是一款非常强大的性能测试工具
    http://docs.locust.io/en/latest/


  • 相关阅读:
    zsh(yum装包的时候,有时候会不行)
    an error occurred during the file system check错误的解决
    Linux使用tcpdump命令抓包保存pcap文件wireshark分析
    安装win7或win8系统时UEFI和Legacy模式的设置
    查看LINUX当前负载
    svn update 每更新一项就输出一行信息,使用首字符来报告执行的动作 这些字符的含义是:
    svn分支管理进行迭代开发
    Linux命令行下创建纳入版本控制下的新目录
    svn update -r m path 代码还原到某个版本(这样之前的log日志也就没了,也就是清空log日志)
    二进制日志BINARY LOG清理
  • 原文地址:https://www.cnblogs.com/Lam7/p/6264317.html
Copyright © 2011-2022 走看看