def filter_include(): filter_func = pycallgraph.GlobbingFilter(include=['*.secret_function', \ 'Banana.__init__']) pycallgraph.start_trace(filter_func=filter_func) banana = Banana() banana.eat() pycallgraph.make_dot_graph('filter-include.png')
def filter_exclude(): filter_func = pycallgraph.GlobbingFilter(exclude=['pycallgraph.*', \ '*.secret_function']) pycallgraph.start_trace(filter_func=filter_func) banana = Banana() banana.eat() pycallgraph.make_dot_graph('filter-exclude.png')
def __init__(self, app, filename): super(PycallgraphMiddleware, self).__init__(app) import pycallgraph pycallgraph.settings['include_stdlib'] = False self._filename = filename globs = ['pycallgraph.*', 'unknown.*'] f = pycallgraph.GlobbingFilter(exclude=globs, max_depth=9999) self._filter = f self.pycallgraph = pycallgraph
def generate_cfg(out_path): ''' Writes the graphviz generated CFG to out_path. Only includes modules from full_pipe, edx_pipe, and curation. ''' config = pycallgraph.Config(max_depth=4) config.trace_filter = pycallgraph.GlobbingFilter( include=['curation.*', 'edx_pipe.*', 'full_pipe.*']) graphviz = pycallgraph.output.GraphvizOutput(output_file=out_path) with pycallgraph.PyCallGraph(output=graphviz, config=config): full_pipe.main()
def test_performance_check_select_destination(self, mock_get_extra, mock_get_by_uuid, mock_get_by_instance_uuid): hosts = 2 requests = 1 self.flags(service_down_time=240) spec_obj = self._get_fake_request_spec() host_states = [] for x in range(hosts): host_state = self._get_fake_host_state(x) host_states.append(host_state) self.driver.all_host_states = {uuids.cell: host_states} provider_summaries = {hs.uuid: hs for hs in host_states} def run_test(): a = timeutils.utcnow() for x in range(requests): self.driver.select_destinations(self.context, spec_obj, [spec_obj.instance_uuid], {}, provider_summaries) b = timeutils.utcnow() c = b - a seconds = (c.days * 24 * 60 * 60 + c.seconds) microseconds = seconds * 1000 + c.microseconds / 1000.0 per_request_ms = microseconds / requests return per_request_ms per_request_ms = None if ENABLE_PROFILER: import pycallgraph from pycallgraph import output config = pycallgraph.Config(max_depth=10) config.trace_filter = pycallgraph.GlobbingFilter(exclude=[ 'pycallgraph.*', 'unittest.*', 'testtools.*', 'nova.tests.unit.*', ]) graphviz = output.GraphvizOutput(output_file='scheduler.png') with pycallgraph.PyCallGraph(output=graphviz): per_request_ms = run_test() else: per_request_ms = run_test() # This has proved to be around 1 ms on a random dev box # But this is here so you can do simply performance testing easily. self.assertLess(per_request_ms, 1000)
def trace(self, fn, fname, exclude_from_trace=None, strings_to_delete=None, class_colors=None): """ Trace the program flow and creates DOT (*.dot) and GML (*.gml) files. # Arguments fn (object): function to call for the entry point to trace the program flow fname (str): filename for graph files (do not supply file extension) exclude_from_trace (list): names (`str`) to exclude from trace (`None` by default) strings_to_delete (list): text (`str`) to remove from graph files (`None` by default) class_colors (dict): node colors (`None` by default) # Returns None: """ # 1. filter out some unwanted functions and classes: config = pycallgraph.Config() if exclude_from_trace is not None: config.trace_filter = pycallgraph.GlobbingFilter( exclude=exclude_from_trace) # 2. trace the program flow: with pycallgraph.PyCallGraph(output=self, config=config): fn() # entry point for tracing the program flow dotstr = self.generate() # 3. customize the output: # 3a. delete strings if strings_to_delete is not None: for rx in strings_to_delete: dotstr = rx.sub('', dotstr) # 3b. customization: change color for class nodes if class_colors is not None: dotstr = Graph._colorize(dotstr, class_colors) # 4. create the DOT file fname_dot = fname + '.dot' with open(fname_dot, 'wt') as fdot: fdot.write(dotstr) # 5. create the GML (XML) file fname_gml = fname + '.gml' cmd = 'dot ' + fname_dot + ' | gv2gml >' + fname_gml os.system(cmd) # use the dot.exe to convert the DOT file into GML file
def test_performance_check_select_destination(self, mock_get_extra): hosts = 2 requests = 1 self.flags(service_down_time=240) request_spec = self._get_fake_request_spec() host_states = [] for x in xrange(hosts): host_state = self._get_fake_host_state(x) host_states.append(host_state) self.driver.all_host_states = host_states def run_test(): a = timeutils.utcnow() for x in xrange(requests): self.driver.select_destinations(self.context, request_spec, {}) b = timeutils.utcnow() c = b - a seconds = (c.days * 24 * 60 * 60 + c.seconds) microseconds = seconds * 1000 + c.microseconds / 1000.0 per_request_ms = microseconds / requests return per_request_ms per_request_ms = None if ENABLE_PROFILER: import pycallgraph from pycallgraph import output config = pycallgraph.Config(max_depth=10) config.trace_filter = pycallgraph.GlobbingFilter(exclude=[ 'pycallgraph.*', 'unittest.*', 'patron.tests.unit.*', ]) graphviz = output.GraphvizOutput(output_file='scheduler.png') with pycallgraph.PyCallGraph(output=graphviz): per_request_ms = run_test() else: per_request_ms = run_test() # This has proved to be around 1 ms on a random dev box # But this is here so you can do simply performance testing easily. self.assertTrue(per_request_ms < 1000)
def run(self, runWithEnviron=True): ''' Main code runner for testing. To set a new test, update the self.callTest attribute in __init__(). Note that the default of runWithEnviron imports music21.environment. That might skew results ''' suffix = '.svg' outputFormat = suffix[1:] _MOD = "test.timeGraphs.py" if runWithEnviron: from music21 import environment environLocal = environment.Environment(_MOD) fp = environLocal.getTempFile(suffix) # manually get a temporary file else: import tempfile import os import sys if os.name in ['nt'] or sys.platform.startswith('win'): platform = 'win' else: platform = 'other' tempdir = os.path.join(tempfile.gettempdir(), 'music21') if platform != 'win': fd, fp = tempfile.mkstemp(dir=tempdir, suffix=suffix) if isinstance(fd, int): # on MacOS, fd returns an int, like 3, when this is called # in some context (specifically, programmatically in a # TestExternal class. the fp is still valid and works # TODO: this did not work on MacOS 10.6.8 w/ py 2.7 pass else: fd.close() else: tf = tempfile.NamedTemporaryFile(dir=tempdir, suffix=suffix) fp = tf.name tf.close() if self.includeList is not None: gf = pycallgraph.GlobbingFilter(include=self.includeList, exclude=self.excludeList) else: gf = pycallgraph.GlobbingFilter(exclude=self.excludeList) # create instance; will call setup routines ct = self.callTest() # start timer print('%s starting test' % _MOD) t = Timer() t.start() pycallgraph.start_trace(filter_func=gf) ct.testFocus() # run routine pycallgraph.stop_trace() pycallgraph.make_dot_graph(fp, format=outputFormat, tool='/usr/local/bin/dot') print('elapsed time: %s' % t) # open the completed file print('file path: ' + fp) try: environLocal = environment.Environment(_MOD) environLocal.launch(outputFormat, fp) except NameError: pass
def run(self, runWithEnviron=True): ''' Main code runner for testing. To set a new test, update the self.callTest attribute in __init__(). Note that the default of runWithEnviron imports music21.environment. That might skew results ''' from music21 import environment suffix = '.png' # '.svg' outputFormat = suffix[1:] _MOD = "test.timeGraphs" if runWithEnviron: environLocal = environment.Environment(_MOD) fp = environLocal.getTempFile(suffix) # manually get a temporary file else: import tempfile import os import sys if os.name in ['nt'] or sys.platform.startswith('win'): platform = 'win' else: platform = 'other' tempdir = os.path.join(tempfile.gettempdir(), 'music21') if platform != 'win': fd, fp = tempfile.mkstemp(dir=tempdir, suffix=suffix) if isinstance(fd, int): # on MacOS, fd returns an int, like 3, when this is called # in some context (specifically, programmatically in a # TestExternal class. the fp is still valid and works # TODO: this did not work on MacOS 10.6.8 w/ py 2.7 pass else: fd.close() else: tf = tempfile.NamedTemporaryFile(dir=tempdir, suffix=suffix) fp = tf.name tf.close() if self.includeList is not None: gf = pycallgraph.GlobbingFilter(include=self.includeList, exclude=self.excludeList) else: gf = pycallgraph.GlobbingFilter(exclude=self.excludeList) # create instance; will call setup routines ct = self.callTest() # start timer print('%s starting test' % _MOD) t = Timer() t.start() graphviz = pycallgraph.output.GraphvizOutput(output_file=fp) graphviz.tool = '/usr/local/bin/dot' config = pycallgraph.Config() config.trace_filter = gf from music21 import meter from music21 import note #from music21 import converter #from music21 import common #beeth = common.getCorpusFilePath() + '/beethoven/opus133.mxl' #s = converter.parse(beeth, forceSource=True) #beeth = common.getCorpusFilePath() + '/bach/bwv66.6.mxl' #s = converter.parse(beeth, forceSource=True) with pycallgraph.PyCallGraph(output=graphviz, config=config): note.Note() meter.TimeSignature('4/4') ct.testFocus() # run routine pass print('elapsed time: %s' % t) # open the completed file print('file path: ' + fp) try: environLocal = environment.Environment(_MOD) environLocal.launch(outputFormat, fp) except NameError: pass
def run(self, runWithEnviron=False): ''' Main code runner for testing. To set a new test, update the self.callTest attribute in __init__(). ''' suffix = '.png' # '.svg' no reader for now... _MOD = "test.timeGraphImportStar.py" if runWithEnviron: from music21 import environment environLocal = environment.Environment(_MOD) fp = environLocal.getTempFile(suffix) # manually get a temporary file else: import tempfile import os import sys if os.name in ['nt'] or sys.platform.startswith('win'): platform = 'win' else: platform = 'other' tempdir = os.path.join(tempfile.gettempdir(), 'music21') if platform != 'win': fd, fp = tempfile.mkstemp(dir=tempdir, suffix=suffix) if isinstance(fd, int): # on MacOS, fd returns an int, like 3, when this is called # in some context (specifically, programmatically in a # TestExternal class. the fp is still valid and works # TODO: this did not work on MacOS 10.6.8 w/ py 2.7 pass else: fd.close() else: tf = tempfile.NamedTemporaryFile(dir=tempdir, suffix=suffix) fp = tf.name tf.close() if self.includeList is not None: gf = pycallgraph.GlobbingFilter(include=self.includeList, exclude=self.excludeList) else: gf = pycallgraph.GlobbingFilter(exclude=self.excludeList) # create instance; will call setup routines ct = self.callTest() # start timer print('%s starting test' % _MOD) t = Timer() t.start() graphviz = pycallgraph.output.GraphvizOutput(output_file=fp) graphviz.tool = '/usr/local/bin/dot' config = pycallgraph.Config() config.trace_filter = gf with pycallgraph.PyCallGraph(output=graphviz, config=config): ct.testFocus() # run routine print('elapsed time: %s' % t) # open the completed file print('file path: ' + fp) try: environLocal = environment.Environment(_MOD) environLocal.launch(format, fp) except NameError: pass
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], nodatabase=False, xml_out=False, callgraph=False): """ Test runner which displays a code coverage report at the end of the run. """ cov = coverage.coverage() cov.erase() cov.use_cache(0) test_labels = test_labels or getattr(settings, "TEST_APPS", None) cover_branch = getattr(settings, "COVERAGE_BRANCH_COVERAGE", False) cov = coverage.coverage(branch=cover_branch, cover_pylib=False) cov.use_cache(0) coverage_modules = [] if test_labels: for label in test_labels: # Don't report coverage if you're only running a single # test case. if '.' not in label: app = get_app(label) coverage_modules.extend(get_all_coverage_modules(app)) else: for app in get_apps(): coverage_modules.extend(get_all_coverage_modules(app)) morfs = filter(is_wanted_module, coverage_modules) if callgraph: try: import pycallgraph #_include = [i.__name__ for i in coverage_modules] _included = getattr(settings, "COVERAGE_INCLUDE_MODULES", []) _excluded = getattr(settings, "COVERAGE_EXCLUDE_MODULES", []) _included = [i.strip('*') + '*' for i in _included] _excluded = [i.strip('*') + '*' for i in _included] _filter_func = pycallgraph.GlobbingFilter( include=_included or ['*'], #include=['lotericas.*'], #exclude=[], #max_depth=options.max_depth, ) pycallgraph_enabled = True except ImportError: pycallgraph_enabled = False else: pycallgraph_enabled = False cov.start() if pycallgraph_enabled: pycallgraph.start_trace(filter_func=_filter_func) if nodatabase: results = nodatabase_run_tests(test_labels, verbosity, interactive, extra_tests) else: results = django_test_runner(test_labels, verbosity, interactive, extra_tests) if callgraph and pycallgraph_enabled: pycallgraph.stop_trace() cov.stop() report_methd = cov.report if getattr(settings, "COVERAGE_HTML_REPORT", False) or \ os.environ.get("COVERAGE_HTML_REPORT"): output_dir = getattr(settings, "COVERAGE_HTML_DIRECTORY", "covhtml") report_method = curry(cov.html_report, directory=output_dir) if callgraph and pycallgraph_enabled: callgraph_path = output_dir + '/' + 'callgraph.png' pycallgraph.make_dot_graph(callgraph_path) print >> sys.stdout print >> sys.stdout, "Coverage HTML reports were output to '%s'" % output_dir if callgraph: if pycallgraph_enabled: print >> sys.stdout, "Call graph was output to '%s'" % callgraph_path else: print >> sys.stdout, "Call graph was not generated: Install 'pycallgraph' module to do so" else: report_method = cov.report if coverage_modules: if xml_out: # using the same output directory as the --xml function uses for testing if not os.path.isdir(os.path.join("temp", "xml")): os.makedirs(os.path.join("temp", "xml")) output_filename = 'temp/xml/coverage_output.xml' cov.xml_report(morfs=coverage_modules, outfile=output_filename) cov.report(coverage_modules, show_missing=1) return results
def filter_module(): filter_func = pycallgraph.GlobbingFilter(include=['sre_parse.*']) pycallgraph.start_trace(time_filter_func=filter_func) for a in xrange(100): re.compile('test3-%i' % a) pycallgraph.make_dot_graph('filter-time-module.png')
def filter_min_depth(): filter_func = pycallgraph.GlobbingFilter(min_depth=7) pycallgraph.start_trace(time_filter_func=filter_func) for a in xrange(100): re.compile('test2-%i' % a) pycallgraph.make_dot_graph('filter-time-min-depth.png')
def filter_max_depth(): filter_func = pycallgraph.GlobbingFilter(max_depth=1) pycallgraph.start_trace(filter_func=filter_func) banana = Banana() banana.eat() pycallgraph.make_dot_graph('filter-max-depth.png')