主要目的是测试python的三个memcached client的性能
分别是python-memcached 1.43 和cmemcache 0.95(libmemcache-1.4.0.rc2 with patch ) 还有 python-libmemcached 0.13.1(libmemcached 0.26)
服务器信息
CPU:Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
内存:4GB
Python版本:2.5.3c1
memcached:1.2.6
Linux:RedHat AS 4 (32bit) / 2.6.9-78.ELsmp
测试数据1,000,000条,key & value均是从1到1000000,写入前均重启memcached
启动参数:memcached -u memcached -d -m 128 -p 11211
1,000,000条数据占用内存:60777780 bytes
分别用python的单线程和多线程来测试读写memcached的速度
(一)Python单线程:
①python-memcached
write:66.30s
read:74.20s
②cmemcache
write:45.67s
read:42.16s
write:45.51s
read:42.91s
(二)Python多线程:
此处我将100万条数据分给4个线程处理
#threadName是0~3,四个整数 l = [ str(i) for i in xrange(self.threadName * 250000, (self.threadName + 1) * 250000)]
①python-memcached
write:
___Thread(0) Used: 69.513560056686401
___Thread(1) Used: 74.226099967956543
___Thread(2) Used: 75.597586154937744
___Thread(3) Used: 76.586071014404297
read:
___Thread(2) Used: 79.42231011390686
___Thread(3) Used: 82.816195011138916
___Thread(1) Used: 83.262571096420288
___Thread(0) Used: 83.155163049697876
②cmemcache
write:
___Thread(0) Used: 22.632564783096313
___Thread(1) Used: 22.02928900718689
___Thread(2) Used: 22.382982969284058
___Thread(3) Used: 22.598598957061768
read:
___Thread(1) Used: 20.166458129882812
___Thread(2) Used: 20.590478897094727
___Thread(0) Used: 20.807707786560059
___Thread(3) Used: 20.396129846572876
write:
___Thread(1) Used: 11.283051013946533
___Thread(2) Used: 11.512333869934082
___Thread(0) Used: 10.283640146255493
___Thread(3) Used: 21.939767837524414
read:
___Thread(3) Used: 9.5806050300598145
___Thread(0) Used: 9.6075308322906494
___Thread(2) Used: 9.7577638626098633
___Thread(1) Used: 10.605507850646973
总结:
python-memcached奇怪的是,在多线程的时候,效率比它单线程要差很多,cmemcache与python-libmemcached单线程的时候几乎没有差别,但在多线程的时候,使用python-libmemcached编写的测试脚本中的四个线程不是同时执行,造成多线程测试脚本执行时间与单线程脚本的执行时间差不多,不知道为什么。使用cmemcache与python-memcached编写的脚本中的四个线程几乎同时开始,并且同时结束,脚本执行时间就是速度最慢的线程的时间。
Django也推荐使用cmemcached,见 http://docs.djangoproject.com/en/dev/topics/cache
附件:
使用python-libmemcached的测试脚本,包括单线程和多线程版本