memcached的压力测试 ::-- ZoomQuiet [2007-07-16 02:36:44]
Contents
1. memcached压力测试
{{{"John.H" <xiaocong.hust@gmail.com> hide details 9:55 am (1 minute ago)
reply-to python-cn@googlegroups.com to "python.cn" <python-cn@googlegroups.com> date Jul 16, 2007 9:55 AM subject [CPyUG:29059] 用python写了一个memcached的压力测试程序,望大家指点 mailed-by googlegroups.com
}}}
Toggle line numbers
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 import memcache
76 import threading
77 import time
78 import random
79 import sys
80
81 serverList = ['192.168.21.106:11211']
82 dIndexRange = 1000 #测试数据的下标范围
83 keyPre = 'TestKey_%d'#测试数据Key前缀
84 dataSize = 512 #每条测试数据 大小
85
86 #test result
87 totleTime = 0
88 times = 0
89 avgTime = 0
90 minTime = 0
91 maxTime = 0
92 rePerS = 0 #每秒相应数
93 opList = [0,0,0] #读,写,删次数
94 myLock = threading.RLock()
95
96 class testThread(threading.Thread):
97 def __init__(self,tName='testMemcached'):
98 threading.Thread.__init__(self,name=tName)
99
100 def run(self):
101 global serverList,dIndexRange,keyPre,dataSize
102 global totleTime,times,avgTime,minTime,maxTime,rePerS,myLock
103
104 mc = memcache.Client(serverList,debug=0)
105 #随机操作类型和下标
106 opType = random.randrange(0,3)
107 opKey = random.randrange(0,dIndexRange)
108 opKey = keyPre%(opKey)
109 #初始数据
110 opData = []
111 for i in range(dataSize):
112 opData.append(i)
113
114 startTime = time.time()
115 if opType == 0:
116 mc.set(opKey,opData)
117 elif opType == 1:
118 mc.get(opKey)
119 elif opType == 2:
120 opresult = mc.delete(opKey)
121 endTime = time.time()
122 opTime = endTime - startTime
123
124 myLock.acquire()
125 opList[opType] = opList[opType] + 1
126 print opTime
127 totleTime = totleTime + opTime
128 if minTime == 0 or opTime < minTime:
129 minTime = opTime
130 if maxTime == 0 or maxTime < opTime:
131 maxTime = opTime
132
133 myLock.release()
134 mc.disconnect_all()
135
136
137 times = int(sys.argv[1])
138 threadNum = int(sys.argv[2])
139
140 for i in range(times):
141 while threading.activeCount()>threadNum:
142 time.sleep(0.1)
143 else:
144 testThread().start()
145
146 while threading.activeCount()>1:
147 time.sleep(2)
148 else:
149 print 'Total used time: %f s' %(totleTime)
150 print 'Send request: %d simultaneities: %d'%(times,threadNum)
151 print 'Avarage time %f s'%(totleTime/times)
152 print 'MaxTime: %f s MinTime %f s'%(maxTime,minTime)
153 print 'Requests Per Second:%f'%(times/totleTime)
154 print 'Set operations: %d '%(opList[0])
155 print 'Get operations: %d '%(opList[1])
156 print 'Delete operations: %d'%(opList[2])