1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
import os
import subprocess
from typing import Dict, IO, List, Set, Tuple
from ..oss.utils import get_pytorch_folder
from ..util.setting import SUMMARY_FOLDER_DIR, TestList, TestStatusType
CoverageItem = Tuple[str, float, int, int]
def key_by_percentage(x: CoverageItem) -> float:
return x[1]
def key_by_name(x: CoverageItem) -> str:
return x[0]
def is_intrested_file(file_path: str, interested_folders: List[str]) -> bool:
if "cuda" in file_path:
return False
if "aten/gen_aten" in file_path or "aten/aten_" in file_path:
return False
for folder in interested_folders:
if folder in file_path:
return True
return False
def is_this_type_of_tests(target_name: str, test_set_by_type: Set[str]) -> bool:
# tests are divided into three types: success / partial success / fail to collect coverage
for test in test_set_by_type:
if target_name in test:
return True
return False
def print_test_by_type(
tests: TestList, test_set_by_type: Set[str], type_name: str, summary_file: IO[str]
) -> None:
print("Tests " + type_name + " to collect coverage:", file=summary_file)
for test in tests:
if is_this_type_of_tests(test.name, test_set_by_type):
print(test.target_pattern, file=summary_file)
print(file=summary_file)
def print_test_condition(
tests: TestList,
tests_type: TestStatusType,
interested_folders: List[str],
coverage_only: List[str],
summary_file: IO[str],
summary_type: str,
) -> None:
print_test_by_type(tests, tests_type["success"], "fully success", summary_file)
print_test_by_type(tests, tests_type["partial"], "partially success", summary_file)
print_test_by_type(tests, tests_type["fail"], "failed", summary_file)
print(
"\n\nCoverage Collected Over Interested Folders:\n",
interested_folders,
file=summary_file,
)
print(
"\n\nCoverage Compilation Flags Only Apply To: \n",
coverage_only,
file=summary_file,
)
print(
"\n\n---------------------------------- "
+ summary_type
+ " ----------------------------------",
file=summary_file,
)
def line_oriented_report(
tests: TestList,
tests_type: TestStatusType,
interested_folders: List[str],
coverage_only: List[str],
covered_lines: Dict[str, Set[int]],
uncovered_lines: Dict[str, Set[int]],
) -> None:
with open(os.path.join(SUMMARY_FOLDER_DIR, "line_summary"), "w+") as report_file:
print_test_condition(
tests,
tests_type,
interested_folders,
coverage_only,
report_file,
"LINE SUMMARY",
)
for file_name in covered_lines:
covered = covered_lines[file_name]
uncovered = uncovered_lines[file_name]
print(
f"{file_name}\n covered lines: {sorted(covered)}\n unconvered lines:{sorted(uncovered)}",
file=report_file,
)
def print_file_summary(
covered_summary: int, total_summary: int, summary_file: IO[str]
) -> float:
# print summary first
try:
coverage_percentage = 100.0 * covered_summary / total_summary
except ZeroDivisionError:
coverage_percentage = 0
print(
f"SUMMARY\ncovered: {covered_summary}\nuncovered: {total_summary}\npercentage: {coverage_percentage:.2f}%\n\n",
file=summary_file,
)
if coverage_percentage == 0:
print("Coverage is 0, Please check if json profiles are valid")
return coverage_percentage
def print_file_oriented_report(
tests_type: TestStatusType,
coverage: List[CoverageItem],
covered_summary: int,
total_summary: int,
summary_file: IO[str],
tests: TestList,
interested_folders: List[str],
coverage_only: List[str],
) -> None:
coverage_percentage = print_file_summary(
covered_summary, total_summary, summary_file
)
# print test condition (interested folder / tests that are successsful or failed)
print_test_condition(
tests,
tests_type,
interested_folders,
coverage_only,
summary_file,
"FILE SUMMARY",
)
# print each file's information
for item in coverage:
print(
item[0].ljust(75),
(str(item[1]) + "%").rjust(10),
str(item[2]).rjust(10),
str(item[3]).rjust(10),
file=summary_file,
)
print(f"summary percentage:{coverage_percentage:.2f}%")
def file_oriented_report(
tests: TestList,
tests_type: TestStatusType,
interested_folders: List[str],
coverage_only: List[str],
covered_lines: Dict[str, Set[int]],
uncovered_lines: Dict[str, Set[int]],
) -> None:
with open(os.path.join(SUMMARY_FOLDER_DIR, "file_summary"), "w+") as summary_file:
covered_summary = 0
total_summary = 0
coverage = []
for file_name in covered_lines:
# get coverage number for this file
covered_count = len(covered_lines[file_name])
total_count = covered_count + len(uncovered_lines[file_name])
try:
percentage = round(covered_count / total_count * 100, 2)
except ZeroDivisionError:
percentage = 0
# store information in a list to be sorted
coverage.append((file_name, percentage, covered_count, total_count))
# update summary
covered_summary = covered_summary + covered_count
total_summary = total_summary + total_count
# sort
coverage.sort(key=key_by_name)
coverage.sort(key=key_by_percentage)
# print
print_file_oriented_report(
tests_type,
coverage,
covered_summary,
total_summary,
summary_file,
tests,
interested_folders,
coverage_only,
)
def get_html_ignored_pattern() -> List[str]:
return ["/usr/*", "*anaconda3/*", "*third_party/*"]
def html_oriented_report() -> None:
# use lcov to generate the coverage report
build_folder = os.path.join(get_pytorch_folder(), "build")
coverage_info_file = os.path.join(SUMMARY_FOLDER_DIR, "coverage.info")
# generage coverage report -- coverage.info in build folder
subprocess.check_call(
[
"lcov",
"--capture",
"--directory",
build_folder,
"--output-file",
coverage_info_file,
]
)
# remove files that are unrelated
cmd_array = (
["lcov", "--remove", coverage_info_file]
+ get_html_ignored_pattern()
+ ["--output-file", coverage_info_file]
)
subprocess.check_call(
# ["lcov", "--remove", coverage_info_file, "--output-file", coverage_info_file]
cmd_array
)
# generate beautiful html page
subprocess.check_call(
[
"genhtml",
coverage_info_file,
"--output-directory",
os.path.join(SUMMARY_FOLDER_DIR, "html_report"),
]
)
|