~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/profile/tests.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-23 21:54:33 UTC
  • mfrom: (13723.1.7 sqlprofiler)
  • Revision ID: launchpad@pqm.canonical.com-20110823215433-77gtrsn5v1coarbl
[r=gmb][bug=832208, 832215] Add ++profile++sqltrace,
        and extended frame information to LP_DEBUG_SQL_EXTRA.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
import glob
13
13
import os
14
14
import random
15
 
import time
16
15
 
17
 
from lp.testing import TestCase
18
16
from zope.app.publication.interfaces import (
19
17
    BeforeTraverseEvent,
20
18
    EndRequestEvent,
22
20
from zope.component import getSiteManager
23
21
 
24
22
import canonical.launchpad.webapp.adapter as da
25
 
from canonical.launchpad.webapp.errorlog import (
26
 
    ErrorReport,
27
 
    ErrorReportingUtility,
28
 
    )
 
23
from canonical.launchpad.webapp.errorlog import ErrorReportingUtility
29
24
from canonical.launchpad.webapp.servers import LaunchpadTestRequest
30
25
from canonical.launchpad.webapp.interfaces import StartRequestEvent
31
26
from canonical.testing import layers
32
27
from lp.services.features.testing import FeatureFixture
33
28
from lp.services.profile import profile
 
29
from lp.testing import (
 
30
    TestCase,
 
31
    TestCaseWithFactory)
34
32
 
35
33
EXAMPLE_HTML_START = '''\
36
34
<html><head><title>Random!</title></head>
84
82
        profile._profilers.actions = None
85
83
        profile._profilers.memory_profile_start = None
86
84
        profile._profilers.profiling = False
 
85
        da.stop_sql_traceback_logging()
87
86
        super(TestCleanupProfiler, self).tearDown()
88
87
 
89
88
 
271
270
        self.assertEqual(len(profile._profilers.memory_profile_start), 2)
272
271
        self.assertEquals(profile._profilers.actions, set(('show', )))
273
272
 
 
273
    def test_sqltrace_start(self):
 
274
        self.pushProfilingConfig(profiling_allowed='True')
 
275
        profile.start_request(self._get_start_event('/++profile++sqltrace/'))
 
276
        self.assertIs(getattr(profile._profilers, 'profiler', None), None)
 
277
        self.assertEquals(profile._profilers.actions, set(('sqltrace', )))
 
278
        self.assertEqual([], da.stop_sql_traceback_logging())
 
279
 
274
280
 
275
281
class BaseRequestEndHandlerTest(BaseTest):
276
282
 
277
283
    def setUp(self):
278
 
        TestCase.setUp(self)
279
 
        self.patch(da, 'get_request_start_time', time.time)
280
 
        self.patch(da, 'get_request_duration', lambda: 0.5)
 
284
        super(BaseRequestEndHandlerTest, self).setUp()
281
285
        self.profile_dir = self.makeTemporaryDirectory()
282
286
        self.memory_profile_log = os.path.join(self.profile_dir, 'memory_log')
283
287
        self.pushConfig('profiling', profile_dir=self.profile_dir)
288
292
 
289
293
    def endRequest(self, path='/', exception=None, pageid=None, work=None):
290
294
        start_event = self._get_start_event(path)
 
295
        da.set_request_started()
291
296
        profile.start_request(start_event)
292
297
        request = start_event.request
293
298
        if pageid is not None:
301
306
            self.eru.raising(
302
307
                (type(exception), exception, None), event.request)
303
308
        profile.end_request(event)
 
309
        da.clear_request_started()
304
310
        return event.request
305
311
 
306
312
    def getAddedResponse(self, request,
519
525
 
520
526
    def test_memory_profile(self):
521
527
        # Does the memory profile work?
 
528
        self.patch(da, 'get_request_duration', lambda: 0.5)
522
529
        self.pushProfilingConfig(
523
530
            profiling_allowed='True',
524
531
            memory_profile_log=self.memory_profile_log)
650
657
        request = self.endRequest('/', work=work)
651
658
        self.assertPStatsProfile(
652
659
            self.assertBasicProfileExists(request, show=True))
 
660
 
 
661
 
 
662
class TestSqlTrace(TestCaseWithFactory, BaseRequestEndHandlerTest):
 
663
 
 
664
    layer = layers.DatabaseFunctionalLayer
 
665
 
 
666
    def testLogging(self):
 
667
        # This is basically a smoketest.  The underlying machinery has tests.
 
668
        self.pushProfilingConfig(profiling_allowed='True')
 
669
        request = self.endRequest(
 
670
            '/++profile++sqltrace/', work=self.factory.makeBug)
 
671
        response = self.getAddedResponse(request)
 
672
        self.assertIn('Top 10 SQL times', response)
 
673
        self.assertIn('Query number', response)
 
674
        self.assertIn('Top 10 Python times', response)
 
675
        self.assertIn('Before query', response)
 
676
        self.assertIn('Repeated Python SQL Triggers', response)
 
677
        self.assertIn('Show all tracebacks', response)
 
678
        # This file should be part of several of the tracebacks.
 
679
        self.assertIn(__file__.replace('.pyc', '.py'), response)