memcached的压力测试 ::-- ZoomQuiet [DateTime(2007-07-16T02:36:44Z)] TableOfContents
1. memcached压力测试
{{{"John.H" <[email protected]> hide details 9:55 am (1 minute ago)
reply-to [email protected] to "python.cn" <[email protected]> date Jul 16, 2007 9:55 AM subject [CPyUG:29059] 用python写了一个memcached的压力测试程序,望大家指点 mailed-by googlegroups.com
}}}
1 import memcache
2 import threading
3 import time
4 import random
5 import sys
6
7 serverList = ['192.168.21.106:11211']
8 dIndexRange = 1000 #测试数据的下标范围
9 keyPre = 'TestKey_%d'#测试数据Key前缀
10 dataSize = 512 #每条测试数据 大小
11
12 #test result
13 totleTime = 0
14 times = 0
15 avgTime = 0
16 minTime = 0
17 maxTime = 0
18 rePerS = 0 #每秒相应数
19 opList = [0,0,0] #读,写,删次数
20 myLock = threading.RLock()
21
22 class testThread(threading.Thread):
23 def __init__(self,tName='testMemcached'):
24 threading.Thread.__init__(self,name=tName)
25
26 def run(self):
27 global serverList,dIndexRange,keyPre,dataSize
28 global totleTime,times,avgTime,minTime,maxTime,rePerS,myLock
29
30 mc = memcache.Client(serverList,debug=0)
31 #随机操作类型和下标
32 opType = random.randrange(0,3)
33 opKey = random.randrange(0,dIndexRange)
34 opKey = keyPre%(opKey)
35 #初始数据
36 opData = []
37 for i in range(dataSize):
38 opData.append(i)
39
40 startTime = time.time()
41 if opType == 0:
42 mc.set(opKey,opData)
43 elif opType == 1:
44 mc.get(opKey)
45 elif opType == 2:
46 opresult = mc.delete(opKey)
47 endTime = time.time()
48 opTime = endTime - startTime
49
50 myLock.acquire()
51 opList[opType] = opList[opType] + 1
52 print opTime
53 totleTime = totleTime + opTime
54 if minTime == 0 or opTime < minTime:
55 minTime = opTime
56 if maxTime == 0 or maxTime < opTime:
57 maxTime = opTime
58
59 myLock.release()
60 mc.disconnect_all()
61
62
63 times = int(sys.argv[1])
64 threadNum = int(sys.argv[2])
65
66 for i in range(times):
67 while threading.activeCount()>threadNum:
68 time.sleep(0.1)
69 else:
70 testThread().start()
71
72 while threading.activeCount()>1:
73 time.sleep(2)
74 else:
75 print '\xb2\xe2\xca\xd4\xcd\xea\xb3\xc9:\xd7\xdc\xd3\xc3\xca\xb1 %f
76 s' %(totleTime)
77 print '\xb2\xe2\xca\xd4\xb4\xce\xca\xfd: %d \xb2\xa2\xb7\xa2\xc7\xeb
78 \xc7\xf3: %d'%(times,threadNum)
79 print '\xc6\xbd\xbe\xf9\xc3\xbf\xb4\xce\xd3\xc3\xca\xb1 %f s'%
80 (totleTime/times)
81 print '\xd7\xee\xb4\xf3\xd3\xc3\xca\xb1: %f s \xd7\xee
82 \xd0\xa1\xd3\xc3\xca\xb1 %f s'%(maxTime,minTime)
83 print '\xc3\xbf\xc3\xeb\xcf\xec\xd3\xa6\xca\xfd:%f'%(times/totleTime)
84 print '\xbd\xf8\xd0\xd0\xc1\xcb %d \xb4\xce set \xb2\xd9\xd7\xf7'%
85 (opList[0])
86 print '\xbd\xf8\xd0\xd0\xc1\xcb %d \xb4\xce get \xb2\xd9\xd7\xf7'%
87 (opList[1])
88 print '\xbd\xf8\xd0\xd0\xc1\xcb %d \xb4\xce delete \xb2\xd9\xd7\xf7'%
89 (opList[2])