LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > 脚本编程 >

Python boom模块介绍

时间:2015-06-10 09:11来源:未知 编辑:linuxeye 点击:
我们先对ApacheBench做个简介,再介绍boom。 ApacheBench ApacheBench简介 ApacheBench 是一个用来衡量http服务器性能的单线程命令行工具。原本针对Apache http服务器,但是也适用于其他http服务器。

我们先对ApacheBench做个简介,再介绍boom。

ApacheBench

ApacheBench简介

ApacheBench 是一个用来衡量http服务器性能的单线程命令行工具。原本针对Apache http服务器,但是也适用于其他http服务器。

ab工具与标准 Apache源码一起发布,免费,开源,基于Apache License

ApacheBench安装

ubuntu 14.04 执行“apt-get install apache2-utils“即可。

ApacheBench快速入门

发送1000个http get请求到http://192.168.10.232:8000/blog,并发为10:

# ab -n1000 -c10 http://192.168.10.232:8000/blogThis is ApacheBench, Version 2.3 <$Revision: 1528965 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.10.232 (be patient)Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        tracd/1.0.1Server Hostname:        192.168.10.232Server Port:            8000Document Path:          /blog
Document Length:        330918 bytesConcurrency Level:      10Time taken for tests:   1353.994 seconds
Complete requests:      1000Failed requests:        0Total transferred:      331323000 bytesHTML transferred:       330918000 bytesRequests per second:    0.74 [#/sec] (mean)Time per request:       13539.943 [ms] (mean)Time per request:       1353.994 [ms] (mean, across all concurrent requests)Transfer rate:          238.97 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   maxConnect:        1    1   0.8      1      17Processing:  8703 13500 640.5  13586   15076Waiting:     8646 13387 641.6  13480   14957Total:       8704 13501 640.5  13587   15079Percentage of the requests served within a certain time (ms)
  50%  13587
  66%  13742
  75%  13848
  80%  13912
  90%  14133
  95%  14315
  98%  14516
  99%  14618
 100%  15079 (longest request)

注意Time per request: 13539.943 [ms] (mean)是平均每批请求的完成时间,这里时10个请求完成的时间。Time per request: 1353.994 [ms] (mean, across all concurrent requests)是平均每个请求的完成时间。

ApacheBench将只使用一个操作系统线程。很多情况下ApacheBench本身是瓶颈。当在硬件具有多个处理器核心使用ApacheBench,建议一个cpu核执行一个ab线程,但是现在cpu核数很多的情况下,比如48核,手工执行48个命令也是个体力活。

整体来说ab因为不能利用到多核、不能进行参数化、不支持http和https以外的协议等原因,只适合单个组件的粗略测试,比如Apache http。不合适业务级别的测试,如一旦后台有数据库等,ab的测试是几乎没有意义的。

ApacheBench参考

  • 命令行帮助

    $ ab -h
    Usage: ab [options] [http[s]://]hostname[:port]/path
    Options are:
        -n requests     Number of requests to perform    -c concurrency  Number of multiple requests to make at a time    -t timelimit    Seconds to max. to spend on benchmarking
                        This implies -n 50000
        -s timeout      Seconds to max. wait for each response
                        Default is 30 seconds    -b windowsize   Size of TCP send/receive buffer, in bytes
        -B address      Address to bind to when making outgoing connections    -p postfile     File containing data to POST. Remember also to set -T    -u putfile      File containing data to PUT. Remember also to set -T    -T content-type Content-type header to use for POST/PUT data, eg.
                        'application/x-www-form-urlencoded'
                        Default is 'text/plain'
        -v verbosity    How much troubleshooting info to print
        -w              Print out results in HTML tables    -i              Use HEAD instead of GET    -x attributes   String to insert as table attributes    -y attributes   String to insert as tr attributes    -z attributes   String to insert as td or th attributes    -C attribute    Add cookie, eg. 'Apache=1234'. (repeatable)
        -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
                        Inserted after all normal header lines. (repeatable)
        -A attribute    Add Basic WWW Authentication, the attributes
                        are a colon separated username and password.
        -P attribute    Add Basic Proxy Authentication, the attributes
                        are a colon separated username and password.
        -X proxy:port   Proxyserver and port number to use    -V              Print version number and exit
        -k              Use HTTP KeepAlive feature    -d              Do not show percentiles served table.
        -S              Do not show confidence estimators and warnings.
        -q              Do not show progress when doing more than 150 requests    -l              Accept variable document length (use this for dynamic pages)
        -g filename     Output collected data to gnuplot format file.
        -e filename     Output CSV file with percentages served    -r              Don't exit on socket receive errors.
        -h              Display usage information (this message)
        -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
        -f protocol     Specify SSL/TLS protocol                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
  • 语法:

    ab [ -A auth-username:password ] [ -b windowsize ] [ -c concurrency ] [ -C cookie-name=value ] [ -d ] [ -e csv-file ] [ -f protocol ] [ -g gnuplot-file ] [ -h ] [ -H custom-header ] [ -i ] [ -k ] [ -n requests ] [ -p POST-file ] [ -P proxy-auth-username:password ] [ -q ] [ -r ] [ -s ] [ -S ] [ -t timelimit ] [ -T content-type ] [ -u PUT-file ] [ -v verbosity] [ -V ] [ -w ] [ -x <table>-attributes ] [ -X proxy[:port] ] [ -y <tr>-attributes ] [ -z <td>-attributes ] [ -Z ciphersuite ] [http[s]://]hostname[:port]/path
  • 命令行选项

    -A auth-username:password
        向服务器提供基本认证信息。用户名和密码之间":"分割,以base64编码形式发送。无论服务器是否需要(即是否发送了401)都发送。 
    
    -b windowsize
        TCP发送/接收缓冲区大小,以字节为单位。
    
    -c concurrency
        并发数,默认为1。
    
    -C cookie-name=value
        添加Cookie。典型形式是name=value对。name参数可以重复。 
    
    -d
    不显示"percentage served within XX [ms] table"消息(兼容以前的版本)。 
    
    -e csv-file
        输出百分率和对应的时间,格式为逗号份额的csv。由于这种格式已经"二进制化",所以比"gnuplot"格式更有用。
    
    -f protocol
        SSL/TLS protocol (SSL2, SSL3, TLS1, 或ALL).
    
    -g gnuplot-file
        把所有测试结果写入"gnuplot"或者TSV(以Tab分隔)文件。该文件可以方便地导入到Gnuplot, IDL, Mathematica甚至Excel中,第一行为标题。
    
    -h
        显示使用方法。
    
    -H custom-header
        附加额外头信息。典型形式有效的头信息行,包含冒号分隔的字段和值(如:"Accept-Encoding: zip/zop;8bit")。
    
    -i
        执行HEAD请求,而不是GET 。
    
    -k
        启用KeepAlive功能,即在HTTP会话中执行多个请求。默认关闭。
    
    -n requests
        会话执行的请求数。默认为1。 
    
    -p POST-file
        附加包含POST数据的文件。注意和-T一起使用。
    
    -P proxy-auth-username:password
        代理认证。用户名和密码之间":"分割,以base64编码形式发送。无论服务器是否需要(即是否发送了407)都发送。
    
    -q
        quiet,静默模式。不在stderr输出进度条。
    
    -r
        套接字接收错误时不退出。
    
    -s timeout 
        超时,默认为30秒。
    
    -S
        不显示中值和标准偏差值,而且在均值和中值为标准偏差值的1到2倍时,也不显示警告或出错信息。默认显示最小值/均值/最大值。(兼容以前的版本)-t timelimit
        测试进行的最大秒数。内部隐含值是"-n 50000"。默认没有时间限制。
    
    -T content-type
        POST/PUT的"Content-type"头信息。比如“application/x-www-form-urlencoded”,默认“text/plain”。
    
    -v verbosity
        详细模式,4以上会显示头信息,3以上显示响应代码(404,200等),2以上显示告警和info。
    
    -V
        显示版本号并退出。
    
    -w
        以HTML表格形式输出。默认是白色背景的两列。
    
    -x <table>-attributes
        设置<table>属性。此属性填入<table 这里 > 。
    
    -X proxy[:port]
        使用代理服务器。
    
    -y <tr>-attributes
        设置<tr>属性。
    
    -z <td>-attributes
        设置<td>属性。 
    
    -Z ciphersuite
        设置SSL/TLS加密
 
  • 结果分析字段

    Server Software
        返回的第一次成功的服务器响应的HTTP头。
    
    Server Hostname
        命令行中给出的域名或IP地址
    
    Server Port
        命令行中给出端口。如果没有80(HTTP)和443(HTTPS)。
    
    SSL/TLS Protocol
        使用SSL打印。
    
    Document Path
        命令行请求的路径。
    
    Document Length
        第一次成功地返回文档的字节大小。后面接受的文档长度变化时,会认为是错误。
    
    Concurrency Level
        并发数
    
    Time taken for tests
        测试耗时
    
    Complete requests
        收到成功响应数
    
    Failed requests
        失败请求数。如果有会打印错误原因
    
    Write errors
       写错误数 (broken pipe)Non-2xx responses
        非2**响应数量。如果有打印。
    
    Keep-Alive requests
        Keep-Alive请求的连接数
    
    Total body sent:
        传输的body的数据量,比如POST的数据。
    
    Total transferred:
        总传输数据量
    
    HTML transferred:       
        累计html传输数据量
    
    Time per request:      
        每批平均请求时间
    
    Time per request: 
        每次平均请求时间。计算公式:Time per request/Concurrency Level。
    
    Transfer rate
        数据传输速率。计算公式:otalread / 1024 / timetaken。

boom

boom简介

boom是python替代ab的模块,并增加了部分功能。

boom安装

ubuntu 14.04 执行“pip install boom“即可,注意可能需要先执行"apt-get install libevent python-dev"。它使用Gevent创建虚拟用户,使用Requests发送请求。

boom快速入门

# boom -n1000 -c10 http://192.168.10.232:8000/blogServer Software: tracd/1.0.1 Python/2.7.3Running GET http://192.168.10.232:8000/blog
Running 1000 times per 10 workers.[================================================================>.] 99% Done-------- Results --------Successful calls                1000Total time                      1355.1412 s
Average                         13.5156 s
Fastest                         8.2434 s
Slowest                         15.3094 s
Amplitude                       7.0660 s
RPS                             0BSI                             Hahahaha-------- Status codes --------Code 200                        1000 times.-------- Legend --------RPS: Request Per Second
BSI: Boom Speed Index
Average:每批请求的平均处理时间。 Fastest:每批请求的最快处理时间。 Slowest:每批请求的最慢处理时间。 Amplitude:振幅,即最慢与最忙批次处理时间差。

boom参考

$ boom -h
usage: boom [-h] [--version] [-m {GET,POST,DELETE,PUT,HEAD,OPTIONS}]
            [--content-type CONTENT_TYPE] [-D DATA] [-c CONCURRENCY] [-a AUTH]
            [--header HEADER] [--hook HOOK] [--json-output]
            [-n REQUESTS | -d DURATION]
            [url]Simple HTTP Load runner.positional arguments:
  url                   URL to hit

optional arguments:
  -h, --help            show this help message and exit
  --version             Displays version and exits.
  -m {GET,POST,DELETE,PUT,HEAD,OPTIONS}, --method {GET,POST,DELETE,PUT,HEAD,OPTIONS}
                        HTTP Method  --content-type CONTENT_TYPE
                        Content-Type  -D DATA, --data DATA  Data. Prefixed by "py:" to point a python callable.
  -c CONCURRENCY, --concurrency CONCURRENCY
                        Concurrency  -a AUTH, --auth AUTH  Basic authentication user:password  --header HEADER       Custom header. name:value  --hook HOOK           Python callable that'll be used on every requests call
  --json-output         Prints the results in JSON instead of the default
                        format  -n REQUESTS, --requests REQUESTS
                        Number of requests  -d DURATION, --duration DURATION
                        Duration in seconds

结论

ab和boom在功能上远不及multi-mechanize和Grinder等主力python性能测试工具,甚至还比不上庞大且扩能能力差的loadrunner。只建议在对单个中间件的基准测试时使用。

转载请保留固定链接: https://linuxeye.com/program/Python-boom.html

------分隔线----------------------------
标签:Pythonboom
栏目列表
推荐内容