devtools/stresstester.py
branchtls-sprint
changeset 1802 d628defebc17
parent 0 b97547f5f1fa
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
     3 Stress test a CubicWeb repository
     3 Stress test a CubicWeb repository
     4 
     4 
     5 OPTIONS:
     5 OPTIONS:
     6   -h / --help
     6   -h / --help
     7      Display this help message and exit.
     7      Display this help message and exit.
     8      
     8 
     9   -u / --user <user>
     9   -u / --user <user>
    10      Connect as <user> instead of being prompted to give it.
    10      Connect as <user> instead of being prompted to give it.
    11   -p / --password <password>
    11   -p / --password <password>
    12      Automatically give <password> for authentication instead of being prompted
    12      Automatically give <password> for authentication instead of being prompted
    13      to give it.
    13      to give it.
    14      
    14 
    15   -n / --nb-times <num>
    15   -n / --nb-times <num>
    16      Repeat queries <num> times.
    16      Repeat queries <num> times.
    17   -t / --nb-threads <num>
    17   -t / --nb-threads <num>
    18      Execute queries in <num> parallel threads.
    18      Execute queries in <num> parallel threads.
    19   -P / --profile <prof_file>
    19   -P / --profile <prof_file>
    20      dumps profile results (hotshot) in <prof_file>
    20      dumps profile results (hotshot) in <prof_file>
    21   -o / --report-output <filename>
    21   -o / --report-output <filename>
    22      Write profiler report into <filename> rather than on stdout
    22      Write profiler report into <filename> rather than on stdout
    23 
    23 
    24 Copyright (c) 2003-2006 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
    24 Copyright (c) 2003-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
    25 http://www.logilab.fr/ -- mailto:contact@logilab.fr
    25 http://www.logilab.fr/ -- mailto:contact@logilab.fr
    26 """
    26 """
    27 
    27 
    28 __revision__ = "$Id: stresstester.py,v 1.3 2006-03-05 14:35:27 syt Exp $"
    28 __revision__ = "$Id: stresstester.py,v 1.3 2006-03-05 14:35:27 syt Exp $"
    29 
    29 
    47     def __init__(self, cursor, times, queries, reporter = None):
    47     def __init__(self, cursor, times, queries, reporter = None):
    48         self._cursor = cursor
    48         self._cursor = cursor
    49         self._times = times
    49         self._times = times
    50         self._queries = queries
    50         self._queries = queries
    51         self._reporter = reporter
    51         self._reporter = reporter
    52         
    52 
    53     def run(self):
    53     def run(self):
    54         cursor = self._cursor
    54         cursor = self._cursor
    55         times = self._times
    55         times = self._times
    56         while times:
    56         while times:
    57             for index, query in enumerate(self._queries):
    57             for index, query in enumerate(self._queries):
    78 class ProfileReporter:
    78 class ProfileReporter:
    79     """a profile reporter gathers all profile informations from several
    79     """a profile reporter gathers all profile informations from several
    80     threads and can write a report that summarizes all profile informations
    80     threads and can write a report that summarizes all profile informations
    81     """
    81     """
    82     profiler_lock = threading.Lock()
    82     profiler_lock = threading.Lock()
    83     
    83 
    84     def __init__(self, queries):
    84     def __init__(self, queries):
    85         self._queries = tuple(queries)
    85         self._queries = tuple(queries)
    86         self._profile_results = [(0., 0)] * len(self._queries)
    86         self._profile_results = [(0., 0)] * len(self._queries)
    87         # self._table_report = Table(3, rheaders = True)
    87         # self._table_report = Table(3, rheaders = True)
    88         len_max = max([len(query) for query in self._queries]) + 5
    88         len_max = max([len(query) for query in self._queries]) + 5
   109         table_elems.append(str(total_time))
   109         table_elems.append(str(total_time))
   110         table_elems.append(' ')
   110         table_elems.append(' ')
   111         table_layout = Table(3, rheaders = True, children = table_elems)
   111         table_layout = Table(3, rheaders = True, children = table_elems)
   112         TextWriter().format(table_layout, output)
   112         TextWriter().format(table_layout, output)
   113         # output.write('\n'.join(tmp_output))
   113         # output.write('\n'.join(tmp_output))
   114         
   114 
   115         
   115 
   116 def run(args):
   116 def run(args):
   117     """run the command line tool"""
   117     """run the command line tool"""
   118     try:
   118     try:
   119         opts, args = getopt.getopt(args, 'hn:t:u:p:P:o:', ['help', 'user=', 'password=',
   119         opts, args = getopt.getopt(args, 'hn:t:u:p:P:o:', ['help', 'user=', 'password=',
   120                                                            'nb-times=', 'nb-threads=',
   120                                                            'nb-times=', 'nb-threads=',
   148     queries =  [query for query in lines(args[1]) if not query.startswith('#')]
   148     queries =  [query for query in lines(args[1]) if not query.startswith('#')]
   149     if user is None:
   149     if user is None:
   150         user = raw_input('login: ')
   150         user = raw_input('login: ')
   151     if password is None:
   151     if password is None:
   152         password = getpass('password: ')
   152         password = getpass('password: ')
   153     from cubicweb.cwconfig import application_configuration 
   153     from cubicweb.cwconfig import application_configuration
   154     config = application_configuration(args[0])
   154     config = application_configuration(args[0])
   155     # get local access to the repository
   155     # get local access to the repository
   156     print "Creating repo", prof_file
   156     print "Creating repo", prof_file
   157     repo = Repository(config, prof_file)
   157     repo = Repository(config, prof_file)
   158     cnxid = repo.connect(user, password)
   158     cnxid = repo.connect(user, password)
   174 ##         for qe in executors:
   174 ##         for qe in executors:
   175 ##             print qe.thread, repeat - qe._times, 'times'
   175 ##             print qe.thread, repeat - qe._times, 'times'
   176     else:
   176     else:
   177         QueryExecutor(repo_cursor, repeat, queries, reporter = reporter).run()
   177         QueryExecutor(repo_cursor, repeat, queries, reporter = reporter).run()
   178     reporter.dump_report(report_output)
   178     reporter.dump_report(report_output)
   179     
   179 
   180     
   180 
   181 if __name__ == '__main__':
   181 if __name__ == '__main__':
   182     run(sys.argv[1:])
   182     run(sys.argv[1:])