MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / count_functions

Function count_functions

lib/sqlalchemy/testing/profiling.py:263–328  ·  view source on GitHub ↗
(variance=0.05)

Source from the content-addressed store, hash-verified

261
262@contextlib.contextmanager
263def count_functions(variance=0.05):
264 if cProfile is None:
265 raise config._skip_test_exception("cProfile is not installed")
266
267 if not _profile_stats.has_stats() and not _profile_stats.write:
268 config.skip_test(
269 "No profiling stats available on this "
270 "platform for this function. Run tests with "
271 "--write-profiles to add statistics to %s for "
272 "this platform." % _profile_stats.short_fname
273 )
274
275 gc_collect()
276
277 pr = cProfile.Profile()
278 pr.enable()
279 # began = time.time()
280 yield
281 # ended = time.time()
282 pr.disable()
283
284 # s = StringIO()
285 stats = pstats.Stats(pr, stream=sys.stdout)
286
287 # timespent = ended - began
288 callcount = stats.total_calls
289
290 expected = _profile_stats.result(callcount)
291
292 if expected is None:
293 expected_count = None
294 else:
295 line_no, expected_count = expected
296
297 print("Pstats calls: %d Expected %s" % (callcount, expected_count))
298 stats.sort_stats(*re.split(r"[, ]", _profile_stats.sort))
299 stats.print_stats()
300 if _profile_stats.dump:
301 base, ext = os.path.splitext(_profile_stats.dump)
302 test_name = _current_test.split(".")[-1]
303 dumpfile = "%s_%s%s" % (base, test_name, ext or ".profile")
304 stats.dump_stats(dumpfile)
305 print("Dumped stats to file %s" % dumpfile)
306 # stats.print_callers()
307 if _profile_stats.force_write:
308 _profile_stats.replace(callcount)
309 elif expected_count:
310 deviance = int(callcount * variance)
311 failed = abs(callcount - expected_count) > deviance
312
313 if failed:
314 if _profile_stats.write:
315 _profile_stats.replace(callcount)
316 else:
317 raise AssertionError(
318 "Adjusted function call count %s not within %s%% "
319 "of expected %s, platform %s. Rerun with "
320 "--write-profiles to "

Callers 1

wrapFunction · 0.85

Calls 5

has_statsMethod · 0.80
skip_testMethod · 0.80
resultMethod · 0.80
splitMethod · 0.80
replaceMethod · 0.45

Tested by

no test coverage detected