author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 22 Dec 2009 13:14:34 +0100 | |
changeset 4183 | b5aa030bb2f9 |
parent 3647 | 2941f4a0aab9 |
child 4252 | 6c4f109c2b03 |
permissions | -rw-r--r-- |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
1 |
""" Usage: %s [OPTIONS] <instance id> <queries file> |
0 | 2 |
|
3 |
Stress test a CubicWeb repository |
|
4 |
||
5 |
OPTIONS: |
|
6 |
-h / --help |
|
7 |
Display this help message and exit. |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
8 |
|
0 | 9 |
-u / --user <user> |
10 |
Connect as <user> instead of being prompted to give it. |
|
11 |
-p / --password <password> |
|
12 |
Automatically give <password> for authentication instead of being prompted |
|
13 |
to give it. |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
14 |
|
0 | 15 |
-n / --nb-times <num> |
16 |
Repeat queries <num> times. |
|
17 |
-t / --nb-threads <num> |
|
18 |
Execute queries in <num> parallel threads. |
|
19 |
-P / --profile <prof_file> |
|
20 |
dumps profile results (hotshot) in <prof_file> |
|
21 |
-o / --report-output <filename> |
|
22 |
Write profiler report into <filename> rather than on stdout |
|
23 |
||
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
24 |
Copyright (c) 2003-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 25 |
http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
26 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 27 |
""" |
28 |
||
29 |
__revision__ = "$Id: stresstester.py,v 1.3 2006-03-05 14:35:27 syt Exp $" |
|
30 |
||
31 |
import os |
|
32 |
import sys |
|
33 |
import threading |
|
34 |
import getopt |
|
35 |
import traceback |
|
36 |
from getpass import getpass |
|
37 |
from os.path import basename |
|
38 |
from time import clock |
|
39 |
||
40 |
from logilab.common.fileutils import lines |
|
41 |
from logilab.common.ureports import Table, TextWriter |
|
42 |
from cubicweb.server.repository import Repository |
|
43 |
from cubicweb.dbapi import Connection |
|
44 |
||
45 |
TB_LOCK = threading.Lock() |
|
46 |
||
47 |
class QueryExecutor: |
|
48 |
def __init__(self, cursor, times, queries, reporter = None): |
|
49 |
self._cursor = cursor |
|
50 |
self._times = times |
|
51 |
self._queries = queries |
|
52 |
self._reporter = reporter |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
53 |
|
0 | 54 |
def run(self): |
55 |
cursor = self._cursor |
|
56 |
times = self._times |
|
57 |
while times: |
|
58 |
for index, query in enumerate(self._queries): |
|
59 |
start = clock() |
|
60 |
try: |
|
61 |
cursor.execute(query) |
|
62 |
except KeyboardInterrupt: |
|
63 |
raise |
|
64 |
except: |
|
65 |
TB_LOCK.acquire() |
|
66 |
traceback.print_exc() |
|
67 |
TB_LOCK.release() |
|
68 |
return |
|
69 |
if self._reporter is not None: |
|
70 |
self._reporter.add_proftime(clock() - start, index) |
|
71 |
times -= 1 |
|
72 |
||
73 |
def usage(status=0): |
|
74 |
"""print usage string and exit""" |
|
75 |
print __doc__ % basename(sys.argv[0]) |
|
76 |
sys.exit(status) |
|
77 |
||
78 |
||
79 |
class ProfileReporter: |
|
80 |
"""a profile reporter gathers all profile informations from several |
|
81 |
threads and can write a report that summarizes all profile informations |
|
82 |
""" |
|
83 |
profiler_lock = threading.Lock() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
84 |
|
0 | 85 |
def __init__(self, queries): |
86 |
self._queries = tuple(queries) |
|
87 |
self._profile_results = [(0., 0)] * len(self._queries) |
|
88 |
# self._table_report = Table(3, rheaders = True) |
|
89 |
len_max = max([len(query) for query in self._queries]) + 5 |
|
90 |
self._query_fmt = '%%%ds' % len_max |
|
91 |
||
92 |
def add_proftime(self, elapsed_time, query_index): |
|
93 |
"""add a new time measure for query""" |
|
94 |
ProfileReporter.profiler_lock.acquire() |
|
95 |
cumul_time, times = self._profile_results[query_index] |
|
96 |
cumul_time += elapsed_time |
|
97 |
times += 1. |
|
98 |
self._profile_results[query_index] = (cumul_time, times) |
|
99 |
ProfileReporter.profiler_lock.release() |
|
100 |
||
101 |
def dump_report(self, output = sys.stdout): |
|
102 |
"""dump report in 'output'""" |
|
103 |
table_elems = ['RQL Query', 'Times', 'Avg Time'] |
|
104 |
total_time = 0. |
|
105 |
for query, (cumul_time, times) in zip(self._queries, self._profile_results): |
|
106 |
avg_time = cumul_time / float(times) |
|
107 |
table_elems += [str(query), '%f' % times, '%f' % avg_time ] |
|
108 |
total_time += cumul_time |
|
109 |
table_elems.append('Total time :') |
|
110 |
table_elems.append(str(total_time)) |
|
111 |
table_elems.append(' ') |
|
112 |
table_layout = Table(3, rheaders = True, children = table_elems) |
|
113 |
TextWriter().format(table_layout, output) |
|
114 |
# output.write('\n'.join(tmp_output)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
115 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
116 |
|
0 | 117 |
def run(args): |
118 |
"""run the command line tool""" |
|
119 |
try: |
|
120 |
opts, args = getopt.getopt(args, 'hn:t:u:p:P:o:', ['help', 'user=', 'password=', |
|
121 |
'nb-times=', 'nb-threads=', |
|
122 |
'profile', 'report-output=',]) |
|
123 |
except Exception, ex: |
|
124 |
print ex |
|
125 |
usage(1) |
|
126 |
repeat = 100 |
|
127 |
threads = 1 |
|
128 |
user = os.environ.get('USER', os.environ.get('LOGNAME')) |
|
129 |
password = None |
|
130 |
report_output = sys.stdout |
|
131 |
prof_file = None |
|
132 |
for opt, val in opts: |
|
133 |
if opt in ('-h', '--help'): |
|
134 |
usage() |
|
135 |
if opt in ('-u', '--user'): |
|
136 |
user = val |
|
137 |
elif opt in ('-p', '--password'): |
|
138 |
password = val |
|
139 |
elif opt in ('-n', '--nb-times'): |
|
140 |
repeat = int(val) |
|
141 |
elif opt in ('-t', '--nb-threads'): |
|
142 |
threads = int(val) |
|
143 |
elif opt in ('-P', '--profile'): |
|
144 |
prof_file = val |
|
145 |
elif opt in ('-o', '--report-output'): |
|
146 |
report_output = file(val, 'w') |
|
147 |
if len(args) != 2: |
|
148 |
usage(1) |
|
149 |
queries = [query for query in lines(args[1]) if not query.startswith('#')] |
|
150 |
if user is None: |
|
151 |
user = raw_input('login: ') |
|
152 |
if password is None: |
|
153 |
password = getpass('password: ') |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
154 |
from cubicweb.cwconfig import instance_configuration |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
155 |
config = instance_configuration(args[0]) |
0 | 156 |
# get local access to the repository |
157 |
print "Creating repo", prof_file |
|
158 |
repo = Repository(config, prof_file) |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
159 |
cnxid = repo.connect(user, password=password) |
0 | 160 |
# connection to the CubicWeb repository |
161 |
repo_cnx = Connection(repo, cnxid) |
|
162 |
repo_cursor = repo_cnx.cursor() |
|
163 |
reporter = ProfileReporter(queries) |
|
164 |
if threads > 1: |
|
165 |
executors = [] |
|
166 |
while threads: |
|
167 |
qe = QueryExecutor(repo_cursor, repeat, queries, reporter = reporter) |
|
168 |
executors.append(qe) |
|
169 |
thread = threading.Thread(target=qe.run) |
|
170 |
qe.thread = thread |
|
171 |
thread.start() |
|
172 |
threads -= 1 |
|
173 |
for qe in executors: |
|
174 |
qe.thread.join() |
|
175 |
## for qe in executors: |
|
176 |
## print qe.thread, repeat - qe._times, 'times' |
|
177 |
else: |
|
178 |
QueryExecutor(repo_cursor, repeat, queries, reporter = reporter).run() |
|
179 |
reporter.dump_report(report_output) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
180 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
181 |
|
0 | 182 |
if __name__ == '__main__': |
183 |
run(sys.argv[1:]) |