python模块介绍-locustio:性能测试工具locustio
轉自:http://automationtesting.sinaapp.com/blog/m_locustio_doc
?
python測試文章
http://weibo.com/cizhenshi?is_search=0&visible=0&is_tag=0&profile_ftype=1&page=2#feedtop
?
?
python模塊介紹-locustio:性能測試工具locustio中文文檔?
目錄
- python模塊介紹-locustio:性能測試工具locustio中文文檔
什么是locustio?
Locust是易于使用、分布式的用戶負載測試工具。用于網站(或其他系統)的負載測試,計算出系統可以處理并發用戶數。
測試時大量蝗蟲會攻擊你的網站。每只蝗蟲(或叫測試用戶)可以自定義、測試過程由web界面實時監控。這能幫助測試并確定瓶頸。
Locust 完全基于的事件,單機可以支持數千用戶。它不使用回調,而是基于輕量進程gevent, 能簡單地實線各種場景。
特點
- Python書寫場景
無需笨重的UI或XML。僅僅是代碼,協程而不是回調。
- 分布式,可擴展和,支持成千上萬的用戶
- 基于Web的用戶界面
Locust有整潔HTML + JS用戶界面,實時展示測試細節,跨平臺和易于擴展。
- 可以測試任何系統
- 可控制
事件完全由gevent處理。
背景
我們研究了現有的解決方案,都不符合要求。比如Apache JMeter和Tsung。JMeter基于UI操作,容易上手,但基本上不具備編程能力。其次JMeter基于線程,要模擬數千用戶幾乎不可能。Tsung基于Erlang,能模擬上千用戶并易于擴展,但它它基于XML的DSL,描述場景能力弱,且需要大量的數據處理才知道測試結果。
無論如何,我們試圖解決創建蝗蟲,當這些問題。希望以上都不是painpoints應該存在。
我想你可以說我們真的只是想在這里從頭開始自己的癢。我們希望其他人會發現,因為我們做的是有益的。
- 作者
- Jonatan Heyman (@jonatanheyman on Twitter)
- Carl Bystr?m (@cgbystrom on Twitter)
- Joakim Hamrén (@Jahaaja on Twitter)
- Hugo Heyman (@hugoheyman on Twitter)
- License: MIT
安裝
- 安裝:
"使用pip安裝gevent ,在cmd命令行里 pip install gevent。
安裝 gevent可能會遇到?GEVENT error:?Microsoft?Visual C 9.0 is required
先到http://www.microsoft.com/en-us/download/details.aspx?id=44266?下載安裝上,然后重新
pip install gevent
然后?pip install locustio,這樣基本安裝成功。
- 檢驗:
執行"locust --help"能看到如下信息表示安裝成功:
# locust --help Usage: locust [options] [LocustClass [LocustClass2 ... ]] Options: -h, --help show this help message and exit ...主要需要 Python 2.6+,不支持python3。分布式測試還需要安裝pyzmq。盡管locustio可以在Windows運行,但是考慮效率不推薦
快速入門
下面我們訪問http://automationtesting.sinaapp.com/的首頁和about頁面。
編寫腳本
#!/usr/bin/env python # coding=utf-8 """ Locust quickstart! Copyright 2015.05.08 Rongzhong.Xu xurongzhong#126.com http://automationtesting.sinaapp.com """ from locust import HttpLocust, TaskSet, task class UserBehavior(TaskSet): """ Locust test class """ def on_start(self): """ called when a Locust start before any task is scheduled """ self.login() def login(self): "pass" pass @task(2) def index(self): "http://automationtesting.sinaapp.com/" self.client.get("/") @task(1) def about(self): "http://automationtesting.sinaapp.com/about" self.client.get("/about") class WebsiteUser(HttpLocust): """ 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. """ task_set = UserBehavior min_wait = 5000 max_wait = 9000HttpLocust繼承自Locust,添加了client屬性。client屬性是HttpSession實例,可以用于生成HTTP請求。
on_start為client初始化時執行的步驟。
task表示下面方法是測試內容,里面的數字執行比例,這里about頁面占三分之一,主頁占三分之二。
task_set指定client執行的類。min_wait和max_wait為兩次執行之間的最小和最長等待時間。
啟動
- 啟動locust后臺
- 在瀏覽器啟動locust
打開?http://127.0.0.1:8089/,配置模擬的用戶數"Number of users to simulate"和每秒發起的用戶數"Hatch rate",提交執行測試。
這時在瀏覽器就可以看到實時的測試結果。點擊瀏覽器上方的"stop”即可停止測試。
- 查看報告:
命令行按Ctrl + c , 可以顯示一些摘要
# locust --host=http://automationtesting.sinaapp.com [2015-05-08 16:33:49,166] andrew-Hi-Fi-A88S2/INFO/locust.main: Starting web monitor at *:8089 [2015-05-08 16:33:49,167] andrew-Hi-Fi-A88S2/INFO/locust.main: Starting Locust 0.7.2 [2015-05-08 16:42:18,656] andrew-Hi-Fi-A88S2/INFO/locust.runners: Hatching and swarming 8 clients at the rate 2 clients/s... [2015-05-08 16:42:22,663] andrew-Hi-Fi-A88S2/INFO/locust.runners: All locusts hatched: WebsiteUser: 8 [2015-05-08 16:42:22,663] andrew-Hi-Fi-A88S2/INFO/locust.runners: Resetting stats ^C[2015-05-08 16:48:19,884] andrew-Hi-Fi-A88S2/ERROR/stderr: KeyboardInterrupt [2015-05-08 16:48:19,884] andrew-Hi-Fi-A88S2/INFO/locust.main: Shutting down (exit code 0), bye. Name # reqs # fails Avg Min Max | Median req/s -------------------------------------------------------------------------------------------------------------------------------------------- GET / 36 0(0.00%) 260 206 411 | 250 0.90 GET /about 17 0(0.00%) 199 146 519 | 170 0.10 -------------------------------------------------------------------------------------------------------------------------------------------- Total 53 0(0.00%) 1.00 Percentage of the requests completed within given times Name # reqs 50% 66% 75% 80% 90% 95% 98% 99% 100% -------------------------------------------------------------------------------------------------------------------------------------------- GET / 36 250 260 260 270 370 400 410 410 411 GET /about 17 170 180 180 200 290 520 520 520 519 --------------------------------------------------------------------------------------------------------------------------------------------網頁上可以下載csv文件,一個是調配記錄,一個是請求記錄。
# cat distribution_1431074713.45.csv "Name","# requests","50%","66%","75%","80%","90%","95%","98%","99%","100%" "GET /",36,250,260,260,270,370,400,410,410,411 "GET /about",17,170,180,180,200,290,520,520,520,519 "None Total",53,250,250,260,260,310,400,410,520,519 # cat requests_1431074710.05.csv "Method","Name","# requests","# failures","Median response time","Average response time","Min response time","Max response time","Average Content Size","Requests/s" "GET","/",36,0,250,260,206,411,9055,0.76 "GET","/about",17,0,170,199,146,519,4456,0.36 "None","Total",53,0,250,241,146,519,7579,1.12- 其他:
指定測試文件啟動:locust -f ../locust_files/my_locust_file.py --host=?http://example.com
分布式測試時作為主測試機:locust -f ../locust_files/my_locust_file.py --master --host=?http://example.com
分布式測試時作為從測試機:locust -f ../locust_files/my_locust_file.py --slave --master-host=192.168.0.100 --host=?http://example.com。master-host的默認值是127.0.0.1。
locustfile
locustfile需要定義至少一個locust類。
Locust類
locust類代表用戶。屬性如下:
- task_set屬性
task_set屬性指向定義了用戶的行為的TaskSet類。
- min_wait和max_wait屬性
兩次執行之間的最小和最長等待時間,單位:毫秒,即執行各任務之間等待時間。默認為1000,并且因此蝗蟲永遠等待1秒各任務之間如果min_wait和MAX_WAIT未聲明。
用下面locustfile,每個用戶將等待任務之間5到15秒:
- weight屬性
可以同時執行同一文件的多個locust:
# locust -f locust_file.py WebUserLocust MobileUserLocustweight標識執行比例,這里WebUserLocust的執行次數3倍于MobileUserLocust:
class WebUserLocust(Locust):weight = 3 .... class MobileUserLocust(Locust): weight = 1 ....- host屬性
URL的前綴(如"http://automationtesting.sinaapp.com”)。通常在命令行使用--host選項指定。也可以設置host屬性,在命令行沒有指定時作為默認值。
TaskSet類
TaskSet表示任務的集合。任務可以嵌套,比如:
- Main user behaviour
- Index page
- Forum page
- Read thread
- Reply
- New thread
- View next page
- Read thread
- Browse categories
- Watch movie
- Filter movies
- About page
嵌套的python代碼示例:
class ForumPage(TaskSet):@task(20) def read_thread(self): pass @task(1) def new_thread(self): pass @task(5) def stop(self): self.interrupt() class UserBehaviour(TaskSet): tasks = {ForumPage:10} @task def index(self): pass類嵌套示例:
class MyTaskSet(TaskSet):@taskclass SubTaskSet(TaskSet): @task def my_task(self): passHTTP請求
HttpLocust繼承自Locust,添加了client屬性。client屬性是HttpSession實例,調用了requests,可以用于生成HTTP請求。 self.client設置了屬性指向 self.locust.client。
get 和post示例:
# get response = self.client.get("/about") print "Response status code:", response.status_code print "Response content:", response.content # post response = self.client.post("/login", {"username":"testuser", "password":"secret"})注意失敗的任何請求如連接錯誤,超時等不產生異常,而是返回None,status_code是0。
默認只要返回的不是2都是失敗。也可以設置失敗和成功:
with client.get("/", catch_response=True) as response: if response.content != "Success": response.failure("Got wrong response") with client.get("/does_not_exist/", catch_response=True) as response: if response.status_code == 404: response.success()動態參數:
# Statistics for these requests will be grouped under: /blog/?id=[id] for i in range(10): client.get("/blog?id=%i" % i, name="/blog?id=[id]")分布式測試
主機使用--master,它不會模擬任何用戶。實際測試機需要--slave和--master-host參數。通常一個cpu核可以執行一個從機。
master的參數還有: "--master-bind-host=X.X.X.X"和--master-bind-host=X.X.X.X。
從機的參數有:"--master-port=5557"。
本文地址
- http://automationtesting.sinaapp.com/blog/m_locustio_doc
- 本站地址:python自動化測試http://automationtesting.sinaapp.com?python開發自動化測試群113938272和軟件測試進階群6089740 微博?http://weibo.com/cizhenshi
參考資料
- ?locustio英文文檔
總結
以上是生活随笔為你收集整理的python模块介绍-locustio:性能测试工具locustio的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx的https设置
- 下一篇: equals()方法详解