28 from optparse
import OptionParser
33 sys.stderr.write(
"* error *\n%s\n" % msg)
34 pp = pprint.PrettyPrinter(indent = 2, stream = sys.stderr)
40 info = {
'host': random.choice(p.targets)}
41 host = info[
'host'][0]
42 port = info[
'host'][1]
45 db = ctx.Db.open(host)
46 c = db.ctx_open(ctx.CTX_USEQL)
48 error(
'ctx_open failed', info, lock)
51 c = ctx.Context.connect(host, port, ctx.CTX_USEQL)
53 error(
'connect failed', info, lock)
57 for i
in xrange(p.n_loop):
62 info[
'ctxinfo'] = c.info_get()
63 error(
'send error: %d' % rc, info, lock)
66 (rc, buf, flags) = c.recv()
69 info[
'ctxinfo'] = c.info_get()
70 error(
'recv error: %d' % rc, info, lock)
72 if not (flags & ctx.CTX_MORE):
76 thd = threading.currentThread()
77 print 'end thread (threadname: %s)' % thd.getName()
80 p = OptionParser(usage =
'%prog [options] dest')
81 p.add_option(
'-p',
'--process', dest =
'n_proc',
82 default = 4, type =
'int',
83 help =
'specify number of processes forked')
84 p.add_option(
'-t',
'--thread', dest =
'n_thd',
85 default = 2, type =
'int',
86 help =
'specify number of threads by one process')
87 p.add_option(
'-l',
'--loop', dest =
'n_loop',
88 default = 10, type =
'int',
89 help =
'specify number of loops on one worker thread')
90 p.add_option(
'',
'--timeout', dest =
'timeout',
91 default = 3, type =
'int',
92 help =
'specify timeout on making context')
93 p.add_option(
'-q',
'--query-file', dest =
'queryfile',
94 help =
'specify query file')
95 p.add_option(
'-v',
'--verbose', dest =
'verbose',
96 action =
'store_true', default =
False,
97 help =
'verbose mode')
101 def check_ctx(targets):
102 import groongactx
as ctx
103 for name, port
in targets:
105 db = ctx.Db.open(name)
107 print 'file: %s cannot be opened with sen_db_open.'
109 c = db.ctx_open(ctx.CTX_USEQL)
111 print 'file: %s sen_ctx_open failed.'
114 c = ctx.Context.connect(name, port, ctx.CTX_USEQL)
116 print 'cannot connect groonga/dicty server(host: %s port: %d)'
122 if not opts.queryfile:
123 print 'please specify query file with -q option.'
125 qf = open(opts.queryfile,
'r')
126 opts.queries = qf.readlines()
130 opts.targets = [x.split(
':')
for x
in args]
131 opts.targets = [(x[0], int(x[1]))
for x
in opts.targets]
133 opts.targets = [(
'localhost', 10041), ]
136 for name, port
in opts.targets:
138 print 'local database file: %s' % name
140 print 'host: %s port: %d' % (name, port)
145 t = threading.Thread(target = check_ctx, args = (opts.targets, ))
150 print 'context timeout.'
154 (pid, st) = os.wait()
155 if os.WEXITSTATUS(st) != 0:
158 print "start %d processs, each process make %d threads." % (
159 opts.n_proc, opts.n_thd)
162 for i
in xrange(opts.n_proc):
165 import groongactx
as ctx
166 lock = threading.Lock()
168 for j
in xrange(opts.n_thd):
169 t = threading.Thread(target = worker, args = (ctx, opts, lock))
177 print 'end process(pid: %d)' % os.getpid()
184 print 'end all processes'
188 threads : %d (%d per process)
189 loops : %d (%d per thread)
190 queries : %d (%d per loop)
193 total time : %f sec""" % (
195 opts.n_proc * opts.n_thd, opts.n_thd,
196 opts.n_proc * opts.n_thd * opts.n_loop, opts.n_loop,
197 opts.n_proc * opts.n_thd * opts.n_loop * len(opts.queries), len(opts.queries),
198 time.strftime(
'%Y/%m/%d %H:%M:%S', time.localtime(stime)),
199 time.strftime(
'%Y/%m/%d %H:%M:%S', time.localtime(etime)),