性能工具之 Locust 簡單入門

      網友投稿 783 2025-03-31

      前言

      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.

      Locust是一個易于使用,分布式,用戶負載測試工具。它用于負載測試web站點(或其他系統)并計算一個系統可以處理多少并發用戶。

      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.

      在測試中,一群蝗蟲會攻擊你的網站。每個蝗蟲(或者測試用戶)的行為由您定義,集群過程由web UI實時監控。這將幫助您在讓真正的用戶進入之前進行測試并識別代碼中的瓶頸。

      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.

      Locust完全是基于事件的,因此在一臺機器上支持數千個并發用戶是可能的。與許多其他基于事件的應用程序不同,它不使用回調。相反,它通過gevent使用輕量級進程。每個聚集在你的站點上的蝗蟲實際上是在它自己的進程中運行的(或者說是greenlet)。這允許您用Python編寫非常有表現力的場景,而不用回調使代碼復雜化。

      幫助文檔:https://docs.locust.io/en/stable/what-is-locust.html

      快速安裝

      pip install locustio

      主要命令

      locust --help Usage: locust [options] [LocustClass [LocustClass2 ... ]] 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 --csv=CSVFILEBASE, --csv-base-name=CSVFILEBASE Store current request stats to files in CSV format. --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. --expect-slaves=EXPECT_SLAVES How many slaves master should expect to connect before starting the test (only when --no-web used). --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 Locust users. 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 -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 --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 --no-reset-stats [DEPRECATED] Do not reset statistics once hatching has been completed. This is now the default behavior. See --reset-stats to disable --reset-stats Reset statistics once hatching has been completed. Should be set on both master and slaves when running in distributed mode -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

      快速上手

      第一步,編寫一盒簡單的腳本。

      # -*- coding: utf-8 -*- # @Time : 2019/12/31 20:24 # @Author : 7DGroup # @File : SevenLoust.py # coding:utf-8 from locust import HttpLocust, TaskSet, task # HttpLocust 這個類的作用是用來發送http請求的 # TaskSet 這個類是定義用戶行為的,相當于loadrunnerhttp協議的腳本,jmeter里面的http請求一樣,要去干嘛的 # task 這個task是一個裝飾器,它用來把一個函數,裝飾成一個任務,也可以指定他們的先后執行順序 class SevenLoust(TaskSet): # 自己定義的類,繼承TaskSet,也就是這個類是實現咱們要去請求什么的 '''打開7DGroup''' @task(1) # @task#用task裝飾器把這個函數裝飾成一個咱們要執行的性能任務 def open_7dtest(self): # 這個函數里面定義的是咱們要具體做的操作 # 定義requests的請求頭 header = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"} r = self.client.get("/", headers=header, verify=False) # 請求這個url里面的哪個路徑, print(r.status_code) assert r.status_code == 200 class websitUser(HttpLocust): # 這個類繼承了HttpLocust,代表每個并發里面的每個用戶 task_set = SevenLoust # 這個是每個用戶都去干什么,指定了 SevenLoust 這個類, min_wait = 3000 # 單位毫秒 max_wait = 6000 # 單位毫秒 if __name__ == "__main__": import os # -f是指定一個python文件 后面跟上咱們剛才寫的python文件 # --host是你要訪問哪個網站,后面跟網站的url os.system("locust -f sevenLoust.py --host=http://www.7dtest.com/7DGroup/")

      點擊啟動

      打開瀏覽器顯示:

      參數說明:

      Number of users to simulate 設置虛擬用戶總數

      性能工具之 Locust 簡單入門

      Hatch rate (users spawned/second) 每秒啟動虛擬用戶數

      第二步,點擊 Start swarming開始運行性能測試。

      主要參數說明:

      Type:請求類型;

      Name:請求路徑;

      requests:當前請求的數量;

      fails:當前請求失敗的數量;

      Median:中間值,單位毫秒,一般服務器響應時間低于該值,而另一半高于該值;

      Average:所有請求的平均響應時間,毫秒;

      Min:請求的最小的服務器響應時間,毫秒;

      Max:請求的最大服務器響應時間,毫秒;

      Content Size:單個請求的大小,單位字節;

      reqs/sec:每秒鐘請求的個數。

      第三步,查看報告顯示。

      吞吐量/每秒響應事務數(rps)實時統計

      平均響應時間/平均事務數實時統計

      虛擬用戶數運行

      第四步,停止點擊按鈕結束測試

      總結

      以上是簡單上手示例,只要會點 Python 基礎,就可以快速入門。

      Python 壓力測試 網站

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:excel替換空格的方法教程詳解
      下一篇:MySQL 高頻面試題,都在這了
      相關文章
      亚洲午夜久久久精品电影院| 久久精品蜜芽亚洲国产AV| 亚洲高清资源在线观看| 亚洲卡一卡2卡三卡4卡无卡三| 亚洲精品无码高潮喷水在线| 亚洲午夜久久久久久久久久| 中文字幕无码精品亚洲资源网| 亚洲国产专区一区| 亚洲人成无码www久久久| 亚洲精品国产日韩无码AV永久免费网 | 亚洲看片无码在线视频| 国产色在线|亚洲| 麻豆狠色伊人亚洲综合网站| 最新国产成人亚洲精品影院| 亚洲五月综合缴情婷婷| 亚洲午夜无码久久久久软件| 亚洲愉拍一区二区三区| 亚洲精品伦理熟女国产一区二区 | 亚洲中文字幕无码中文| 亚洲欧洲精品成人久久曰| 亚洲乱人伦中文字幕无码| 久久精品国产亚洲av品善| 最新亚洲人成无码网站| 亚洲另类激情专区小说图片| 一本色道久久综合亚洲精品高清| 亚洲中文字幕在线乱码| 亚洲s色大片在线观看| 亚洲邪恶天堂影院在线观看| 亚洲国产人成在线观看| 亚洲日韩国产精品乱-久| 亚洲av无码偷拍在线观看| 国产亚洲情侣久久精品| 久久久青草青青国产亚洲免观| 国产午夜亚洲精品国产成人小说| 国产亚洲色视频在线| 久久国产亚洲电影天堂| 亚洲第一精品电影网| 亚洲欧美一区二区三区日产| 国产亚洲精品精品精品| 中文字幕精品亚洲无线码二区| 亚洲AV中文无码字幕色三|