PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Misc/tsc_stats_test.py

http://unladen-swallow.googlecode.com/
Python | 131 lines | 117 code | 11 blank | 3 comment | 0 complexity | c2691677486ecb612aaa5be617c3a0b3 MD5 | raw file
  1#!/usr/bin/env python
  2
  3from __future__ import with_statement
  4
  5import StringIO
  6import unittest
  7import warnings
  8
  9import tsc_stats
 10
 11
 12class TscStatsTest(unittest.TestCase):
 13
 14    def testMedianOdd(self):
 15        # Note: median requires that its input be sorted.
 16        xs = [1, 2, 3, 4, 5]
 17        self.assertEqual(tsc_stats.median(xs), 3)
 18
 19    def testMedianEven(self):
 20        # Note: median requires that its input be sorted.
 21        xs = [1, 2, 3, 4, 5, 6]
 22        self.assertAlmostEqual(tsc_stats.median(xs), 3.5, 0.01)
 23
 24    def testMean(self):
 25        xs = [1, 2, 3]
 26        self.assertAlmostEqual(tsc_stats.mean(xs), 2.0, 0.01)
 27
 28    def testStdDevNoVariance(self):
 29        xs = [2, 2, 2]
 30        self.assertAlmostEqual(tsc_stats.stddev(xs), 0, 0.01)
 31
 32    def testStdDev(self):
 33        xs = [1, 2, 3]
 34        expected = (1 + 0 + 1) / 2
 35        self.assertAlmostEqual(tsc_stats.stddev(xs), expected, 0.01)
 36
 37    def testAnalyzerSimpleCalls(self):
 38        input = StringIO.StringIO("""\
 390	CALL_START_EVAL	42602597860025
 400	CALL_ENTER_C	42602597871713
 410	CALL_START_EVAL	42602597880984
 420	CALL_ENTER_PYOBJ_CALL	42602597883307
 430	CALL_START_EVAL	42602597894126
 440	CALL_ENTER_EVAL	42602597898375
 450	CALL_START_EVAL	42602597902292
 460	CALL_ENTER_C	42602597902916
 470	LOAD_GLOBAL_ENTER_EVAL	42602633110000
 480	LOAD_GLOBAL_EXIT_EVAL	42602633111000
 49""")
 50        analyzer = tsc_stats.TimeAnalyzer(input)
 51        analyzer.analyze()
 52        call_delta_dict = {
 53            ('CALL_START_EVAL', 'CALL_ENTER_C'):
 54                [42602597871713 - 42602597860025,
 55                 42602597902916 - 42602597902292],
 56            ('CALL_START_EVAL', 'CALL_ENTER_PYOBJ_CALL'):
 57                [42602597883307 - 42602597880984],
 58            ('CALL_START_EVAL', 'CALL_ENTER_EVAL'):
 59                [42602597898375 - 42602597894126],
 60        }
 61        global_delta_dict = {
 62            ('LOAD_GLOBAL_ENTER_EVAL', 'LOAD_GLOBAL_EXIT_EVAL'):
 63                [1000],
 64        }
 65        self.assertEqual(analyzer.call_stats.delta_dict, call_delta_dict)
 66        self.assertEqual(analyzer.global_stats.delta_dict, global_delta_dict)
 67
 68    def testAnalyzerJittedCall(self):
 69        input = StringIO.StringIO("""\
 700	CALL_START_EVAL	0
 710	EVAL_COMPILE_START	10
 720	LLVM_COMPILE_START	15
 730	LLVM_COMPILE_END	35
 740	JIT_START	40
 750	JIT_END	95
 760	EVAL_COMPILE_END	100
 770	CALL_ENTER_LLVM	200
 780	CALL_START_LLVM	300
 790	CALL_ENTER_EVAL	400
 80""")
 81        analyzer = tsc_stats.TimeAnalyzer(input)
 82        analyzer.analyze()
 83        eval_compile_time = 90
 84        call_delta_dict = {
 85            ('CALL_START_EVAL', 'CALL_ENTER_LLVM'):
 86                [200 - eval_compile_time],
 87            ('CALL_START_LLVM', 'CALL_ENTER_EVAL'):
 88                [100],
 89        }
 90        self.assertEqual(analyzer.call_stats.delta_dict, call_delta_dict)
 91        self.assertEqual(analyzer.llvm_stats.aggregate_deltas, [20])
 92        self.assertEqual(analyzer.native_stats.aggregate_deltas, [55])
 93        self.assertEqual(analyzer.eval_compile_stats.aggregate_deltas,
 94                         [eval_compile_time])
 95
 96    def testAnalyzerFlushFudge(self):
 97        input = StringIO.StringIO("""\
 980	CALL_START_EVAL	0
 990	EVAL_COMPILE_START	10
1000	LLVM_COMPILE_START	15
1010	LLVM_COMPILE_END	35
1020	JIT_START	40
1030	FLUSH_START	41
1040	FLUSH_END	20041
1050	JIT_END	20095
1060	EVAL_COMPILE_END	20100
1070	CALL_ENTER_LLVM	20200
1080	CALL_START_LLVM	20300
1090	CALL_ENTER_EVAL	20400
110""")
111        analyzer = tsc_stats.TimeAnalyzer(input)
112        analyzer.analyze()
113        eval_compile_time = 90
114        flush_time = 20000
115        call_delta_dict = {
116            ('CALL_START_EVAL', 'CALL_ENTER_LLVM'): [200 - eval_compile_time],
117            ('CALL_START_LLVM', 'CALL_ENTER_EVAL'): [100],
118        }
119        self.assertEqual(analyzer.call_stats.delta_dict, call_delta_dict)
120        self.assertEqual(analyzer.flush_stats.aggregate_deltas, [20000])
121        self.assertEqual(analyzer.llvm_stats.aggregate_deltas, [20])
122        self.assertEqual(analyzer.native_stats.aggregate_deltas, [55])
123        self.assertEqual(analyzer.eval_compile_stats.aggregate_deltas,
124                         [eval_compile_time])
125
126
127if __name__ == '__main__':
128    # Silence a warning from the unittest module relating to floating point
129    # equality.  We're doing the right thing, but it doesn't in Python 2.5.
130    warnings.filterwarnings('ignore', '.*integer.*float.*')
131    unittest.main()