Feb5

【原创】Redis与Memcached性能测试对比

Author: leeon  Click: 5500   Comments: 0 Category: 优化  Tag: redis,memcached

      最近一哥们面试一家在线视频直播的公司的PHP岗位,面试官问一题目“redis一定比memcached读写性能更好吗?”,哥们回答是不一定,但是这面试官一口咬定redis性能就是比memcached好,我对这个答案不置可否,我认为应该是看应用场景来判断性能优劣,而不能一口认定redis就比memcached的读写性能要好。

为了真实的反应最新版本的memcached和redis的单实例进程性能,我们做一个比较客观的测试。

测试环境:树莓派3,1G内存,4核 ARMv7

memcached:1.4.34版本,设置内存256M,参数设置为-r 100000 -c 10240 -m 256M -t 4 -b 20480

redis:3.2.7版本,关闭掉数据落地的相关设置,纯粹的测试内存的读写访问性能。

        为了保证对redis和memcached的请求设置一致化,我们采用memtier_benchmark工具来进行性能压力测试。同时保证公平性采用memtier_benchmark默认的压力测试配置参数。

memcached压力测试命令:

./memtier_benchmark -p 11211 -P memcache_binary --hide-histogram

redis压力测试命令:

./memtier_benchmark -p 6379 --hide-histogram

测试结果如下:

redis:

