Locust Performance-Zero Foundation Entry Series (17)-Simplified Locust Test Process

When running the Locust test before, the general steps are as follows:

  • Create a Locust script (locust file) to simulate a good scene

  • Run the Locust script on the command line as follows:
locust -f locust_file.py
  • Then open the web UI and set the test parameters on the UI, such as the number of users, ramp up rate, etc.
    As you will find above, the overall steps are still a bit cumbersome. In fact, Locust provides a more concise method than the above method, which can concentrate the configuration and operation related to the performance test in the script. Specifically, you can refer to the following script (Locust version 1.1.1):
import gevent
from locust import HttpUser,task,between
from locust.env import Environment
from locust.stats import stats_printer
from locust.log import setup_logging

setup_logging("INFO",None)

class WebUser(HttpUser):
    wait_time = between(1,5)
    host = 'https://blog.51cto.com'
    @task
    def open_blog1(self):
        with self.client.get('/13734261/2538770',catch_response=True) as res:
            if res.status_code == 200:
                res.success()

    @task
    def open_blog2(self):
        with self.client.get('/13734261/2538745',catch_response=True) as res:
            if res.status_code == 200:
                res.success()

#创建环境和runner
env = Environment(user_classes=[WebUser,])
env.create_local_runner()

#start a WebUI instance
env.create_web_ui("127.0.0.1",8001)

#开启了协程,会定期打印runner状态
gevent.spawn(stats_printer(env.stats))

#开启测试,开启用户数为1
env.runner.start(1,hatch_rate=10)

# 设置100秒后,然后停止测试
gevent.spawn_later(100,lambda: env.runner.quit())
#等待greenlet结束
env.runner.greenlet.join()
#结束locust webUI
env.web_ui.stop()

The test output results are as follows. During the test, such test result tables will also be output regularly, usually every few seconds.

Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
 GET /13734261/2538745                                             13     0(0.00%)     300     183     542  |     280    0.10    0.00
 GET /13734261/2538770                                             17     0(0.00%)     376     199     931  |     270    0.20    0.00
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                        30     0(0.00%)     343     183     931  |     270    0.30    0.00

For such a test step improvement, the following steps can be omitted:

  • Run the test script on the command line

  • Set test parameters on the webUI in the browser and monitor the test running process.

Locust Performance-Zero Foundation Entry Series (17)-Simplified Locust Test Process

Guess you like

Origin blog.51cto.com/13734261/2571599