profiling.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # testing/profiling.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: ignore-errors
  8. """Profiling support for unit and performance tests.
  9. These are special purpose profiling methods which operate
  10. in a more fine-grained way than nose's profiling plugin.
  11. """
  12. from __future__ import annotations
  13. import collections
  14. import contextlib
  15. import os
  16. import platform
  17. import pstats
  18. import re
  19. import sys
  20. from . import config
  21. from .util import gc_collect
  22. from ..util import has_compiled_ext
  23. try:
  24. import cProfile
  25. except ImportError:
  26. cProfile = None
  27. _profile_stats = None
  28. """global ProfileStatsFileInstance.
  29. plugin_base assigns this at the start of all tests.
  30. """
  31. _current_test = None
  32. """String id of current test.
  33. plugin_base assigns this at the start of each test using
  34. _start_current_test.
  35. """
  36. def _start_current_test(id_):
  37. global _current_test
  38. _current_test = id_
  39. if _profile_stats.force_write:
  40. _profile_stats.reset_count()
  41. class ProfileStatsFile:
  42. """Store per-platform/fn profiling results in a file.
  43. There was no json module available when this was written, but now
  44. the file format which is very deterministically line oriented is kind of
  45. handy in any case for diffs and merges.
  46. """
  47. def __init__(self, filename, sort="cumulative", dump=None):
  48. self.force_write = (
  49. config.options is not None and config.options.force_write_profiles
  50. )
  51. self.write = self.force_write or (
  52. config.options is not None and config.options.write_profiles
  53. )
  54. self.fname = os.path.abspath(filename)
  55. self.short_fname = os.path.split(self.fname)[-1]
  56. self.data = collections.defaultdict(
  57. lambda: collections.defaultdict(dict)
  58. )
  59. self.dump = dump
  60. self.sort = sort
  61. self._read()
  62. if self.write:
  63. # rewrite for the case where features changed,
  64. # etc.
  65. self._write()
  66. @property
  67. def platform_key(self):
  68. dbapi_key = config.db.name + "_" + config.db.driver
  69. if config.db.name == "sqlite" and config.db.dialect._is_url_file_db(
  70. config.db.url
  71. ):
  72. dbapi_key += "_file"
  73. # keep it at 2.7, 3.1, 3.2, etc. for now.
  74. py_version = ".".join([str(v) for v in sys.version_info[0:2]])
  75. platform_tokens = [
  76. platform.machine(),
  77. platform.system().lower(),
  78. platform.python_implementation().lower(),
  79. py_version,
  80. dbapi_key,
  81. ]
  82. platform_tokens.append("dbapiunicode")
  83. _has_cext = has_compiled_ext()
  84. platform_tokens.append(_has_cext and "cextensions" or "nocextensions")
  85. return "_".join(platform_tokens)
  86. def has_stats(self):
  87. test_key = _current_test
  88. return (
  89. test_key in self.data and self.platform_key in self.data[test_key]
  90. )
  91. def result(self, callcount):
  92. test_key = _current_test
  93. per_fn = self.data[test_key]
  94. per_platform = per_fn[self.platform_key]
  95. if "counts" not in per_platform:
  96. per_platform["counts"] = counts = []
  97. else:
  98. counts = per_platform["counts"]
  99. if "current_count" not in per_platform:
  100. per_platform["current_count"] = current_count = 0
  101. else:
  102. current_count = per_platform["current_count"]
  103. has_count = len(counts) > current_count
  104. if not has_count:
  105. counts.append(callcount)
  106. if self.write:
  107. self._write()
  108. result = None
  109. else:
  110. result = per_platform["lineno"], counts[current_count]
  111. per_platform["current_count"] += 1
  112. return result
  113. def reset_count(self):
  114. test_key = _current_test
  115. # since self.data is a defaultdict, don't access a key
  116. # if we don't know it's there first.
  117. if test_key not in self.data:
  118. return
  119. per_fn = self.data[test_key]
  120. if self.platform_key not in per_fn:
  121. return
  122. per_platform = per_fn[self.platform_key]
  123. if "counts" in per_platform:
  124. per_platform["counts"][:] = []
  125. def replace(self, callcount):
  126. test_key = _current_test
  127. per_fn = self.data[test_key]
  128. per_platform = per_fn[self.platform_key]
  129. counts = per_platform["counts"]
  130. current_count = per_platform["current_count"]
  131. if current_count < len(counts):
  132. counts[current_count - 1] = callcount
  133. else:
  134. counts[-1] = callcount
  135. if self.write:
  136. self._write()
  137. def _header(self):
  138. return (
  139. "# %s\n"
  140. "# This file is written out on a per-environment basis.\n"
  141. "# For each test in aaa_profiling, the corresponding "
  142. "function and \n"
  143. "# environment is located within this file. "
  144. "If it doesn't exist,\n"
  145. "# the test is skipped.\n"
  146. "# If a callcount does exist, it is compared "
  147. "to what we received. \n"
  148. "# assertions are raised if the counts do not match.\n"
  149. "# \n"
  150. "# To add a new callcount test, apply the function_call_count \n"
  151. "# decorator and re-run the tests using the --write-profiles \n"
  152. "# option - this file will be rewritten including the new count.\n"
  153. "# \n"
  154. ) % (self.fname)
  155. def _read(self):
  156. try:
  157. profile_f = open(self.fname)
  158. except OSError:
  159. return
  160. for lineno, line in enumerate(profile_f):
  161. line = line.strip()
  162. if not line or line.startswith("#"):
  163. continue
  164. test_key, platform_key, counts = line.split()
  165. per_fn = self.data[test_key]
  166. per_platform = per_fn[platform_key]
  167. c = [int(count) for count in counts.split(",")]
  168. per_platform["counts"] = c
  169. per_platform["lineno"] = lineno + 1
  170. per_platform["current_count"] = 0
  171. profile_f.close()
  172. def _write(self):
  173. print("Writing profile file %s" % self.fname)
  174. profile_f = open(self.fname, "w")
  175. profile_f.write(self._header())
  176. for test_key in sorted(self.data):
  177. per_fn = self.data[test_key]
  178. profile_f.write("\n# TEST: %s\n\n" % test_key)
  179. for platform_key in sorted(per_fn):
  180. per_platform = per_fn[platform_key]
  181. c = ",".join(str(count) for count in per_platform["counts"])
  182. profile_f.write("%s %s %s\n" % (test_key, platform_key, c))
  183. profile_f.close()
  184. def function_call_count(variance=0.05, times=1, warmup=0):
  185. """Assert a target for a test case's function call count.
  186. The main purpose of this assertion is to detect changes in
  187. callcounts for various functions - the actual number is not as important.
  188. Callcounts are stored in a file keyed to Python version and OS platform
  189. information. This file is generated automatically for new tests,
  190. and versioned so that unexpected changes in callcounts will be detected.
  191. """
  192. # use signature-rewriting decorator function so that pytest fixtures
  193. # still work on py27. In Py3, update_wrapper() alone is good enough,
  194. # likely due to the introduction of __signature__.
  195. from sqlalchemy.util import decorator
  196. @decorator
  197. def wrap(fn, *args, **kw):
  198. for warm in range(warmup):
  199. fn(*args, **kw)
  200. timerange = range(times)
  201. with count_functions(variance=variance):
  202. for time in timerange:
  203. rv = fn(*args, **kw)
  204. return rv
  205. return wrap
  206. @contextlib.contextmanager
  207. def count_functions(variance=0.05):
  208. if cProfile is None:
  209. raise config._skip_test_exception("cProfile is not installed")
  210. if not _profile_stats.has_stats() and not _profile_stats.write:
  211. config.skip_test(
  212. "No profiling stats available on this "
  213. "platform for this function. Run tests with "
  214. "--write-profiles to add statistics to %s for "
  215. "this platform." % _profile_stats.short_fname
  216. )
  217. gc_collect()
  218. pr = cProfile.Profile()
  219. pr.enable()
  220. # began = time.time()
  221. yield
  222. # ended = time.time()
  223. pr.disable()
  224. # s = StringIO()
  225. stats = pstats.Stats(pr, stream=sys.stdout)
  226. # timespent = ended - began
  227. callcount = stats.total_calls
  228. expected = _profile_stats.result(callcount)
  229. if expected is None:
  230. expected_count = None
  231. else:
  232. line_no, expected_count = expected
  233. print("Pstats calls: %d Expected %s" % (callcount, expected_count))
  234. stats.sort_stats(*re.split(r"[, ]", _profile_stats.sort))
  235. stats.print_stats()
  236. if _profile_stats.dump:
  237. base, ext = os.path.splitext(_profile_stats.dump)
  238. test_name = _current_test.split(".")[-1]
  239. dumpfile = "%s_%s%s" % (base, test_name, ext or ".profile")
  240. stats.dump_stats(dumpfile)
  241. print("Dumped stats to file %s" % dumpfile)
  242. # stats.print_callers()
  243. if _profile_stats.force_write:
  244. _profile_stats.replace(callcount)
  245. elif expected_count:
  246. deviance = int(callcount * variance)
  247. failed = abs(callcount - expected_count) > deviance
  248. if failed:
  249. if _profile_stats.write:
  250. _profile_stats.replace(callcount)
  251. else:
  252. raise AssertionError(
  253. "Adjusted function call count %s not within %s%% "
  254. "of expected %s, platform %s. Rerun with "
  255. "--write-profiles to "
  256. "regenerate this callcount."
  257. % (
  258. callcount,
  259. (variance * 100),
  260. expected_count,
  261. _profile_stats.platform_key,
  262. )
  263. )