regression.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # resulttool - regression analysis
  2. #
  3. # Copyright (c) 2019, Intel Corporation.
  4. # Copyright (c) 2019, Linux Foundation
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms and conditions of the GNU General Public License,
  8. # version 2, as published by the Free Software Foundation.
  9. #
  10. # This program is distributed in the hope it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. # more details.
  14. #
  15. import resulttool.resultutils as resultutils
  16. import json
  17. from oeqa.utils.git import GitRepo
  18. import oeqa.utils.gitarchive as gitarchive
  19. def compare_result(logger, base_name, target_name, base_result, target_result):
  20. base_result = base_result.get('result')
  21. target_result = target_result.get('result')
  22. result = {}
  23. if base_result and target_result:
  24. for k in base_result:
  25. base_testcase = base_result[k]
  26. base_status = base_testcase.get('status')
  27. if base_status:
  28. target_testcase = target_result.get(k, {})
  29. target_status = target_testcase.get('status')
  30. if base_status != target_status:
  31. result[k] = {'base': base_status, 'target': target_status}
  32. else:
  33. logger.error('Failed to retrieved base test case status: %s' % k)
  34. if result:
  35. resultstring = "Regression: %s\n %s\n" % (base_name, target_name)
  36. for k in sorted(result):
  37. resultstring += ' %s: %s -> %s\n' % (k, result[k]['base'], result[k]['target'])
  38. else:
  39. resultstring = "Match: %s\n %s" % (base_name, target_name)
  40. return result, resultstring
  41. def get_results(logger, source):
  42. return resultutils.load_resultsdata(source, configmap=resultutils.regression_map)
  43. def regression(args, logger):
  44. base_results = get_results(logger, args.base_result)
  45. target_results = get_results(logger, args.target_result)
  46. regression_common(args, logger, base_results, target_results)
  47. def regression_common(args, logger, base_results, target_results):
  48. if args.base_result_id:
  49. base_results = resultutils.filter_resultsdata(base_results, args.base_result_id)
  50. if args.target_result_id:
  51. target_results = resultutils.filter_resultsdata(target_results, args.target_result_id)
  52. matches = []
  53. regressions = []
  54. notfound = []
  55. for a in base_results:
  56. if a in target_results:
  57. base = list(base_results[a].keys())
  58. target = list(target_results[a].keys())
  59. # We may have multiple base/targets which are for different configurations. Start by
  60. # removing any pairs which match
  61. for c in base.copy():
  62. for b in target.copy():
  63. res, resstr = compare_result(logger, c, b, base_results[a][c], target_results[a][b])
  64. if not res:
  65. matches.append(resstr)
  66. base.remove(c)
  67. target.remove(b)
  68. break
  69. # Should only now see regressions, we may not be able to match multiple pairs directly
  70. for c in base:
  71. for b in target:
  72. res, resstr = compare_result(logger, c, b, base_results[a][c], target_results[a][b])
  73. if res:
  74. regressions.append(resstr)
  75. else:
  76. notfound.append("%s not found in target" % a)
  77. print("\n".join(sorted(matches)))
  78. print("\n".join(sorted(regressions)))
  79. print("\n".join(sorted(notfound)))
  80. return 0
  81. def regression_git(args, logger):
  82. base_results = {}
  83. target_results = {}
  84. tag_name = "{branch}/{commit_number}-g{commit}/{tag_number}"
  85. repo = GitRepo(args.repo)
  86. revs = gitarchive.get_test_revs(logger, repo, tag_name, branch=args.branch)
  87. if args.branch2:
  88. revs2 = gitarchive.get_test_revs(logger, repo, tag_name, branch=args.branch2)
  89. if not len(revs2):
  90. logger.error("No revisions found to compare against")
  91. return 1
  92. if not len(revs):
  93. logger.error("No revision to report on found")
  94. return 1
  95. else:
  96. if len(revs) < 2:
  97. logger.error("Only %d tester revisions found, unable to generate report" % len(revs))
  98. return 1
  99. # Pick revisions
  100. if args.commit:
  101. if args.commit_number:
  102. logger.warning("Ignoring --commit-number as --commit was specified")
  103. index1 = gitarchive.rev_find(revs, 'commit', args.commit)
  104. elif args.commit_number:
  105. index1 = gitarchive.rev_find(revs, 'commit_number', args.commit_number)
  106. else:
  107. index1 = len(revs) - 1
  108. if args.branch2:
  109. revs2.append(revs[index1])
  110. index1 = len(revs2) - 1
  111. revs = revs2
  112. if args.commit2:
  113. if args.commit_number2:
  114. logger.warning("Ignoring --commit-number2 as --commit2 was specified")
  115. index2 = gitarchive.rev_find(revs, 'commit', args.commit2)
  116. elif args.commit_number2:
  117. index2 = gitarchive.rev_find(revs, 'commit_number', args.commit_number2)
  118. else:
  119. if index1 > 0:
  120. index2 = index1 - 1
  121. # Find the closest matching commit number for comparision
  122. # In future we could check the commit is a common ancestor and
  123. # continue back if not but this good enough for now
  124. while index2 > 0 and revs[index2].commit_number > revs[index1].commit_number:
  125. index2 = index2 - 1
  126. else:
  127. logger.error("Unable to determine the other commit, use "
  128. "--commit2 or --commit-number2 to specify it")
  129. return 1
  130. logger.info("Comparing:\n%s\nto\n%s\n" % (revs[index1], revs[index2]))
  131. base_results = resultutils.git_get_result(repo, revs[index1][2])
  132. target_results = resultutils.git_get_result(repo, revs[index2][2])
  133. regression_common(args, logger, base_results, target_results)
  134. return 0
  135. def register_commands(subparsers):
  136. """Register subcommands from this plugin"""
  137. parser_build = subparsers.add_parser('regression', help='regression file/directory analysis',
  138. description='regression analysis comparing the base set of results to the target results',
  139. group='analysis')
  140. parser_build.set_defaults(func=regression)
  141. parser_build.add_argument('base_result',
  142. help='base result file/directory/URL for the comparison')
  143. parser_build.add_argument('target_result',
  144. help='target result file/directory/URL to compare with')
  145. parser_build.add_argument('-b', '--base-result-id', default='',
  146. help='(optional) filter the base results to this result ID')
  147. parser_build.add_argument('-t', '--target-result-id', default='',
  148. help='(optional) filter the target results to this result ID')
  149. parser_build = subparsers.add_parser('regression-git', help='regression git analysis',
  150. description='regression analysis comparing base result set to target '
  151. 'result set',
  152. group='analysis')
  153. parser_build.set_defaults(func=regression_git)
  154. parser_build.add_argument('repo',
  155. help='the git repository containing the data')
  156. parser_build.add_argument('-b', '--base-result-id', default='',
  157. help='(optional) default select regression based on configurations unless base result '
  158. 'id was provided')
  159. parser_build.add_argument('-t', '--target-result-id', default='',
  160. help='(optional) default select regression based on configurations unless target result '
  161. 'id was provided')
  162. parser_build.add_argument('--branch', '-B', default='master', help="Branch to find commit in")
  163. parser_build.add_argument('--branch2', help="Branch to find comparision revisions in")
  164. parser_build.add_argument('--commit', help="Revision to search for")
  165. parser_build.add_argument('--commit-number', help="Revision number to search for, redundant if --commit is specified")
  166. parser_build.add_argument('--commit2', help="Revision to compare with")
  167. parser_build.add_argument('--commit-number2', help="Revision number to compare with, redundant if --commit2 is specified")