coverage.py 9.5 KB

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