~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
import bzrlib.inventory
48
48
import bzrlib.iterablefile
49
49
import bzrlib.lockdir
 
50
try:
 
51
    import bzrlib.lsprof
 
52
except ImportError:
 
53
    # lsprof not available
 
54
    pass
50
55
from bzrlib.merge import merge_inner
51
56
import bzrlib.merge3
52
57
import bzrlib.osutils
223
228
        self.extractBenchmarkTime(test)
224
229
        if self.showAll:
225
230
            self.stream.writeln('   OK %s' % self._testTimeString())
 
231
            for bench_called, stats in test._benchcalls:
 
232
                self.stream.writeln('LSProf output for %s(%s, %s)' % bench_called)
 
233
                stats.pprint(file=self.stream)
226
234
        elif self.dots and self.pb is None:
227
235
            self.stream.write('~')
228
236
        elif self.dots:
375
383
 
376
384
    _log_file_name = None
377
385
    _log_contents = ''
 
386
    # record lsprof data when performing benchmark calls.
 
387
    _gather_lsprof_in_benchmarks = False
378
388
 
379
389
    def __init__(self, methodName='testMethod'):
380
390
        super(TestCase, self).__init__(methodName)
385
395
        self._cleanEnvironment()
386
396
        bzrlib.trace.disable_default_logging()
387
397
        self._startLogFile()
 
398
        self._benchcalls = []
388
399
        self._benchtime = None
389
400
 
390
401
    def _ndiff_strings(self, a, b):
541
552
        unittest.TestCase.tearDown(self)
542
553
 
543
554
    def time(self, callable, *args, **kwargs):
544
 
        """Run callable and accrue the time it takes to the benchmark time."""
 
555
        """Run callable and accrue the time it takes to the benchmark time.
 
556
        
 
557
        If lsprofiling is enabled (i.e. by --lsprof-time to bzr selftest) then
 
558
        this will cause lsprofile statistics to be gathered and stored in
 
559
        self._benchcalls.
 
560
        """
545
561
        if self._benchtime is None:
546
562
            self._benchtime = 0
547
563
        start = time.time()
548
564
        try:
549
 
            callable(*args, **kwargs)
 
565
            if not self._gather_lsprof_in_benchmarks:
 
566
                return callable(*args, **kwargs)
 
567
            else:
 
568
                # record this benchmark
 
569
                ret, stats = bzrlib.lsprof.profile(callable, *args, **kwargs)
 
570
                stats.sort()
 
571
                self._benchcalls.append(((callable, args, kwargs), stats))
 
572
                return ret
550
573
        finally:
551
574
            self._benchtime += time.time() - start
552
575
 
1031
1054
 
1032
1055
def run_suite(suite, name='test', verbose=False, pattern=".*",
1033
1056
              stop_on_failure=False, keep_output=False,
1034
 
              transport=None):
 
1057
              transport=None, lsprof_timed=None):
1035
1058
    TestCaseInTempDir._TEST_NAME = name
 
1059
    TestCase._gather_lsprof_in_benchmarks = lsprof_timed
1036
1060
    if verbose:
1037
1061
        verbosity = 2
1038
1062
        pb = None
1054
1078
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
1055
1079
             keep_output=False,
1056
1080
             transport=None,
1057
 
             test_suite_factory=None):
 
1081
             test_suite_factory=None,
 
1082
             lsprof_timed=None):
1058
1083
    """Run the whole test suite under the enhanced runner"""
1059
1084
    global default_transport
1060
1085
    if transport is None:
1068
1093
            suite = test_suite_factory()
1069
1094
        return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
1070
1095
                     stop_on_failure=stop_on_failure, keep_output=keep_output,
1071
 
                     transport=transport)
 
1096
                     transport=transport,
 
1097
                     lsprof_timed=lsprof_timed)
1072
1098
    finally:
1073
1099
        default_transport = old_transport
1074
1100