coverage.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/env python3
  2. #
  3. import os
  4. import glob
  5. import csv
  6. import re
  7. import collections as co
  8. import bisect as b
  9. INFO_PATHS = 'tests/*.toml.info'
  10. def collect(paths, **args):
  11. file = None
  12. funcs = []
  13. lines = co.defaultdict(lambda: 0)
  14. pattern = re.compile(
  15. '^(?P<file>SF:/?(?P<file_name>.*))$'
  16. '|^(?P<func>FN:(?P<func_lineno>[0-9]*),(?P<func_name>.*))$'
  17. '|^(?P<line>DA:(?P<line_lineno>[0-9]*),(?P<line_hits>[0-9]*))$')
  18. for path in paths:
  19. with open(path) as f:
  20. for line in f:
  21. m = pattern.match(line)
  22. if m and m.group('file'):
  23. file = m.group('file_name')
  24. elif m and file and m.group('func'):
  25. funcs.append((file, int(m.group('func_lineno')),
  26. m.group('func_name')))
  27. elif m and file and m.group('line'):
  28. lines[(file, int(m.group('line_lineno')))] += (
  29. int(m.group('line_hits')))
  30. # map line numbers to functions
  31. funcs.sort()
  32. def func_from_lineno(file, lineno):
  33. i = b.bisect(funcs, (file, lineno))
  34. if i and funcs[i-1][0] == file:
  35. return funcs[i-1][2]
  36. else:
  37. return None
  38. # reduce to function info
  39. reduced_funcs = co.defaultdict(lambda: (0, 0))
  40. for (file, line_lineno), line_hits in lines.items():
  41. func = func_from_lineno(file, line_lineno)
  42. if not func:
  43. continue
  44. hits, count = reduced_funcs[(file, func)]
  45. reduced_funcs[(file, func)] = (hits + (line_hits > 0), count + 1)
  46. results = []
  47. for (file, func), (hits, count) in reduced_funcs.items():
  48. # discard internal/testing functions (test_* injected with
  49. # internal testing)
  50. if func.startswith('__') or func.startswith('test_'):
  51. continue
  52. # discard .8449 suffixes created by optimizer
  53. func = re.sub('\.[0-9]+', '', func)
  54. results.append((file, func, hits, count))
  55. return results
  56. def main(**args):
  57. # find coverage
  58. if not args.get('input', None):
  59. # find *.info files
  60. paths = []
  61. for path in args['info_paths']:
  62. if os.path.isdir(path):
  63. path = path + '/*.gcov'
  64. for path in glob.glob(path, recursive=True):
  65. paths.append(path)
  66. if not paths:
  67. print('no .info files found in %r?' % args['info_paths'])
  68. sys.exit(-1)
  69. results = collect(paths, **args)
  70. else:
  71. with open(args['input']) as f:
  72. r = csv.DictReader(f)
  73. results = [
  74. ( result['file'],
  75. result['function'],
  76. int(result['hits']),
  77. int(result['count']))
  78. for result in r]
  79. total_hits, total_count = 0, 0
  80. for _, _, hits, count in results:
  81. total_hits += hits
  82. total_count += count
  83. # find previous results?
  84. if args.get('diff', None):
  85. with open(args['diff']) as f:
  86. r = csv.DictReader(f)
  87. prev_results = [
  88. ( result['file'],
  89. result['function'],
  90. int(result['hits']),
  91. int(result['count']))
  92. for result in r]
  93. prev_total_hits, prev_total_count = 0, 0
  94. for _, _, hits, count in prev_results:
  95. prev_total_hits += hits
  96. prev_total_count += count
  97. # write results to CSV
  98. if args.get('output', None):
  99. results.sort(key=lambda x: (-(x[3]-x[2]), -x[3], x))
  100. with open(args['output'], 'w') as f:
  101. w = csv.writer(f)
  102. w.writerow(['file', 'function', 'hits', 'count'])
  103. for file, func, hits, count in results:
  104. w.writerow((file, func, hits, count))
  105. # print results
  106. def dedup_entries(results, by='function'):
  107. entries = co.defaultdict(lambda: (0, 0))
  108. for file, func, hits, count in results:
  109. entry = (file if by == 'file' else func)
  110. entry_hits, entry_count = entries[entry]
  111. entries[entry] = (entry_hits + hits, entry_count + count)
  112. return entries
  113. def diff_entries(olds, news):
  114. diff = co.defaultdict(lambda: (None, None, None, None, None, None))
  115. for name, (new_hits, new_count) in news.items():
  116. diff[name] = (
  117. 0, 0,
  118. new_hits, new_count,
  119. new_hits, new_count)
  120. for name, (old_hits, old_count) in olds.items():
  121. new_hits = diff[name][2] or 0
  122. new_count = diff[name][3] or 0
  123. diff[name] = (
  124. old_hits, old_count,
  125. new_hits, new_count,
  126. new_hits-old_hits, new_count-old_count)
  127. return diff
  128. def print_header(by=''):
  129. if not args.get('diff', False):
  130. print('%-36s %11s' % (by, 'hits/count'))
  131. else:
  132. print('%-36s %11s %11s %11s' % (by, 'old', 'new', 'diff'))
  133. def print_entries(by='function'):
  134. entries = dedup_entries(results, by=by)
  135. if not args.get('diff', None):
  136. print_header(by=by)
  137. for name, (hits, count) in sorted(entries.items(),
  138. key=lambda x: (-(x[1][1]-x[1][0]), -x[1][1], x)):
  139. print("%-36s %11s (%.2f%%)" % (name,
  140. '%d/%d' % (hits, count),
  141. 100*(hits/count if count else 1.0)))
  142. else:
  143. prev_entries = dedup_entries(prev_results, by=by)
  144. diff = diff_entries(prev_entries, entries)
  145. print_header(by='%s (%d added, %d removed)' % (by,
  146. sum(1 for _, old, _, _, _, _ in diff.values() if not old),
  147. sum(1 for _, _, _, new, _, _ in diff.values() if not new)))
  148. for name, (
  149. old_hits, old_count,
  150. new_hits, new_count,
  151. diff_hits, diff_count) in sorted(diff.items(),
  152. key=lambda x: (
  153. -(x[1][5]-x[1][4]), -x[1][5], -x[1][3], x)):
  154. ratio = ((new_hits/new_count if new_count else 1.0)
  155. - (old_hits/old_count if old_count else 1.0))
  156. if diff_hits or diff_count or args.get('all', False):
  157. print("%-36s %11s %11s %11s%s" % (name,
  158. '%d/%d' % (old_hits, old_count)
  159. if old_count else '-',
  160. '%d/%d' % (new_hits, new_count)
  161. if new_count else '-',
  162. '%+d/%+d' % (diff_hits, diff_count),
  163. ' (%+.2f%%)' % (100*ratio) if ratio else ''))
  164. def print_totals():
  165. if not args.get('diff', None):
  166. print("%-36s %11s (%.2f%%)" % ('TOTALS',
  167. '%d/%d' % (total_hits, total_count),
  168. 100*(total_hits/total_count if total_count else 1.0)))
  169. else:
  170. ratio = ((total_hits/total_count
  171. if total_count else 1.0)
  172. - (prev_total_hits/prev_total_count
  173. if prev_total_count else 1.0))
  174. print("%-36s %11s %11s %11s%s" % ('TOTALS',
  175. '%d/%d' % (prev_total_hits, prev_total_count),
  176. '%d/%d' % (total_hits, total_count),
  177. '%+d/%+d' % (total_hits-prev_total_hits,
  178. total_count-prev_total_count),
  179. ' (%+.2f%%)' % (100*ratio) if ratio else ''))
  180. def print_status():
  181. if not args.get('diff', None):
  182. print("%d/%d (%.2f%%)" % (total_hits, total_count,
  183. 100*(total_hits/total_count if total_count else 1.0)))
  184. else:
  185. ratio = ((total_hits/total_count
  186. if total_count else 1.0)
  187. - (prev_total_hits/prev_total_count
  188. if prev_total_count else 1.0))
  189. print("%d/%d (%+.2f%%)" % (total_hits, total_count,
  190. (100*ratio) if ratio else ''))
  191. if args.get('quiet', False):
  192. pass
  193. elif args.get('status', False):
  194. print_status()
  195. elif args.get('summary', False):
  196. print_header()
  197. print_totals()
  198. elif args.get('files', False):
  199. print_entries(by='file')
  200. print_totals()
  201. else:
  202. print_entries(by='function')
  203. print_totals()
  204. if __name__ == "__main__":
  205. import argparse
  206. import sys
  207. parser = argparse.ArgumentParser(
  208. description="Show/manipulate coverage info")
  209. parser.add_argument('info_paths', nargs='*', default=[INFO_PATHS],
  210. help="Description of where to find *.info files. May be a directory \
  211. or list of paths. *.info files will be merged to show the total \
  212. coverage. Defaults to \"%s\"." % INFO_PATHS)
  213. parser.add_argument('-v', '--verbose', action='store_true',
  214. help="Output commands that run behind the scenes.")
  215. parser.add_argument('-i', '--input',
  216. help="Don't do any work, instead use this CSV file.")
  217. parser.add_argument('-o', '--output',
  218. help="Specify CSV file to store results.")
  219. parser.add_argument('-d', '--diff',
  220. help="Specify CSV file to diff code size against.")
  221. parser.add_argument('-a', '--all', action='store_true',
  222. help="Show all functions, not just the ones that changed.")
  223. parser.add_argument('--files', action='store_true',
  224. help="Show file-level coverage.")
  225. parser.add_argument('-s', '--summary', action='store_true',
  226. help="Only show the total coverage.")
  227. parser.add_argument('-S', '--status', action='store_true',
  228. help="Show minimum info useful for a single-line status.")
  229. parser.add_argument('-q', '--quiet', action='store_true',
  230. help="Don't show anything, useful with -o.")
  231. sys.exit(main(**vars(parser.parse_args())))