Redis和Memcached在Ruby上的性能评测(2)——读取性能

阅读更多
上一篇我们测试了memcached和redis在ruby上的写入性能,这次我们来测试一下它们的单纯读取性能。

测试环境不变,代码只需要进行一点点修改

memcached测试代码:
require 'rubygems'
require 'memcached'
@cache = Memcached.new('localhost:11211')
100000.times do |i|
  @cache.get "Test/#{rand(10000)}"
end


redis测试代码:
require 'rubygems'
require 'hiredis'
@cache = Hiredis::Connection.new
@cache.connect("127.0.0.1", 6379)
100000.times do |i|
  @cache.write ['GET', "Test/#{rand(10000)}"]
  @cache.read
end


测试结果:
													real	user	sys
memcached	1x GET pipeline		10000 times		0.783s	0.274s	0.285s
redis		1x GET pipeline		10000 times		1.114s	0.417s	0.375s
						
memcached	10x GET pipeline	10000 times		1.634s	2.969s	1.745s	
redis		10x GET pipeline	10000 times		3.360s	5.204s	2.077s		
						
memcached	20x GET pipeline	10000 times		3.163s	6.081s	3.587s	
redis		20x GET pipeline	10000 times		6.588s	10.634s	4.338s	

memcached	1x GET pipeline		100000 times		6.723s	1.786s	2.658s
redis		1x GET pipeline		100000 times		9.157s	1.644s	2.109s
						
memcached	10x GET pipeline	100000 times		12.237s	17.363s	14.919s	
redis		10x GET pipeline	100000 times		24.886s	16.058s	14.649s		
						
memcached	20x GET pipeline	100000 times		24.184s	35.505s	29.955s	
redis		20x GET pipeline	100000 times		47.903s	32.386s	29.155s	

memcached	1x GET pipeline		1000000 times		1m19.704s	15.792s	23.092s
redis		1x GET pipeline		1000000 times		1m30.097s	15.186s	24.438s
						
memcached	10x GET pipeline	1000000 times		1m57.467s	2m44.216s	2m23.563s
redis		10x GET pipeline	1000000 times		3m58.644s	2m5.219s	2m16.882s
	
memcached	20x GET pipeline	1000000 times		3m51.706s	5m32.873s	4m48.830s	
redis		20x GET pipeline	1000000 times		7m28.133s	4m5.430s	4m28.178s


测试结果和写入时的基本一致,包括速度和资源占用。由此我们可以得出它们各自的使用场景:

memcached:性能就是一切,很大的并发量和读写量,有专门的服务器作为缓存机,这时候似乎就只能选择memcached了。

而以下情况可以考虑用redis替代:
1、并发量很小或每秒的读写次数比较有限
2、没有单独的cache服务器,还有其他比较消耗cpu的服务在运行
3、内存不是很足够(可以用redis的hash来作替代方案,内存可以节约6/7,参考:http://redis.io/topics/memory-optimization)
4、希望cache复杂数据类型

所以,当只追求性能的时候选择memcached,其他时候都可以尝试使用redis替代~


你可能感兴趣的:(memcached,redis)