1.目的
WEB负载测试(Load testing)可用于评估WEB系统的最大可服务容量,同时识别系统中潜在的性能瓶颈,
提高系统在正常以及接近极限访问量下的可靠性。
负载测试中,当负载不断增加,超过被测系统的容量上限时,此时变成了压力测试(stress testing),被测试系统一般会逐渐服务不正常,但是系统行为需要在预期的范围,待压力测试持续一段时间后(如1小时),负载变轻时,系统应能能自动恢复正常(不需要人工干预)。
负载测试技术指标
Requests per second:
每秒能支持的请求数Concurrent users:
能支持的用户并发请求数(同时发送请求的用户数)Response time per request:
单次请求的响应时间(包括平均,最大,标准差)Throughput:
吞吐量,表示每秒能支持的数据下载访问量,侧重衡量网络性能Availability:
可用性,一段时间的请求成功数/总请求数。
tools
1.Locust
What is Locust:
Locust is an easy-to-use,distributed, user load testing tool. It s is intended for load-testing web sites(or other systems) and figuring out how many concurrent users a
system can handle.The idea is that during a test, a swarm of locusts will attack your websiute. The behavior of each locust 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 bottllenecks in your code before letting real users in.
Links
- github: https://github.com/locustio/locust
- Website: http://locust.io/
- Documentation: http://docs.locust.io/en/latest/api.html
- Support/Questions: https://slack.locust.io/
How to use?
No need for clunky UIs or bloated XML - just code as you normally would(python programming). Based of coroutines instead of callbacks, your code looks and behaves like normal ,blocking python code.
Demo1:Hello world
#demo.py
from locust import Locust,TaskSet, task
class MyTaskSet(TaskSet):
@task
def my_task(self):
print "executing my task"
class MyLocust(Locust):
task_set = MyTaskSet
min_wait = 5000
max_wait = 12000
How to start?
when in linux terminal,input:
locust -f demo.py –host = http://www.baidu.com (or other url)
When you have started Locust using the above command liens, you should open up a browser and point to http://127.0.0.1:8089(if Locust in local).Then you should filled 2 things: Number of users to simulate and Hatch rate.
Demo2:from official website(login before hack)
#locustfile.py
from locust import HttpLocust, TaskSet
def login(l):
l.client.post("/login", {"username":"ellen_key", "password":"education"})
def index(l):
l.client.get("/")
def profile(l):
l.client.get("/profile")
class UserBehavior(TaskSet):
tasks = {index:2, profile:1}
def on_start(self):
login(self)
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 5000
max_wait = 10000
locust -f locustfile.py –host = http://192.168.1.4:8000
demo3:api test
#fil3.py
from locust import HttpLocust, TaskSet
headers= {
'Cookie':'csrftoken=Ksrre3kIVCsQXv9g1II8Yt2grxj3C9KS; sessionid=aofv01n4v5piskrzm78dppj3erws2bn2'
}
def api1(l):
#print l.client.get('/api/v1/platform/strategy/live',headers=headers).json()
print l.client.get('/api/v1/platform/strategy/live',headers=headers).json()['code']
def api2(l):
print l.client.get('/api/v1/platform/strategy/account',headers=headers).json()['code']
class UserBehavior(TaskSet):
tasks = {api1:2,api2:1}
def on_start(self):
print "begin"
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait=1000
max_wait=2000
locust -f fil3.py –host:http://192.168.1.5
2.Apache Bench
ab
可以使用常用的Apache Bench.
$ab -n 100 -c 10 -l http://192.168.1.180/
The most common:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-l Accept variable document length (use this for dynamic pages)
ab可以执行一些日常的负载测试,具体用法可参考ab的文档(man ab)。
siege
siege能比ab更真实的模拟实际的用户访问,且可以同时进行多个网页/网站的负载测试。
使用sproxy记录访问过程
为了模拟真实的用户访问过程,可以使用sproxy记录用户访问过程:
- 下载(sproxy)[http://download.joedog.org]作为代理服务器,
- 在浏览器上将代理服务器设置为sproxy。
- 模拟一个用户典型的访问流程。
- 从代理服务器sproxy上获得整个访问过程的URL记录。
$ sproxy -h
sproxy v1.02-March-20-2017
Usage: sproxy [options] [hostname]
(If a hostname is not specified, then sproxy will bind
to localhost [127.0.0.1])
Options:
-V VERSION, prints version number to screen.
-v Verbose, prints URLS to screen.
-h Help, prints this section.
-t NUM Timeout, set the sproxy connection timeout to NUMM (default 120)
-p NUM Port, specify the port on which sproxy will listen for
incoming connections (default 9001)
-f FILE File, specify an alternative configuration file.
(default $prefix/etc/sproxy.conf)
-o FILE Output file, specify an alternative file to write URLs.
(default $HOME/urls.txt)
$sproxy -p 9001 -o ./1.url
收集到的ruls访问记录包括:
$cat ./1.url
http://192.168.1.180/js/vendor/vue.min.js
http://192.168.1.180/js/vendor/echarts.min.js
http://192.168.1.180/js/my.bundle.js
http://192.168.1.180/js/strategy-upload.bundle.js
http://192.168.1.180/api/v1/user
http://192.168.1.180/api/v1/strategy_upload/strategy
http://192.168.1.180/api/v1/public/product
http://192.168.1.180:80
http://192.168.1.180/api/v1/strategy_upload/dependency
http://192.168.1.180/api/v1/strategy_upload/strategy POST ------WebKitFormBoundarywEDbBF...
http://192.168.1.180/api/v1/strategy_upload/strategy/100
http://192.168.1.180/api/v1/strategy_upload/strategy POST ------WebKitFormBoundaryMXmAkp...
为了测试真实的WEB服务进程压力,可以将sproxy收集到的url中的静态文件(nginx配置中静态文件)的urls去掉,
因为这部分的访问压力由Nginx承担,没有转发到后台的WEB服务进程。
配置siege进行测试
1.下载(siege)[http://download.joedog.org]
2. 使用记录的url列表进行测试:
$siege -f ./1.url -c 100 -r 10
可模拟GET/POST等请求
其它:
* 登陆和授权处理可参考:https://www.joedog.org/2012/02/16/http-authentication/
wrk
(wrk)[https://github.com/wg/wrk] 相比ab和siege是一个比较新的HTTP测试工具。
$wrk -t12 -c400 -d30s http://127.0.0.1:8080/index.html
其它
参考:
- en.wikipedia.org/wiki/Load_testing
- https://www.joedog.org/sproxy-manual/