python模块介绍-locustio:性能测试工具locustio
轉(zhuǎn)自:http://automationtesting.sinaapp.com/blog/m_locustio_doc
?
python測(cè)試文章
http://weibo.com/cizhenshi?is_search=0&visible=0&is_tag=0&profile_ftype=1&page=2#feedtop
?
?
python模塊介紹-locustio:性能測(cè)試工具locustio中文文檔?
目錄
- python模塊介紹-locustio:性能測(cè)試工具locustio中文文檔
什么是locustio?
Locust是易于使用、分布式的用戶負(fù)載測(cè)試工具。用于網(wǎng)站(或其他系統(tǒng))的負(fù)載測(cè)試,計(jì)算出系統(tǒng)可以處理并發(fā)用戶數(shù)。
測(cè)試時(shí)大量蝗蟲(chóng)會(huì)攻擊你的網(wǎng)站。每只蝗蟲(chóng)(或叫測(cè)試用戶)可以自定義、測(cè)試過(guò)程由web界面實(shí)時(shí)監(jiān)控。這能幫助測(cè)試并確定瓶頸。
Locust 完全基于的事件,單機(jī)可以支持?jǐn)?shù)千用戶。它不使用回調(diào),而是基于輕量進(jìn)程gevent, 能簡(jiǎn)單地實(shí)線各種場(chǎng)景。
特點(diǎn)
- Python書(shū)寫(xiě)場(chǎng)景
無(wú)需笨重的UI或XML。僅僅是代碼,協(xié)程而不是回調(diào)。
- 分布式,可擴(kuò)展和,支持成千上萬(wàn)的用戶
- 基于Web的用戶界面
Locust有整潔HTML + JS用戶界面,實(shí)時(shí)展示測(cè)試細(xì)節(jié),跨平臺(tái)和易于擴(kuò)展。
- 可以測(cè)試任何系統(tǒng)
- 可控制
事件完全由gevent處理。
背景
我們研究了現(xiàn)有的解決方案,都不符合要求。比如Apache JMeter和Tsung。JMeter基于UI操作,容易上手,但基本上不具備編程能力。其次JMeter基于線程,要模擬數(shù)千用戶幾乎不可能。Tsung基于Erlang,能模擬上千用戶并易于擴(kuò)展,但它它基于XML的DSL,描述場(chǎng)景能力弱,且需要大量的數(shù)據(jù)處理才知道測(cè)試結(jié)果。
無(wú)論如何,我們?cè)噲D解決創(chuàng)建蝗蟲(chóng),當(dāng)這些問(wèn)題。希望以上都不是painpoints應(yīng)該存在。
我想你可以說(shuō)我們真的只是想在這里從頭開(kāi)始自己的癢。我們希望其他人會(huì)發(fā)現(xiàn),因?yàn)槲覀冏龅氖怯幸娴摹?/p>
- 作者
- 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可能會(huì)遇到?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,這樣基本安裝成功。
- 檢驗(yàn):
執(zhí)行"locust --help"能看到如下信息表示安裝成功:
# locust --help Usage: locust [options] [LocustClass [LocustClass2 ... ]] Options: -h, --help show this help message and exit ...主要需要 Python 2.6+,不支持python3。分布式測(cè)試還需要安裝pyzmq。盡管locustio可以在Windows運(yùn)行,但是考慮效率不推薦
快速入門(mén)
下面我們?cè)L問(wèn)http://automationtesting.sinaapp.com/的首頁(yè)和about頁(yè)面。
編寫(xiě)腳本
#!/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實(shí)例,可以用于生成HTTP請(qǐng)求。
on_start為client初始化時(shí)執(zhí)行的步驟。
task表示下面方法是測(cè)試內(nèi)容,里面的數(shù)字執(zhí)行比例,這里about頁(yè)面占三分之一,主頁(yè)占三分之二。
task_set指定client執(zhí)行的類(lèi)。min_wait和max_wait為兩次執(zhí)行之間的最小和最長(zhǎng)等待時(shí)間。
啟動(dòng)
- 啟動(dòng)locust后臺(tái)
- 在瀏覽器啟動(dòng)locust
打開(kāi)?http://127.0.0.1:8089/,配置模擬的用戶數(shù)"Number of users to simulate"和每秒發(fā)起的用戶數(shù)"Hatch rate",提交執(zhí)行測(cè)試。
這時(shí)在瀏覽器就可以看到實(shí)時(shí)的測(cè)試結(jié)果。點(diǎn)擊瀏覽器上方的"stop”即可停止測(cè)試。
- 查看報(bào)告:
命令行按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 --------------------------------------------------------------------------------------------------------------------------------------------網(wǎng)頁(yè)上可以下載csv文件,一個(gè)是調(diào)配記錄,一個(gè)是請(qǐng)求記錄。
# 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- 其他:
指定測(cè)試文件啟動(dòng):locust -f ../locust_files/my_locust_file.py --host=?http://example.com
分布式測(cè)試時(shí)作為主測(cè)試機(jī):locust -f ../locust_files/my_locust_file.py --master --host=?http://example.com
分布式測(cè)試時(shí)作為從測(cè)試機(jī):locust -f ../locust_files/my_locust_file.py --slave --master-host=192.168.0.100 --host=?http://example.com。master-host的默認(rèn)值是127.0.0.1。
locustfile
locustfile需要定義至少一個(gè)locust類(lèi)。
Locust類(lèi)
locust類(lèi)代表用戶。屬性如下:
- task_set屬性
task_set屬性指向定義了用戶的行為的TaskSet類(lèi)。
- min_wait和max_wait屬性
兩次執(zhí)行之間的最小和最長(zhǎng)等待時(shí)間,單位:毫秒,即執(zhí)行各任務(wù)之間等待時(shí)間。默認(rèn)為1000,并且因此蝗蟲(chóng)永遠(yuǎn)等待1秒各任務(wù)之間如果min_wait和MAX_WAIT未聲明。
用下面locustfile,每個(gè)用戶將等待任務(wù)之間5到15秒:
- weight屬性
可以同時(shí)執(zhí)行同一文件的多個(gè)locust:
# locust -f locust_file.py WebUserLocust MobileUserLocustweight標(biāo)識(shí)執(zhí)行比例,這里WebUserLocust的執(zhí)行次數(shù)3倍于MobileUserLocust:
class WebUserLocust(Locust):weight = 3 .... class MobileUserLocust(Locust): weight = 1 ....- host屬性
URL的前綴(如"http://automationtesting.sinaapp.com”)。通常在命令行使用--host選項(xiàng)指定。也可以設(shè)置host屬性,在命令行沒(méi)有指定時(shí)作為默認(rèn)值。
TaskSet類(lèi)
TaskSet表示任務(wù)的集合。任務(wù)可以嵌套,比如:
- 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類(lèi)嵌套示例:
class MyTaskSet(TaskSet):@taskclass SubTaskSet(TaskSet): @task def my_task(self): passHTTP請(qǐng)求
HttpLocust繼承自Locust,添加了client屬性。client屬性是HttpSession實(shí)例,調(diào)用了requests,可以用于生成HTTP請(qǐng)求。 self.client設(shè)置了屬性指向 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"})注意失敗的任何請(qǐng)求如連接錯(cuò)誤,超時(shí)等不產(chǎn)生異常,而是返回None,status_code是0。
默認(rèn)只要返回的不是2都是失敗。也可以設(shè)置失敗和成功:
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()動(dòng)態(tài)參數(shù):
# 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]")分布式測(cè)試
主機(jī)使用--master,它不會(huì)模擬任何用戶。實(shí)際測(cè)試機(jī)需要--slave和--master-host參數(shù)。通常一個(gè)cpu核可以執(zhí)行一個(gè)從機(jī)。
master的參數(shù)還有: "--master-bind-host=X.X.X.X"和--master-bind-host=X.X.X.X。
從機(jī)的參數(shù)有:"--master-port=5557"。
本文地址
- http://automationtesting.sinaapp.com/blog/m_locustio_doc
- 本站地址:python自動(dòng)化測(cè)試http://automationtesting.sinaapp.com?python開(kāi)發(fā)自動(dòng)化測(cè)試群113938272和軟件測(cè)試進(jìn)階群6089740 微博?http://weibo.com/cizhenshi
參考資料
- ?locustio英文文檔
總結(jié)
以上是生活随笔為你收集整理的python模块介绍-locustio:性能测试工具locustio的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Nginx的https设置
- 下一篇: 运维:windows+python+ro