高併發壓力測試工具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.php

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.node

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.python

蝗蟲是什麼?ios

蝗蟲是一個易於使用、分發、用戶負載測試工具。它是用於負載測試的網站(或其餘系統),弄清楚有多少併發用戶系統能夠處理。web

咱們的想法是,在測試期間,一羣蝗蟲會攻擊你的網站。每一個蝗蟲的行爲(或測試用戶若是你願意)被定義爲你和彙集過程實時監控從web UI。這將幫助你戰鬥測試和代碼中識別瓶頸以前讓真正的用戶。shell

蝗蟲是徹底基於事件,所以能夠支持在單個機器上成千上萬的併發用戶。與許多其餘的基於事件的應用程序不使用回調。相反,它使用輕量級進程,經過gevent。每一個蝗蟲爬你的網站其實是運行在它本身的進程(或一種綠色小鳥,是正確的)。這容許您編寫很是富有表現力的場景在Python中沒有複雜的代碼和回調。express

Installation

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

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/
相關文章
相關標籤/搜索