一、前言:
Siege是一个压力测试和评测工具,设计用于WEB开发这评估应用在压力下的承受能力:可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进行。(注意:Siege默认不支持https访问)
官方解释:【谷歌翻译过来的】
Siege 是一个 http 负载测试和基准测试工具。它旨在让 Web 开发人员在胁迫下测量他们的代码,以查看它如何在互联网上加载。Siege 支持基本身份验证、cookie、HTTP、HTTPS 和 FTP 协议。它允许用户访问具有可配置数量的模拟客户端的服务器。这些客户端将服务器置于“围攻之下”。
如果要支持https
需要安装openssl cetos 安装: 参考这个:http://mrdede.com/?p=3410 但是我服务器安装之后,可以直接测试百度https的。
yum install openssl
git clone https://github.com/openssl/openssl
cd openssl
./config --prefix=/usr/local/openssl
make
make install
openssl version
二、安装使用:
文件备份:
https://wwa.lanzoui.com/b00v263be 密码:d75z
1、mac安装:
直接:
brew install siege
2、linux 安装:【centos 服务器】
想着mac可以直接brew安装,centos应该可以yum安装,结果还真的安装成功了。
yum install siege
通用Linux安装:
wget http://download.joedog.org/siege/siege-latest.tar.gz
tar zxvf siege-4.1.1.tar.gz
cd siege-4.1.1
make clean
./configure --prefix=/usr/local/siege --with-ssl=/usr/local/openssl
make
make install
3、window安装:
下周地址:官方未提供window版本。 去GitHub下周window版本:
下再之后,解压,把siege-windows目录移动到c磁盘. C:\siege-windows。并吧C:\siege-windows目录放到系统环境path里面。
打开cmd就可以使用了。
4、测试百度:
参数:
siege -h
参数:
参数 描述 -V, –version 打印版本号 -h, –help 打印帮助信息 -C, –config 打印当前配置信息 -g, –get 拉取http头信息 -p, –print 打印整个页面的内容 -c, –concurrent=NUM 并发用户数量,默认10个 -r, –reps=NUM 运行次数 -d, –delay=NUM 随机时间延迟(秒) -b, –benchmark 请求没有延迟 -i, –internet 模拟网络用户随机点击URL -f, –file=FILE 选择指定的URL文件 -R, –rc=FILE 指定siegerc文件 -l, –log[=FILE] 日志文件,默认是 PREFIX/var/siege.log -H, –header=”text” 给请求添加头,支持多个 -A, –user-agent=”text” 给请求设置User-Agent -T, –content-type=”text” 给请求设置Content-Type
测试百度:
我这里设置3个并发,遍历3次,太多担心ip被封,无法测试。 一般可以使自己搭建web接口测试使用。
siege -c 3 -r 3 -b https://www.baidu.com/
最后返回结果:
Transactions: 270 hits Availability: 100.00 % Elapsed time: 5.26 secs Data transferred: 4.01 MB Response time: 0.06 secs Transaction rate: 51.33 trans/sec Throughput: 0.76 MB/sec Concurrency: 2.95 Successful transactions: 270 Failed transactions: 0 Longest transaction: 0.90 Shortest transaction: 0.02
5、查看配置文件位置:
1)、siege -C
CURRENT SIEGE CONFIGURATION
Mozilla/5.0 (apple-x86_64-darwin20.4.0) Siege/4.1.1
Edit the resource file to change the settings.
----------------------------------------------
version: 4.1.1
verbose: true
color: true
quiet: false
debug: false
protocol: HTTP/1.1
HTML parser: enabled
get method: HEAD
connection: close
concurrent users: 25
time to run: n/a
repetitions: n/a
socket timeout: 30
cache enabled: false
accept-encoding: gzip, deflate
delay: 0.000 sec
internet simulation: false
benchmark mode: false
failures until abort: 1024
named URL: none
URLs file: /usr/local/Cellar/siege/4.1.1/etc/urls.txt
thread limit: 2550
logging: false
log file: /usr/local/var/log/siege.log
resource file: /Users/zjf228/.siege/siege.conf
timestamped output: false
comma separated output: false
allow redirects: true
allow zero byte data: true
allow chunked encoding: true
upload unique files: true
json output: false
no-follow:
- ad.doubleclick.net
- pagead2.googlesyndication.com
- ads.pubsqrd.com
- ib.adnxs.com
proxy auth:
www auth:
2)、找到后缀:siege.conf
配置文件就在这里面。我的是mac电脑。
/Users/zjf228/.siege/siege.conf
三、flask 四种wsgi方式对比(tornado,Gunicorn,Twisted,Gevent)
参考:https://blog.csdn.net/marscrazy_90/article/details/41943211
最后找到这一篇文章: https://www.jianshu.com/p/d6961f713e46
1、文件:
1)、代码结构:
2)、hello.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/<name>')
def index(name=None):
return render_template('hello.html',name=name)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=13579, debug=False)
3)、hello.html
from Flask</title>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello World!</h1>
{% endif %}
2、flask 压力测试:
1)、执行hello.py【启动flask服务】
浏览器访问:http://127.0.0.1:13579/
2)、siege测试
经过测试,应该我这个电脑配置不高,还是设置为1000个用户,每个访问1次测试吧。
siege -c 1000 -r 1 -b http://127.0.0.1:13579/3344
返回最终压力测试结果:只有63%的成功率。 最高并发394.67。
Transactions: 783 hits Availability: 78.30 % Elapsed time: 2.66 secs Data transferred: 0.03 MB Response time: 1.34 secs Transaction rate: 294.36 trans/sec Throughput: 0.01 MB/sec Concurrency: 394.67 Successful transactions: 783 Failed transactions: 217 Longest transaction: 2.62 Shortest transaction: 0.32
3、flask 打开自带的多线程
1)、修改代码
app.run(threaded=True)
app.run(processes=True)
ps:多进程或多线程只能选择一个,不能同时开启
app.run(host='0.0.0.0', port=13579,threaded=True, debug=False)
2)、压力测试
siege -c 1000 -r 1 -b http://127.0.0.1:13579/3344
返回的结果: 成功率反而降低了。
Transactions: 774 hits Availability: 77.40 % Elapsed time: 1.55 secs Data transferred: 0.03 MB Response time: 0.81 secs Transaction rate: 499.35 trans/sec Throughput: 0.02 MB/sec Concurrency: 405.55 Successful transactions: 774 Failed transactions: 226 Longest transaction: 1.45 Shortest transaction: 0.30
多进程:
Transactions: 728 hits Availability: 72.80 % Elapsed time: 1.28 secs Data transferred: 0.03 MB Response time: 0.74 secs Transaction rate: 568.75 trans/sec Throughput: 0.02 MB/sec Concurrency: 423.62 Successful transactions: 728 Failed transactions: 272 Longest transaction: 1.24 Shortest transaction: 0.28
4、gunicorn 部署:
1)、gunicorn 部署
pip install gunicorn
gunicorn -w 1 -b 127.0.0.1:13578 hello:app
先浏览器访问一下,还是可以的。
2)、压力测试:
siege -c 1000 -r 1 -b http://127.0.0.1:13578/3344
①、w=1:
Transactions: 501 hits Availability: 50.10 % Elapsed time: 1.59 secs Data transferred: 0.02 MB Response time: 0.99 secs Transaction rate: 315.09 trans/sec Throughput: 0.01 MB/sec Concurrency: 312.28 Successful transactions: 501 Failed transactions: 499 Longest transaction: 1.51 Shortest transaction: 0.00
②、w=3
Transactions: 608 hits Availability: 60.80 % Elapsed time: 1.32 secs Data transferred: 0.02 MB Response time: 0.57 secs Transaction rate: 460.61 trans/sec Throughput: 0.02 MB/sec Concurrency: 260.27 Successful transactions: 608 Failed transactions: 392 Longest transaction: 1.27 Shortest transaction: 0.00
③、w=8
Transactions: 578 hits Availability: 57.80 % Elapsed time: 1.53 secs Data transferred: 0.02 MB Response time: 0.99 secs Transaction rate: 377.78 trans/sec Throughput: 0.01 MB/sec Concurrency: 372.63 Successful transactions: 578 Failed transactions: 422 Longest transaction: 1.51 Shortest transaction: 0.00
5、flask+gevent:
安装gevent模块:
pip install gevent
1)、hello-gevent.py
from gevent import monkey
monkey.patch_all()
from gevent.pywsgi import WSGIServer
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/<name>')
def index(name=None):
return render_template('hello.html', name=name)
if __name__== '__main__':
http_server = WSGIServer(('127.0.0.1', int(13579)), app)
http_server.serve_forever()
2)、压力测试:
成功率,有98%,99%,89%,取最后一次,发现,效果确实提升了不少。 不知道原作者前几个怎么高的,我的咋差别这么多。
siege -c 1000 -r 1 -b http://127.0.0.1:13579/3344
Transactions: 955 hits Availability: 95.50 % Elapsed time: 1.23 secs Data transferred: 0.04 MB Response time: 0.71 secs Transaction rate: 776.42 trans/sec Throughput: 0.03 MB/sec Concurrency: 550.13 Successful transactions: 955 Failed transactions: 45 Longest transaction: 1.13 Shortest transaction: 0.16
6、tornado:
1)、代码:
import logging
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.options import define, options
define('port', type=int, default=13579)
define('mode', default='debug')
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/<name>')
def index(name=None):
return render_template('hello.html', name=name)
def main():
options.parse_command_line()
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(options.port)
logging.info("[UOP] App is running on: localhost:%d" % options.port)
IOLoop.instance().start()
if __name__ == '__main__':
main()
直接跑这个py文件即可:
2)、压测:
siege -c 1000 -r 1 -b http://127.0.0.1:13579/3344
这个也是不固定值,这个可能和我的办公电脑有关吧。。。
Transactions: 863 hits Availability: 86.30 % Elapsed time: 1.34 secs Data transferred: 0.04 MB Response time: 0.82 secs Transaction rate: 644.03 trans/sec Throughput: 0.03 MB/sec Concurrency: 526.29 Successful transactions: 863 Failed transactions: 137 Longest transaction: 1.30 Shortest transaction: 0.21
7、twist + flask
1)、服务代码:
from flask import Flask
from flask import render_template
from flask_twisted import Twisted
app = Flask(__name__)
@app.route('/')
@app.route('/<name>')
def index(name=None):
print('come into index')
return render_template('hello.html', name=name)
twisted = Twisted(app)
if __name__ == "__main__":
print('come into main')
twisted.run(host='0.0.0.0',port=13579, debug=False)
直接运行脚本
2)、压测:
siege -c 1000 -r 1 -b http://127.0.0.1:13579/3344
平均在80%左右。
Transactions: 778 hits Availability: 77.80 % Elapsed time: 1.62 secs Data transferred: 0.04 MB Response time: 1.21 secs Transaction rate: 480.25 trans/sec Throughput: 0.02 MB/sec Concurrency: 578.83 Successful transactions: 778 Failed transactions: 222 Longest transaction: 1.58 Shortest transaction: 0.65
参考地址:
https://www.dazhuanlan.com/shsunyang/topics/932282 https://github.com/ewwink/siege-windows http://mrdede.com/?p=3410 https://blog.csdn.net/wsliangjian/article/details/47147827 https://blog.csdn.net/funnyPython/article/details/78824598 https://www.joedog.org/siege-home/ http://download.joedog.org/siege/ https://blog.csdn.net/u011466469/article/details/88593043 https://blog.csdn.net/marscrazy_90/article/details/41943211 https://zhuanlan.zhihu.com/p/131364462 独立 WSGI 容器 http://docs.jinkan.org/docs/flask/deploying/wsgi-standalone.html
|