[code="plain"]
root@home2:~/memtier_benchmark# ./memtier_benchmark -p 6379 --hide-histogram
[RUN #1] Preparing benchmark client...
[RUN #1] Launching threads now...
[RUN #1 100%, 76 secs] 0 threads: 2000000 ops, 26604 (avg: 26231) ops/sec, 1010.67KB/sec (avg: 996.38KB/sec), 7.52 (avg: 1.17) msec latencyy

4 Threads
50 Connections per thread
10000 Requests per thread
Type Ops/sec Hits/sec Misses/sec Latency KB/sec
------------------------------------------------------------------------
Sets 2367.43 --- --- 7.63500 182.35
Gets 23648.29 2.60 23645.69 7.61300 805.85
Waits 0.00 --- --- 0.00000 ---
Totals 26015.72 2.60 23645.69 7.61500 988.20

[/code]

memcached:

[code="plain"]
root@home2:~/memtier_benchmark# ./memtier_benchmark -p 11211 -P memcache_binary --hide-histogram
[RUN #1] Preparing benchmark client...
[RUN #1] Launching threads now...
[RUN #1 100%, 54 secs] 0 threads: 2000000 ops, 44100 (avg: 36452) ops/sec, 1.79MB/sec (avg: 1.48MB/sec), 4.52 (avg: 1.18) msec latencyy

4 Threads
50 Connections per thread
10000 Requests per thread
Type Ops/sec Hits/sec Misses/sec Latency KB/sec
------------------------------------------------------------------------
Sets 3325.54 --- --- 5.50400 256.14
Gets 33218.86 3.65 33215.20 5.47500 1261.61
Waits 0.00 --- --- 0.00000 ---
Totals 36544.40 3.65 33215.20 5.47800 1517.75

[/code]

从结果可以看出memcached的随机set get性能并不比redis差,因此我们抱有对memcached因为年久性能不行的固有思维印象是不可取的。memcached的多线程机制在读写交叉的高并发请求下性能或许会比redis要好一些,当然redis的主从机制是memcached未有的。

Jun13

ETag与Expires的区别

Author: leeon  Click: 6744   Comments: 0 Category: 优化  Tag: etag,expires
ETag是用在向服务器GET请求的时候判断文件是否过期。
而Expires是用来判断本地缓存的组件是否过期。
Etag 和 If-Modified-Since 均是用来判断服务器端的文件是否过期。
如果页面已经缓存,当页面刷新(F5)的时候浏览器不会向服务器GET请求。
如果页面(Ctrl+F5)的时候浏览器会向服务器发起询问,If-Modified-Since提交本地记录的组件的日期给服务器,如果服务器查询组件未做修改,那么返回304(Not Modified),如果服务器端文件有做修改那么服务器会返回新的组件给客户端。
Expires 和 Cache-Control用来判断本地电脑中的缓存文件是否过期。

通常的页面访问中,如果是访问一个全新的页面那么ETag,Expires的作用如下:
第一次访问服务器会将ETag和Expires发送给浏览器。
再次访问浏览器会根据缓存来加载组件,此时如果有缓存的就不发起HTTP请求。如果本地有缓存但是依旧强制向服务器提交组件的GET请求,那么ETag就会派上用场。

Cache-ControlExpires 有更高的优先级。
If-None-Match(ETag) If-Modified-Since(Last-Modified)有更高的优先级。
Feb17

nginx的rewrite 参数和例子

Author: leeon  Click: 8241   Comments: 0 Category: 优化  Tag: nginx,rewrite
nginx的rewrite 参数和例子
正则表达式匹配,其中:
* ~ 为区分大小写匹配
*~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中: * -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行
flag标记有: * last 相当于Apache里的[L]标记,表示完成rewrite
* break 终止匹配, 不再匹配后面的规则
* redirect 返回302临时重定向 地址栏会显示跳转后的地址
* permanent 返回301永久重定向 地址栏会显示跳转后的地址
一些可用的全局变量有,可以用做条件判断(待补全) $args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
结合QeePHP的例子
[code="plain"]
if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;[/code]
多目录转成参数
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
[code="plain"]
if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;   
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}[/code]
目录对换
/123456/xxxx -> /xxxx?id=123456
[code="plain"]
rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;[/code]
例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
[code="plain"]
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}[/code]
目录自动加“/”
[code="plain"]
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}[/code]
禁止htaccess
[code="plain"]
location ~/\.ht {
         deny all;
     }[/code]
禁止多个目录
[code="plain"]
location ~ ^/(cron|templates)/ {
         deny all;
break;
     }[/code]
禁止以/data开头的文件
可以禁止/data/下多级目录下.log.txt等请求;
[code="plain"]
location ~ ^/data {
         deny all;
     }[/code]
禁止单个目录
不能禁止.log.txt能请求
[code="plain"]
location /searchword/cron/ {
         deny all;
     }[/code]
禁止单个文件
[code="plain"]
location ~ /data/sql/data.sql {
         deny all;
     }[/code]
给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志
[code="plain"]
location ~(favicon.ico) {
                 log_not_found off;
expires 99d;
break;
     }      location ~(robots.txt) {
                 log_not_found off;
expires 7d;
break;
     }[/code]
设定某个文件的过期时间;这里为600秒,并不记录访问日志
[code="plain"]
location ^~ /html/scripts/loadhead_1.js {
                 access_log   off;
                 root /opt/lampp/htdocs/web;
expires 600;
break;
       }[/code]
文件反盗链并设置过期时间
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
“rewrite ^/ http://leech.example.com/leech.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存
[code="plain"]
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.example.com *.example.net localhost 208.97.167.194;
if ($invalid_referer) {
    rewrite ^/ http://leech.example.com/leech.gif;
    return 412;
    break;
}
                 access_log   off;
                 root /opt/lampp/htdocs/web;
expires 3d;
break;
     }[/code]
只充许固定ip访问网站,并加上密码
[code="plain"]
root  /opt/htdocs/www;
allow   208.97.167.194;
allow   222.33.1.2;
allow   231.152.49.4;
deny    all;
auth_basic "C1G_ADMIN";
auth_basic_user_file htpasswd;[/code]
将多级目录下的文件转成一个文件,增强seo效果
/job-123-456-789.html 指向/job/123/456/789.html
[code="plain"]
rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;[/code]
将根目录下某个文件夹指向2级目录
如/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/
[code="plain"]
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;[/code]
上面例子有个问题是访问/shanghai 时将不会匹配
[code="plain"]
rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;[/code]
这样/shanghai 也可以访问了,但页面中的相对链接无法使用,
如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。
那我加上自动跳转也是不行咯
(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果
[code="plain"]
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}[/code]
知道原因后就好办了,让我手动跳转吧
[code="plain"]
rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;[/code]
文件和目录不存在的时候重定向:
[code="plain"]
if (!-e $request_filename) {
proxy_pass http://127.0.0.1;
}[/code]
域名跳转
[code="plain"]
server
     {
             listen       80;
             server_name  jump.example.com;
             index index.html index.htm index.php;
             root  /opt/lampp/htdocs/www;
             rewrite ^/ http://www.example.com/;
             access_log  off;
     }[/code]
多域名转向
[code="plain"]
server_name  www.example.com www.example.net;
             index index.html index.htm index.php;
             root  /opt/lampp/htdocs;
if ($host ~ "example\.net") {
rewrite ^(.*) http://www.example.com$1 permanent;
}[/code]
三级域名跳转
[code="plain"]
if ($http_host ~* "^(.*)\.i\.example\.com$") {
rewrite ^(.*) http://top.yingjiesheng.com$1;
break;
}[/code]
域名镜向
[code="plain"]
server
     {
             listen       80;
             server_name  mirror.example.com;
             index index.html index.htm index.php;
             root  /opt/lampp/htdocs/www;
             rewrite ^/(.*) http://www.example.com/$1 last;
             access_log  off;
     }[/code]
某个子目录作镜向
[code="plain"]
location ^~ /zhaopinhui {
  rewrite ^.+ http://zph.example.com/ last;
  break;
     }
[/code]
给discuz某版块单独配置域名
[code="plain"]
server_name  bbs.example.com news.example.com;      location = / {
        if ($http_host ~ news\.example.com$) {
  rewrite ^.+ http://news.example.com/forum-831-1.html last;
  break;
}
     }[/code]
Feb3

ApacheBench测试结果解析

Author: leeon  Click: 9276   Comments: 0 Category: 优化  Tag: apache,apachebench

ab -n 10 -c 10 http://www.google.com/ 
这个命令的意思是启动ab ,向 www.google.com 发送10个请求(-n 10) ,并每次模拟10个并发用户请求(-c 10)——也就是说一次都发过去了。
-n是总共发送的请求数
-c是每次并发用户数

Benchmarking www.google.com (be patient).....done

 

 

Server Software:        GWS/2.1

Server Hostname:        www.google.com

Server Port:            80

 

Document Path:          /

Document Length:        230 bytes

 

Concurrency Level:      10

/*整个测试持续的时间*/

Time taken for tests:   3.234651 seconds

/*完成的请求数量*/

Complete requests:      10

/*失败的请求数量*/

Failed requests:        0

Write errors:           0

Non-2xx responses:      10

Keep-Alive requests:    10

/*整个场景中的网络传输量*/

Total transferred:      6020 bytes

/*整个场景中的HTML内容传输量*/

HTML transferred:       2300 bytes

/*相当于 LR 中的 每秒事务数 ,mean 表示这是一个平均值*/

Requests per second:    3.09 [#/sec] (mean)

/*相当于 LR 中的 平均事务响应时间,一次并发10个请求花费的时间 */

Time per request:       3234.651 [ms] (mean)

/*一次并发10个用户请求时的每个请求的平均响应时间*/

Time per request:       323.465 [ms] (mean, across all concurrent requests)

/*平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题*/

Transfer rate:          1.55 [Kbytes/sec] received

/*网络上消耗的时间的分解,*/

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:       20  318 926.1     30    2954  //连接开销的时间

Processing:    40 2160 1462.0   3034    3154 //服务器端处理的时间与等待的时间相等

Waiting:       40 2160 1462.0   3034    3154 //等待的时间

Total:         60 2479 1276.4   3064    3184 //总共花费的时间

 

/*下面的内容为整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中 50% 的用户响应时间小于 3064 毫秒,60 % 的用户响应时间小于 3094 毫秒,最大的响应时间小于 3184 毫秒*/


[code="bash"]
Percentage of the requests served within a certain time (ms)

50% 3064

66% 3094

75% 3124

80% 3154

90% 3184

95% 3184

98% 3184

99% 3184

100% 3184 (longest request)
[/code]


分类

标签

归档

最新评论

Abyss在00:04:28评论了
Linux中ramdisk,tmpfs,ramfs的介绍与性能测试
shallwe99在10:21:17评论了
【原创】如何在微信小程序开发中正确的使用vant ui组件
默一在09:04:53评论了
Berkeley DB 由浅入深【转自架构师杨建】
Memory在14:09:22评论了
【原创】最佳PHP框架选择(phalcon,yaf,laravel,thinkphp,yii)
leo在17:57:04评论了
shell中使用while循环ssh的注意事项

我看过的书

链接

其他

访问本站种子 本站平均热度:8823 c° 本站链接数:1 个 本站标签数:464 个 本站被评论次数:94 次