author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 25 Oct 2010 15:31:29 +0200 | |
changeset 6622 | 27402fe6a94a |
parent 5424 | 8ecbcbff9777 |
child 7815 | 2a164a9cf81c |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
18 |
""" Usage: %s [OPTIONS] <instance id> <queries file> |
0 | 19 |
|
20 |
Stress test a CubicWeb repository |
|
21 |
||
22 |
OPTIONS: |
|
23 |
-h / --help |
|
24 |
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
|
25 |
|
0 | 26 |
-u / --user <user> |
27 |
Connect as <user> instead of being prompted to give it. |
|
28 |
-p / --password <password> |
|
29 |
Automatically give <password> for authentication instead of being prompted |
|
30 |
to give it. |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
31 |
|
0 | 32 |
-n / --nb-times <num> |
33 |
Repeat queries <num> times. |
|
34 |
-t / --nb-threads <num> |
|
35 |
Execute queries in <num> parallel threads. |
|
36 |
-P / --profile <prof_file> |
|
37 |
dumps profile results (hotshot) in <prof_file> |
|
38 |
-o / --report-output <filename> |
|
39 |
Write profiler report into <filename> rather than on stdout |
|
40 |
||
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
41 |
Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 42 |
http://www.logilab.fr/ -- mailto:contact@logilab.fr |
43 |
""" |
|
44 |
||
45 |
import os |
|
46 |
import sys |
|
47 |
import threading |
|
48 |
import getopt |
|
49 |
import traceback |
|
50 |
from getpass import getpass |
|
51 |
from os.path import basename |
|
52 |
from time import clock |
|
53 |
||
54 |
from logilab.common.fileutils import lines |
|
55 |
from logilab.common.ureports import Table, TextWriter |
|
56 |
from cubicweb.server.repository import Repository |
|
57 |
from cubicweb.dbapi import Connection |
|
58 |
||
59 |
TB_LOCK = threading.Lock() |
|
60 |
||
61 |
class QueryExecutor: |
|
62 |
def __init__(self, cursor, times, queries, reporter = None): |
|
63 |
self._cursor = cursor |
|
64 |
self._times = times |
|
65 |
self._queries = queries |
|
66 |
self._reporter = reporter |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
67 |
|
0 | 68 |
def run(self): |
69 |
cursor = self._cursor |
|
70 |
times = self._times |
|
71 |
while times: |
|
72 |
for index, query in enumerate(self._queries): |
|
73 |
start = clock() |
|
74 |
try: |
|
75 |
cursor.execute(query) |
|
76 |
except KeyboardInterrupt: |
|
77 |
raise |
|
78 |
except: |
|
79 |
TB_LOCK.acquire() |
|
80 |
traceback.print_exc() |
|
81 |
TB_LOCK.release() |
|
82 |
return |
|
83 |
if self._reporter is not None: |
|
84 |
self._reporter.add_proftime(clock() - start, index) |
|
85 |
times -= 1 |
|
86 |
||
87 |
def usage(status=0): |
|
88 |
"""print usage string and exit""" |
|
89 |
print __doc__ % basename(sys.argv[0]) |
|
90 |
sys.exit(status) |
|
91 |
||
92 |
||
93 |
class ProfileReporter: |
|
94 |
"""a profile reporter gathers all profile informations from several |
|
95 |
threads and can write a report that summarizes all profile informations |
|
96 |
""" |
|
97 |
profiler_lock = threading.Lock() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
98 |
|
0 | 99 |
def __init__(self, queries): |
100 |
self._queries = tuple(queries) |
|
101 |
self._profile_results = [(0., 0)] * len(self._queries) |
|
102 |
# self._table_report = Table(3, rheaders = True) |
|
103 |
len_max = max([len(query) for query in self._queries]) + 5 |
|
104 |
self._query_fmt = '%%%ds' % len_max |
|
105 |
||
106 |
def add_proftime(self, elapsed_time, query_index): |
|
107 |
"""add a new time measure for query""" |
|
108 |
ProfileReporter.profiler_lock.acquire() |
|
109 |
cumul_time, times = self._profile_results[query_index] |
|
110 |
cumul_time += elapsed_time |
|
111 |
times += 1. |
|
112 |
self._profile_results[query_index] = (cumul_time, times) |
|
113 |
ProfileReporter.profiler_lock.release() |
|
114 |
||
115 |
def dump_report(self, output = sys.stdout): |
|
116 |
"""dump report in 'output'""" |
|
117 |
table_elems = ['RQL Query', 'Times', 'Avg Time'] |
|
118 |
total_time = 0. |
|
119 |
for query, (cumul_time, times) in zip(self._queries, self._profile_results): |
|
120 |
avg_time = cumul_time / float(times) |
|
121 |
table_elems += [str(query), '%f' % times, '%f' % avg_time ] |
|
122 |
total_time += cumul_time |
|
123 |
table_elems.append('Total time :') |
|
124 |
table_elems.append(str(total_time)) |
|
125 |
table_elems.append(' ') |
|
126 |
table_layout = Table(3, rheaders = True, children = table_elems) |
|
127 |
TextWriter().format(table_layout, output) |
|
128 |
# 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
|
129 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
130 |
|
0 | 131 |
def run(args): |
132 |
"""run the command line tool""" |
|
133 |
try: |
|
134 |
opts, args = getopt.getopt(args, 'hn:t:u:p:P:o:', ['help', 'user=', 'password=', |
|
135 |
'nb-times=', 'nb-threads=', |
|
136 |
'profile', 'report-output=',]) |
|
137 |
except Exception, ex: |
|
138 |
print ex |
|
139 |
usage(1) |
|
140 |
repeat = 100 |
|
141 |
threads = 1 |
|
142 |
user = os.environ.get('USER', os.environ.get('LOGNAME')) |
|
143 |
password = None |
|
144 |
report_output = sys.stdout |
|
145 |
prof_file = None |
|
146 |
for opt, val in opts: |
|
147 |
if opt in ('-h', '--help'): |
|
148 |
usage() |
|
149 |
if opt in ('-u', '--user'): |
|
150 |
user = val |
|
151 |
elif opt in ('-p', '--password'): |
|
152 |
password = val |
|
153 |
elif opt in ('-n', '--nb-times'): |
|
154 |
repeat = int(val) |
|
155 |
elif opt in ('-t', '--nb-threads'): |
|
156 |
threads = int(val) |
|
157 |
elif opt in ('-P', '--profile'): |
|
158 |
prof_file = val |
|
159 |
elif opt in ('-o', '--report-output'): |
|
160 |
report_output = file(val, 'w') |
|
161 |
if len(args) != 2: |
|
162 |
usage(1) |
|
163 |
queries = [query for query in lines(args[1]) if not query.startswith('#')] |
|
164 |
if user is None: |
|
165 |
user = raw_input('login: ') |
|
166 |
if password is None: |
|
167 |
password = getpass('password: ') |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
168 |
from cubicweb.cwconfig import instance_configuration |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
169 |
config = instance_configuration(args[0]) |
0 | 170 |
# get local access to the repository |
171 |
print "Creating repo", prof_file |
|
172 |
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
|
173 |
cnxid = repo.connect(user, password=password) |
0 | 174 |
# connection to the CubicWeb repository |
175 |
repo_cnx = Connection(repo, cnxid) |
|
176 |
repo_cursor = repo_cnx.cursor() |
|
177 |
reporter = ProfileReporter(queries) |
|
178 |
if threads > 1: |
|
179 |
executors = [] |
|
180 |
while threads: |
|
181 |
qe = QueryExecutor(repo_cursor, repeat, queries, reporter = reporter) |
|
182 |
executors.append(qe) |
|
183 |
thread = threading.Thread(target=qe.run) |
|
184 |
qe.thread = thread |
|
185 |
thread.start() |
|
186 |
threads -= 1 |
|
187 |
for qe in executors: |
|
188 |
qe.thread.join() |
|
189 |
## for qe in executors: |
|
190 |
## print qe.thread, repeat - qe._times, 'times' |
|
191 |
else: |
|
192 |
QueryExecutor(repo_cursor, repeat, queries, reporter = reporter).run() |
|
193 |
reporter.dump_report(report_output) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
194 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
195 |
|
0 | 196 |
if __name__ == '__main__': |
197 |
run(sys.argv[1:]) |