cubicweb/devtools/stresstester.py
changeset 12037 88bf0ec257b2
parent 11983 5de78b6fff2e
child 12567 26744ad37953
equal deleted inserted replaced
12036:4c2c731f9190 12037:88bf0ec257b2
    52 from os.path import basename
    52 from os.path import basename
    53 from time import clock
    53 from time import clock
    54 
    54 
    55 from logilab.common.fileutils import lines
    55 from logilab.common.fileutils import lines
    56 from logilab.common.ureports import Table, TextWriter
    56 from logilab.common.ureports import Table, TextWriter
       
    57 
       
    58 from cubicweb import repoapi
    57 from cubicweb.server.repository import Repository
    59 from cubicweb.server.repository import Repository
    58 
    60 
    59 TB_LOCK = threading.Lock()
    61 TB_LOCK = threading.Lock()
    60 
    62 
       
    63 
    61 class QueryExecutor:
    64 class QueryExecutor:
    62     def __init__(self, session, times, queries, reporter = None):
    65 
    63         self._session = session
    66     def __init__(self, cnx, times, queries, reporter = None):
       
    67         self._cnx = cnx
    64         self._times = times
    68         self._times = times
    65         self._queries = queries
    69         self._queries = queries
    66         self._reporter = reporter
    70         self._reporter = reporter
    67 
    71 
    68     def run(self):
    72     def run(self):
    69         with self._session.new_cnx() as cnx:
    73         with self._cnx as cnx:
    70             times = self._times
    74             times = self._times
    71             while times:
    75             while times:
    72                 for index, query in enumerate(self._queries):
    76                 for index, query in enumerate(self._queries):
    73                     start = clock()
    77                     start = clock()
    74                     try:
    78                     try:
   167     config = instance_configuration(args[0])
   171     config = instance_configuration(args[0])
   168     # get local access to the repository
   172     # get local access to the repository
   169     print("Creating repo", prof_file)
   173     print("Creating repo", prof_file)
   170     repo = Repository(config, prof_file)
   174     repo = Repository(config, prof_file)
   171     repo.bootstrap()
   175     repo.bootstrap()
   172     session = repo.new_session(user, password=password)
   176     cnx = repoapi.connect(repo, user, password=password)
   173     reporter = ProfileReporter(queries)
   177     reporter = ProfileReporter(queries)
   174     if threads > 1:
   178     if threads > 1:
   175         executors = []
   179         executors = []
   176         while threads:
   180         while threads:
   177             qe = QueryExecutor(session, repeat, queries, reporter = reporter)
   181             qe = QueryExecutor(cnx, repeat, queries, reporter=reporter)
   178             executors.append(qe)
   182             executors.append(qe)
   179             thread = threading.Thread(target=qe.run)
   183             thread = threading.Thread(target=qe.run)
   180             qe.thread = thread
   184             qe.thread = thread
   181             thread.start()
   185             thread.start()
   182             threads -= 1
   186             threads -= 1
   183         for qe in executors:
   187         for qe in executors:
   184             qe.thread.join()
   188             qe.thread.join()
   185 ##         for qe in executors:
   189 ##         for qe in executors:
   186 ##             print qe.thread, repeat - qe._times, 'times'
   190 ##             print qe.thread, repeat - qe._times, 'times'
   187     else:
   191     else:
   188         QueryExecutor(session, repeat, queries, reporter = reporter).run()
   192         QueryExecutor(cnx, repeat, queries, reporter=reporter).run()
   189     reporter.dump_report(report_output)
   193     reporter.dump_report(report_output)
   190 
   194 
   191 
   195 
   192 if __name__ == '__main__':
   196 if __name__ == '__main__':
   193     run(sys.argv[1:])
   197     run(sys.argv[1:])