-
Notifications
You must be signed in to change notification settings - Fork 116
/
stress.py
executable file
·202 lines (184 loc) · 5.86 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# Groonga/Dicty server stress tool
# (c) Brazil 2007-
# Copyright(C) 2007 Brazil
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os
import sys
import time
import random
import pprint
import threading
from optparse import OptionParser
def error(msg, info, lock):
lock.acquire()
try:
sys.stderr.write("* error *\n%s\n" % msg)
pp = pprint.PrettyPrinter(indent = 2, stream = sys.stderr)
pp.pprint(info)
finally:
lock.release()
def worker(ctx, p, lock):
info = {'host': random.choice(p.targets)}
host = info['host'][0]
port = info['host'][1]
if port == 'file':
db = ctx.Db.open(host)
c = db.ctx_open(ctx.CTX_USEQL)
if not c:
error('ctx_open failed', info, lock)
return
else:
c = ctx.Context.connect(host, port, ctx.CTX_USEQL)
if not c:
error('connect failed', info, lock)
return
info['queries'] = 0
for i in xrange(p.n_loop):
for q in p.queries:
info['queries'] += 1
rc = c.send(q, 0)
if rc:
info['ctxinfo'] = c.info_get()
error('send error: %d' % rc, info, lock)
return
while True:
(rc, buf, flags) = c.recv()
# print buf
if rc:
info['ctxinfo'] = c.info_get()
error('recv error: %d' % rc, info, lock)
return
if not (flags & ctx.CTX_MORE):
break
if p.verbose:
thd = threading.currentThread()
print 'end thread (threadname: %s)' % thd.getName()
def parse_opts():
p = OptionParser(usage = '%prog [options] dest')
p.add_option('-p', '--process', dest = 'n_proc',
default = 4, type = 'int',
help = 'specify number of processes forked')
p.add_option('-t', '--thread', dest = 'n_thd',
default = 2, type = 'int',
help = 'specify number of threads by one process')
p.add_option('-l', '--loop', dest = 'n_loop',
default = 10, type = 'int',
help = 'specify number of loops on one worker thread')
p.add_option('', '--timeout', dest = 'timeout',
default = 3, type = 'int',
help = 'specify timeout on making context')
p.add_option('-q', '--query-file', dest = 'queryfile',
help = 'specify query file')
p.add_option('-v', '--verbose', dest = 'verbose',
action = 'store_true', default = False,
help = 'verbose mode')
return p.parse_args()
def main():
def check_ctx(targets):
import groongactx as ctx
for name, port in targets:
if port == 'file':
db = ctx.Db.open(name)
if not db:
print 'file: %s cannot be opened with sen_db_open.'
sys.exit(1)
c = db.ctx_open(ctx.CTX_USEQL)
if not c:
print 'file: %s sen_ctx_open failed.'
sys.exit(1)
else:
c = ctx.Context.connect(name, port, ctx.CTX_USEQL)
if not c:
print 'cannot connect groonga/dicty server(host: %s port: %d)'
sys.exit(1)
(opts, args) = parse_opts()
# check and load queryfile
if not opts.queryfile:
print 'please specify query file with -q option.'
sys.exit(1)
qf = open(opts.queryfile, 'r')
opts.queries = qf.readlines()
qf.close()
if args:
opts.targets = [x.split(':') for x in args]
opts.targets = [(x[0], int(x[1])) for x in opts.targets]
else:
opts.targets = [('localhost', 10041), ]
if opts.verbose:
for name, port in opts.targets:
if port == 'file':
print 'local database file: %s' % name
else:
print 'host: %s port: %d' % (name, port)
# check ctx
pid = os.fork() # avoid import groongactx/sen_init call
if pid == 0:
t = threading.Thread(target = check_ctx, args = (opts.targets, ))
t.setDaemon(True)
t.start()
t.join(3)
if t.isAlive():
print 'context timeout.'
sys.exit(1)
else:
sys.exit(0)
(pid, st) = os.wait()
if os.WEXITSTATUS(st) != 0:
sys.exit(1)
print "start %d processs, each process make %d threads." % (
opts.n_proc, opts.n_thd)
procs = list()
stime = time.time()
for i in xrange(opts.n_proc):
pid = os.fork()
if pid == 0:
import groongactx as ctx # sen_init per process
lock = threading.Lock()
threads = list()
for j in xrange(opts.n_thd):
t = threading.Thread(target = worker, args = (ctx, opts, lock))
t.setDaemon(True)
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
if opts.verbose:
print 'end process(pid: %d)' % os.getpid()
sys.exit(0) # sen_fin per process
else:
procs.append(pid)
for pid in procs:
os.waitpid(pid, 0)
etime = time.time()
print 'end all processes'
print """
processes : %d
threads : %d (%d per process)
loops : %d (%d per thread)
queries : %d (%d per loop)
start time : %s
end time : %s
total time : %f sec""" % (
opts.n_proc,
opts.n_proc * opts.n_thd, opts.n_thd,
opts.n_proc * opts.n_thd * opts.n_loop, opts.n_loop,
opts.n_proc * opts.n_thd * opts.n_loop * len(opts.queries), len(opts.queries),
time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(stime)),
time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(etime)),
etime - stime)
main()