-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stress.py
executable file
·165 lines (138 loc) · 4.63 KB
/
Stress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python2.7
import os
import sys
import time
#import subprocess
from subprocess import Popen,PIPE,STDOUT,call
from random import randint
from multiprocessing import Process, Queue
import clime
query_filename="query.txt"
build_filename="build.txt"
build_query_filename="build_query.txt"
drop_filename="drop.txt"
server="127.0.0.1"
mysql_port="3307"
restful_port="9090"
mysql_server_down_str = "ERROR 2003 (HY000): Can't connect to MySQL server on"
def ReadFile(filename):
array = []
with open(filename, 'rt') as myFile:
for line in myFile:
line_data = line.split('\n')[0]
if line_data[0:2] != "//":
#array.append(line.split('\n')[0])
array.append(line_data)
return array
def runMysqlCmd(cmdStr):
now = time.time()
full_cmd = "mysql -h " + server + " -P " + mysql_port + " -e \"" + cmdStr + "\""
#print full_cmd
proc=Popen(full_cmd, shell=True, stdout=PIPE,stderr=PIPE)
#result=proc.communicate()[0]
result,err=proc.communicate()
proc.wait()
end = time.time()
if mysql_server_down_str in err:
# print "server down";
# raise Exception, "server down"
return -1 , (end - now)
#print result
#return round(end - now)
return 0 , (end - now)
#def runRestCmd(cmdStr):
def runCmdWorker(query_num, queue):
build_in = -1;
q_count = 0;
bq_count = 0;
q_arr = ReadFile(query_filename)
b_arr = ReadFile(build_filename)
bq_arr = ReadFile(build_query_filename)
d_arr = ReadFile(drop_filename)
q_time_avg = 0.0
bq_time_avg = 0.0
# print " :::: " + str(len(bq_arr))
res = 0
build_flag = 0
for q in range(0, int(query_num)):
cmd_type = randint(0,int(query_num) / 4)
#print "cmd_type : " , cmd_type
if cmd_type == 0 and build_flag == 0:
build_flag = 1
#build case
#print "run build statement"
for b_cmd in b_arr:
#print b_cmd
runMysqlCmd(b_cmd)
build_in = q
else:
#query
if build_flag == 1 and len(bq_arr) > 0:
#run query + build query
query_type = randint(1,2)
#print "query_type : " , query_type
if query_type == 1 :
#print "query case"
q_r_index = randint(0, len(q_arr) - 1)
res, runtime = runMysqlCmd(q_arr[q_r_index])
q_time_avg = q_time_avg + runtime
q_count = q_count + 1
else:
#print "build query case"
bq_r_index = randint(0, len(bq_arr) - 1)
res, runtime = runMysqlCmd(bq_arr[bq_r_index])
bq_time_avg = bq_time_avg + runtime
bq_count = bq_count + 1
else:
#run query
#print "query case : before build"
q_r_index = randint(0, len(q_arr) - 1)
res, runtime = runMysqlCmd(q_arr[q_r_index])
q_time_avg = q_time_avg + runtime
q_count = q_count + 1
if res == -1:
break;
for d_cmd in d_arr:
#print "run drop statement"
runMysqlCmd(d_cmd)
total_cmd = q_count + bq_count
try:
total_avg = (q_time_avg + bq_time_avg) / total_cmd
except ZeroDivisionError:
total_avg = 0.0
if q_count != 0:
q_time_avg = q_time_avg / q_count
if bq_count != 0:
bq_time_avg = bq_time_avg / bq_count
queue.put("proc avg time:" + str(total_avg))
return res
def runTest(num_proc, query_num):
print "Simulate concurrent user: {}".format(num_proc)
jobs = []
queue = Queue()
try:
for num in range(num_proc):
p = Process(target=runCmdWorker, args=(query_num, queue))
jobs.append(p)
p.start()
for j in jobs:
j.join()
if j.exitcode != 0:
print '%s.exitcode = %s' % (j.name, j.exitcode)
finally:
total_avg = 0.0
for i in range(queue.qsize()):
result = queue.get()
proc_avg = float(result.split(':')[1])
total_avg = total_avg + proc_avg
#print result
print "average execution time: {:.8f}s".format(total_avg/num_proc)
print
def main(max_proc=1, query_num=10000, continuous=False):
if continuous:
for i in range(1, max_proc + 1):
runTest(i, query_num)
else:
runTest(max_proc, query_num)
if __name__ == "__main__":
clime.Program().main()