~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Robert Collins
  • Date: 2006-05-24 08:04:06 UTC
  • mto: (1728.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1729.
  • Revision ID: robertc@robertcollins.net-20060524080406-df923c07fbf32e00
'bzr selftest --benchmark --lsprof-timed' will use lsprofile to generate
profile data for the individual profiled calls, allowing for fine
grained analysis of performance.
(Robert Collins, Martin Pool).

Show diffs side-by-side

added added

removed removed

Lines of Context:
395
395
        self.calls.append(('clear',))
396
396
 
397
397
 
398
 
class TestResult(TestCase):
 
398
class TestTestResult(TestCase):
399
399
 
400
400
    def test_progress_bar_style_quiet(self):
401
401
        # test using a progress bar.
468
468
        # cheat. Yes, wash thy mouth out with soap.
469
469
        self._benchtime = None
470
470
 
 
471
    def _time_hello_world_encoding(self):
 
472
        """Profile two sleep calls
 
473
        
 
474
        This is used to exercise the test framework.
 
475
        """
 
476
        self.time(unicode, 'hello', errors='replace')
 
477
        self.time(unicode, 'world', errors='replace')
 
478
 
 
479
    def test_lsprofiling(self):
 
480
        """Verbose test result prints lsprof statistics from test cases."""
 
481
        try:
 
482
            import bzrlib.lsprof
 
483
        except ImportError:
 
484
            raise TestSkipped("lsprof not installed.")
 
485
        result_stream = StringIO()
 
486
        result = bzrlib.tests._MyResult(
 
487
            unittest._WritelnDecorator(result_stream),
 
488
            descriptions=0,
 
489
            verbosity=2,
 
490
            )
 
491
        # we want profile a call of some sort and check it is output by
 
492
        # addSuccess. We dont care about addError or addFailure as they
 
493
        # are not that interesting for performance tuning.
 
494
        # make a new test instance that when run will generate a profile
 
495
        example_test_case = TestTestResult("_time_hello_world_encoding")
 
496
        example_test_case._gather_lsprof_in_benchmarks = True
 
497
        # execute the test, which should succeed and record profiles
 
498
        example_test_case.run(result)
 
499
        # lsprofile_something()
 
500
        # if this worked we want 
 
501
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
 
502
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
 
503
        # (the lsprof header)
 
504
        # ... an arbitrary number of lines
 
505
        # and the function call which is time.sleep.
 
506
        #           1        0            ???         ???       ???(sleep) 
 
507
        # and then repeated but with 'world', rather than 'hello'.
 
508
        # this should appear in the output stream of our test result.
 
509
        self.assertContainsRe(result_stream.getvalue(), 
 
510
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)\n"
 
511
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n"
 
512
            r" +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n"
 
513
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n"
 
514
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n"
 
515
            r" +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n"
 
516
            )
 
517
 
471
518
 
472
519
class TestRunner(TestCase):
473
520
 
571
618
            output_stream.getvalue(),
572
619
            "[1-9][0-9]ms/   [1-9][0-9]ms\n$")
573
620
        
 
621
    def test__gather_lsprof_in_benchmarks(self):
 
622
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
 
623
        
 
624
        Each self.time() call is individually and separately profiled.
 
625
        """
 
626
        try:
 
627
            import bzrlib.lsprof
 
628
        except ImportError:
 
629
            raise TestSkipped("lsprof not installed.")
 
630
        # overrides the class member with an instance member so no cleanup 
 
631
        # needed.
 
632
        self._gather_lsprof_in_benchmarks = True
 
633
        self.time(time.sleep, 0.000)
 
634
        self.time(time.sleep, 0.003)
 
635
        self.assertEqual(2, len(self._benchcalls))
 
636
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
 
637
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
 
638
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
 
639
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
 
640
 
574
641
 
575
642
class TestExtraAssertions(TestCase):
576
643
    """Tests for new test assertions in bzrlib test suite"""