locust性能

一杨何、安裝Locust

安裝Locust之前先安裝的庫:

gevent庫:第三方庫垫言,gevent為python提供了比較完善的協(xié)程支持堕伪。使用gevent,可以獲得極高的并發(fā)性能纺弊。

pip install gevent==1.1.2

flask庫:Flask是一個使用 Python 編寫的輕量級 Web 應用框架圆恤。

pip install flask==0.10.1

requests庫:用python寫過接口測試的朋友應該都不陌生,Requests 是用Python語言編寫队伟,基于 urllib穴吹,采用 Apache2 Licensed 開源協(xié)議的 HTTP 庫。它比 urllib 更加方便嗜侮,可以節(jié)約我們大量的工作港令,完全滿足 HTTP 測試需求。

pip install requests==2.10.0

msgpack-python庫:比JSON快10倍的序列化包锈颗。

pip install msgpack-python==0.4.2

six庫:six 提供了一些簡單的工具用來封裝 Python 2 和 Python 3 之間的差異性顷霹。

pip install six==1.10.0

pyzmq庫:Pyzmq是zeromq的Python綁定。

pip install pyzmq==15.2.0

安裝庫的過程中可能會遇到一些報錯信息:

ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out.

解決方案:

更新pip版本到pip官網(wǎng)下載:https://pypi.python.org/simple/pip/

比如下載pip-9.0.1-py2.py3-none-any.whl

pip install pip-9.0.1-py2.py3-none-any.whl

如果還報錯再執(zhí)行:

pip --default-timeout=100 install -U pip

其他第三方庫都安裝完成后击吱,接下來再安裝locust:

pip install locustio

安裝locustio庫時可能會報錯:

error:command 'gcc' failed with exit status 1

解決方案:yum install gcc python-devel

安裝完成后淋淀,執(zhí)行:locust--help

看到如下locust相關(guān)命令介紹,說明安裝成功覆醇。

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

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

二朵纷、使用Locust測試淘寶首頁

#locust_taobao.py

#!/usr/bin/env python

#-*-coding:utf-8-*-

#author:@TT

from locust import HttpLocust, TaskSet, task

#定義用戶行為

class UserBehavior(TaskSet):

@task

def taobao_page(self): #定義一個方法訪問淘寶首頁

self.client.get("/")

class WebsiteUser(HttpLocust):

task_set = UserBehavior

min_wait = 3000 #用戶等待時間下限

max_wait = 5000 #用戶等待時間上限

執(zhí)行:

locust -f locust_taobao.py --host=https://www.taobao.com

提示:Starting web monitor at *.8089

Starting Locust 0.7.5

說明已經(jīng)啟動成功炭臭,瀏覽器輸入:http://120.76.139.13:8089即可看到設(shè)置頁面,并監(jiān)控

Number of users to simulate:設(shè)置需要模擬的用戶數(shù)量,這次只是為了演示袍辞,設(shè)置為10鞋仍;

Hatch rate:每秒需要啟動的用戶數(shù)量,這次只是為了演示搅吁,設(shè)置為2威创;

點擊『start swarming』,開始了性能測試:

Type:請求的類型谎懦;

Name:請求的url或者自定義的統(tǒng)計分組名字肚豺;

requests:當前請求的數(shù)量;

fails:當前請求失敗的數(shù)量界拦;

Median:中間值吸申,一半的服務器響應時間高于該值,而另一半的服務器響應時間低于該值(毫秒)寞奸;

Average:所有請求的平均響應時間(毫秒)呛谜;

Min:請求最小響應時間(毫秒)在跳;

Max:請求最大響應時間(毫秒)枪萄;

Content Size:單個請求的大小(字節(jié))猫妙;

reqs/sec:每秒請求的個數(shù)瓷翻;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市割坠,隨后出現(xiàn)的幾起案子齐帚,更是在濱河造成了極大的恐慌,老刑警劉巖彼哼,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件对妄,死亡現(xiàn)場離奇詭異,居然都是意外死亡敢朱,警方通過查閱死者的電腦和手機剪菱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拴签,“玉大人孝常,你說我怎么就攤上這事◎玖ǎ” “怎么了构灸?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長岸梨。 經(jīng)常有香客問我喜颁,道長稠氮,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任半开,我火速辦了婚禮括袒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘稿茉。我一直安慰自己锹锰,他們只是感情好,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布漓库。 她就那樣靜靜地躺著恃慧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪渺蒿。 梳的紋絲不亂的頭發(fā)上痢士,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音茂装,去河邊找鬼怠蹂。 笑死,一個胖子當著我的面吹牛少态,可吹牛的內(nèi)容都是我干的城侧。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼彼妻,長吁一口氣:“原來是場噩夢啊……” “哼嫌佑!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起侨歉,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤屋摇,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后幽邓,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體炮温,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年牵舵,在試婚紗的時候發(fā)現(xiàn)自己被綠了柒啤。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡棋枕,死狀恐怖白修,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情重斑,我是刑警寧澤兵睛,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響祖很,放射性物質(zhì)發(fā)生泄漏笛丙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一假颇、第九天 我趴在偏房一處隱蔽的房頂上張望胚鸯。 院中可真熱鬧,春花似錦笨鸡、人聲如沸姜钳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽哥桥。三九已至,卻和暖如春激涤,著一層夾襖步出監(jiān)牢的瞬間拟糕,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工倦踢, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留送滞,地道東北人。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓辱挥,卻偏偏與公主長得像犁嗅,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子般贼,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容