def _test_sql_log_cleared_after_x(self): person = self.factory.makePerson() email = person.preferredemail.email logger = BufferLogger() base = WorkingBase() base.init(email, transaction.manager, logger) with base.statement_logging: self.factory.makeEmail("*****@*****.**", person) self.assertTrue(len(get_request_statements()) > 0, "We need at least one statement in the SQL log.") yield base self.assertTrue( len(get_request_statements()) == 0, "SQL statement log not cleared by WorkingBase.warning()." )
def _test_sql_log_cleared_after_x(self): person = self.factory.makePerson() email = person.preferredemail.email logger = BufferLogger() base = WorkingBase() base.init(email, transaction.manager, logger) with base.statement_logging: self.factory.makeEmail('*****@*****.**', person) self.assertTrue( len(get_request_statements()) > 0, "We need at least one statement in the SQL log.") yield base self.assertTrue( len(get_request_statements()) == 0, "SQL statement log not cleared by WorkingBase.warning().")
def test_statement_logging(self): # The WorkingBase.statement_logging context manager starts # statement logging on entry and stops it on exit. base = WorkingBase() base.init(self.email, transaction.manager, self.logger) self.factory.makeEmail("*****@*****.**", self.person) self.assertEqual( 0, len(get_request_statements()), "The statement log should be empty because " "logging is not enabled." ) with base.statement_logging: self.assertEqual(0, len(get_request_statements()), "There should be no statements in the log yet.") self.factory.makeEmail("*****@*****.**", self.person) self.assertTrue(len(get_request_statements()) > 0, "There should be at least one statement in the log.") self.assertEqual( 0, len(get_request_statements()), "SQL statement log not cleared on exit " "from base.statement_logging." )
def afterCall(self, request, ob): """See `zope.publisher.interfaces.IPublication`. Our implementation calls self.finishReadOnlyRequest(), which by default aborts the transaction, for read-only requests. Because of this we cannot chain to the superclass and implement the whole behaviour here. """ assert hasattr(request, '_publicationticks_start'), ( 'request._publicationticks_start, which should have been set by ' 'callObject(), was not found.') ticks = tickcount.difference(request._publicationticks_start, tickcount.tickcount()) request.setInWSGIEnvironment('launchpad.publicationticks', ticks) # Calculate SQL statement statistics. sql_statements = da.get_request_statements() sql_milliseconds = sum( endtime - starttime for starttime, endtime, id, statement, tb in sql_statements) # Log publication tickcount, sql statement count, and sql time # to the tracelog. tracelog(request, 't', '%d %d %d' % (ticks, len(sql_statements), sql_milliseconds)) # Annotate the transaction with user data. That was done by # zope.app.publication.zopepublication.ZopePublication. txn = transaction.get() self.annotateTransaction(txn, request, ob) # Abort the transaction on a read-only request. # NOTHING AFTER THIS SHOULD CAUSE A RETRY. if request.method in ['GET', 'HEAD']: self.finishReadOnlyRequest(request, ob, txn) elif txn.isDoomed(): # The following sends an abort to the database, even though the # transaction is still doomed. txn.abort() else: txn.commit() # Don't render any content for a HEAD. This was done # by zope.app.publication.browser.BrowserPublication if request.method == 'HEAD': request.response.setResult('') try: getUtility(IStoreSelector).pop() except IndexError: # We have to cope with no database policy being installed # to allow doc/webapp-publication.txt tests to pass. These # tests rely on calling the afterCall hook without first # calling beforeTraversal or doing proper cleanup. pass
def afterCall(self, request, ob): """See `zope.publisher.interfaces.IPublication`. Our implementation calls self.finishReadOnlyRequest(), which by default aborts the transaction, for read-only requests. Because of this we cannot chain to the superclass and implement the whole behaviour here. """ assert hasattr(request, '_publicationticks_start'), ( 'request._publicationticks_start, which should have been set by ' 'callObject(), was not found.') ticks = tickcount.difference( request._publicationticks_start, tickcount.tickcount()) request.setInWSGIEnvironment('launchpad.publicationticks', ticks) # Calculate SQL statement statistics. sql_statements = da.get_request_statements() sql_milliseconds = sum( endtime - starttime for starttime, endtime, id, statement, tb in sql_statements) # Log publication tickcount, sql statement count, and sql time # to the tracelog. tracelog(request, 't', '%d %d %d' % ( ticks, len(sql_statements), sql_milliseconds)) # Annotate the transaction with user data. That was done by # zope.app.publication.zopepublication.ZopePublication. txn = transaction.get() self.annotateTransaction(txn, request, ob) # Abort the transaction on a read-only request. # NOTHING AFTER THIS SHOULD CAUSE A RETRY. if request.method in ['GET', 'HEAD']: self.finishReadOnlyRequest(request, ob, txn) elif txn.isDoomed(): # The following sends an abort to the database, even though the # transaction is still doomed. txn.abort() else: txn.commit() # Don't render any content for a HEAD. This was done # by zope.app.publication.browser.BrowserPublication if request.method == 'HEAD': request.response.setResult('') try: getUtility(IStoreSelector).pop() except IndexError: # We have to cope with no database policy being installed # to allow doc/webapp-publication.txt tests to pass. These # tests rely on calling the afterCall hook without first # calling beforeTraversal or doing proper cleanup. pass
def test_statement_logging(self): # The WorkingBase.statement_logging context manager starts # statement logging on entry and stops it on exit. base = WorkingBase() base.init(self.email, transaction.manager, self.logger) self.factory.makeEmail('*****@*****.**', self.person) self.assertEqual( 0, len(get_request_statements()), "The statement log should be empty because " "logging is not enabled.") with base.statement_logging: self.assertEqual(0, len(get_request_statements()), "There should be no statements in the log yet.") self.factory.makeEmail('*****@*****.**', self.person) self.assertTrue( len(get_request_statements()) > 0, "There should be at least one statement in the log.") self.assertEqual( 0, len(get_request_statements()), "SQL statement log not cleared on exit " "from base.statement_logging.")
def __call__(self, event): if self._active: self.queries = get_request_statements() self.count = len(self.queries)