##language:zh #pragma section-numbers on ''' memcached的压力测试 ''' ::-- ZoomQuiet [<>] <> ## 默许导航,请保留 <> = memcached压力测试 = {{{"John.H" hide details 9:55 am (1 minute ago) reply-to python-cn@googlegroups.com to "python.cn" date Jul 16, 2007 9:55 AM subject [CPyUG:29059] 用python写了一个memcached的压力测试程序,望大家指点 mailed-by googlegroups.com }}} {{{#!python import memcache import threading import time import random import sys serverList = ['192.168.21.106:11211'] dIndexRange = 1000 #测试数据的下标范围 keyPre = 'TestKey_%d'#测试数据Key前缀 dataSize = 512 #每条测试数据 大小 #test result totleTime = 0 times = 0 avgTime = 0 minTime = 0 maxTime = 0 rePerS = 0 #每秒相应数 opList = [0,0,0] #读,写,删次数 myLock = threading.RLock() class testThread(threading.Thread): def __init__(self,tName='testMemcached'): threading.Thread.__init__(self,name=tName) def run(self): global serverList,dIndexRange,keyPre,dataSize global totleTime,times,avgTime,minTime,maxTime,rePerS,myLock mc = memcache.Client(serverList,debug=0) #随机操作类型和下标 opType = random.randrange(0,3) opKey = random.randrange(0,dIndexRange) opKey = keyPre%(opKey) #初始数据 opData = [] for i in range(dataSize): opData.append(i) startTime = time.time() if opType == 0: mc.set(opKey,opData) elif opType == 1: mc.get(opKey) elif opType == 2: opresult = mc.delete(opKey) endTime = time.time() opTime = endTime - startTime myLock.acquire() opList[opType] = opList[opType] + 1 print opTime totleTime = totleTime + opTime if minTime == 0 or opTime < minTime: minTime = opTime if maxTime == 0 or maxTime < opTime: maxTime = opTime myLock.release() mc.disconnect_all() times = int(sys.argv[1]) threadNum = int(sys.argv[2]) for i in range(times): while threading.activeCount()>threadNum: time.sleep(0.1) else: testThread().start() while threading.activeCount()>1: time.sleep(2) else: import memcache import threading import time import random import sys serverList = ['192.168.21.106:11211'] dIndexRange = 1000 #测试数据的下标范围 keyPre = 'TestKey_%d'#测试数据Key前缀 dataSize = 512 #每条测试数据 大小 #test result totleTime = 0 times = 0 avgTime = 0 minTime = 0 maxTime = 0 rePerS = 0 #每秒相应数 opList = [0,0,0] #读,写,删次数 myLock = threading.RLock() class testThread(threading.Thread): def __init__(self,tName='testMemcached'): threading.Thread.__init__(self,name=tName) def run(self): global serverList,dIndexRange,keyPre,dataSize global totleTime,times,avgTime,minTime,maxTime,rePerS,myLock mc = memcache.Client(serverList,debug=0) #随机操作类型和下标 opType = random.randrange(0,3) opKey = random.randrange(0,dIndexRange) opKey = keyPre%(opKey) #初始数据 opData = [] for i in range(dataSize): opData.append(i) startTime = time.time() if opType == 0: mc.set(opKey,opData) elif opType == 1: mc.get(opKey) elif opType == 2: opresult = mc.delete(opKey) endTime = time.time() opTime = endTime - startTime myLock.acquire() opList[opType] = opList[opType] + 1 print opTime totleTime = totleTime + opTime if minTime == 0 or opTime < minTime: minTime = opTime if maxTime == 0 or maxTime < opTime: maxTime = opTime myLock.release() mc.disconnect_all() times = int(sys.argv[1]) threadNum = int(sys.argv[2]) for i in range(times): while threading.activeCount()>threadNum: time.sleep(0.1) else: testThread().start() while threading.activeCount()>1: time.sleep(2) else: print 'Total used time: %f s' %(totleTime) print 'Send request: %d simultaneities: %d'%(times,threadNum) print 'Avarage time %f s'%(totleTime/times) print 'MaxTime: %f s MinTime %f s'%(maxTime,minTime) print 'Requests Per Second:%f'%(times/totleTime) print 'Set operations: %d '%(opList[0]) print 'Get operations: %d '%(opList[1]) print 'Delete operations: %d'%(opList[2]) }}} == 反馈 ==