get_single_test_cov.py 9.5 KB
Newer Older
Z
zhangchunle 已提交
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
Z
zhangchunle 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
6
#
Z
zhangchunle 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
Z
zhangchunle 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import json
Z
zhangchunle 已提交
16 17
import os
import re
Z
zhangbo9674 已提交
18
import subprocess
19
import sys
R
risemeup1 已提交
20
import time
Z
zhangchunle 已提交
21 22 23


def getFNDAFile(rootPath, test):
24 25 26 27 28 29 30 31 32 33 34
    # load base fnda
    fnda_base_dict = {}
    find_file_cmd = os.popen("find %s -name %s.cc" % (rootPath, test))
    if find_file_cmd.read() != "":
        print("%s is a c++ unittest" % test)
        with open(
            "%s/build/ut_map/simple_precision_test/base_fnda.json" % rootPath,
            'r',
        ) as load_f:
            fnda_base_dict = json.load(load_f)
    # analyse fnda
Z
zhangchunle 已提交
35 36 37
    filename = '%s/build/ut_map/%s/coverage.info.tmp' % (rootPath, test)
    fn_filename = '%s/build/ut_map/%s/fnda.tmp' % (rootPath, test)
    os.system('touch %s' % fn_filename)
R
risemeup1 已提交
38 39 40 41 42 43
    try:
        f = open(filename)
        print("oepn %s succesfully" % filename)
    except FileNotFoundError:
        print("%s is not found." % filename)
        return
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    all_data = f.read().split('TN:')
    del all_data[0]
    for gcov_data in all_data:
        message_list = gcov_data.split('\n')
        os.system('echo %s >> %s' % (message_list[1], fn_filename))
        if 'FNH:0' not in gcov_data:
            for message in message_list:
                if message.startswith(('FNDA:')) and (
                    not message.startswith(('FNDA:0,'))
                ):
                    tmp_data = message.split('FNDA:')[1].split(',')
                    hit = int(tmp_data[0])
                    symbol = tmp_data[1]
                    if symbol in fnda_base_dict:
                        if (hit - fnda_base_dict[symbol]) > 0:
                            fnda_str = 'FNDA:%s,%s' % (
                                str(hit - fnda_base_dict[symbol]),
                                symbol,
                            )
                            os.system('echo %s >> %s' % (fnda_str, fn_filename))
                    else:
                        os.system('echo %s >> %s' % (message, fn_filename))
Z
zhangchunle 已提交
66 67 68 69
    f.close()


def analysisFNDAFile(rootPath, test):
70 71 72 73 74
    related_ut_map_file = '%s/build/ut_map/%s/related_%s.txt' % (
        rootPath,
        test,
        test,
    )
Z
zhangchunle 已提交
75
    notrelated_ut_map_file = '%s/build/ut_map/%s/notrelated_%s.txt' % (
76 77 78 79
        rootPath,
        test,
        test,
    )
Z
zhangchunle 已提交
80 81
    os.system('touch %s' % related_ut_map_file)
    os.system('touch %s' % notrelated_ut_map_file)
R
risemeup1 已提交
82 83 84 85 86 87 88 89 90

    if os.path.isfile(related_ut_map_file) and os.path.isfile(
        notrelated_ut_map_file
    ):
        print("make related.txt and not_related.txt succesfully")
    else:
        print("make related.txt and not_related.txt failed")
        return

Z
zhangchunle 已提交
91
    fn_filename = '%s/build/ut_map/%s/fnda.tmp' % (rootPath, test)
R
risemeup1 已提交
92 93 94 95 96 97
    try:
        f = open(fn_filename)
        print("oepn %s succesfully" % fn_filename)
    except FileNotFoundError:
        print("%s is not found." % fn_filename)
        return
Z
zhangchunle 已提交
98
    data = f.read().split('SF:')
Z
zhangchunle 已提交
99
    related_file_list = []
Z
zhangchunle 已提交
100
    for message in data:
Z
zhangchunle 已提交
101 102 103 104 105 106 107 108
        message_list = message.split('\n')
        clazz_filename = message_list[0]
        if '/build/' in clazz_filename:
            clazz_filename = clazz_filename.replace('/build', '')
        if '.pb.h' in clazz_filename:
            clazz_filename = clazz_filename.replace('.pb.h', '.proto')
        if '.pb.cc' in clazz_filename:
            clazz_filename = clazz_filename.replace('.pb.cc', '.proto')
Z
zhangchunle 已提交
109
        if 'FNDA:' in message:
Z
zhangchunle 已提交
110
            OP_REGIST = True
Z
zhangchunle 已提交
111 112 113 114
            for i in range(1, len(message_list) - 1):
                fn = message_list[i]
                matchObj = re.match(
                    r'(.*)Maker(.*)|(.*)Touch(.*)Regist(.*)|(.*)Touch(.*)JitKernel(.*)|(.*)converterC2Ev(.*)',
115 116 117
                    fn,
                    re.I,
                )
118
                if matchObj is None:
Z
zhangchunle 已提交
119
                    OP_REGIST = False
Z
zhangchunle 已提交
120
                    break
121
            if not OP_REGIST:
Z
zhangchunle 已提交
122
                related_file_list.append(clazz_filename)
123 124 125
                os.system(
                    'echo %s >> %s' % (clazz_filename, related_ut_map_file)
                )
Z
zhangchunle 已提交
126
            else:
127 128 129
                os.system(
                    'echo %s >> %s' % (clazz_filename, notrelated_ut_map_file)
                )
Z
zhangchunle 已提交
130 131
        else:
            if clazz_filename != '':
132 133 134 135 136 137 138
                if (
                    clazz_filename not in related_file_list
                ):  # xx.pb.cc in RELATED xx.pb.h not in RELATED
                    os.system(
                        'echo %s >> %s'
                        % (clazz_filename, notrelated_ut_map_file)
                    )
Z
zhangchunle 已提交
139 140 141
    f.close()


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
def getBaseFnda(rootPath, test):
    filename = '%s/build/ut_map/%s/coverage.info.tmp' % (rootPath, test)
    try:
        f = open(filename)
        print("oepn %s succesfully" % filename)
    except FileNotFoundError:
        print("%s is not found." % filename)
    symbol_fnda = {}
    all_data = f.read().split('TN:')
    del all_data[0]
    for gcov_data in all_data:
        message_list = gcov_data.split('\n')
        # only for cc file
        if ".cc" in message_list[1]:
            for message in message_list:
                if message.startswith(('FNDA:')) and (
                    not message.startswith(('FNDA:0,'))
                ):
                    tmp_data = message.split('FNDA:')[1].split(',')
                    symbol_fnda[tmp_data[1]] = int(tmp_data[0])
    f.close()

    with open("%s/build/ut_map/%s/base_fnda.json" % (rootPath, test), "w") as f:
        json.dump(symbol_fnda, f, indent=4)


Z
zhangchunle 已提交
168 169
def getCovinfo(rootPath, test):
    ut_map_path = '%s/build/ut_map/%s' % (rootPath, test)
Z
zhangbo9674 已提交
170
    print("start get fluid ===>")
171
    cmd_fluid = (
R
risemeup1 已提交
172
        'cd %s && lcov --capture -d paddle/fluid/ -o paddle/fluid/coverage_fluid.info --rc lcov_branch_coverage=0'
173 174
        % ut_map_path
    )
Z
zhangbo9674 已提交
175 176 177
    p_fluid = subprocess.Popen(cmd_fluid, shell=True, stdout=subprocess.DEVNULL)

    print("start get phi ===>")
178
    cmd_phi = (
R
risemeup1 已提交
179
        'cd %s && lcov --capture -d paddle/phi -o paddle/phi/coverage_phi.info --rc lcov_branch_coverage=0'
180 181
        % ut_map_path
    )
R
risemeup1 已提交
182 183
    if os.path.exists("%s/paddle/phi" % ut_map_path):
        p_phi = subprocess.Popen(cmd_phi, shell=True, stdout=subprocess.DEVNULL)
Z
zhangbo9674 已提交
184 185

    print("start get utils ===>")
186
    cmd_utils = (
R
risemeup1 已提交
187
        'cd %s && lcov --capture -d paddle/utils -o paddle/utils/coverage_utils.info --rc lcov_branch_coverage=0'
188 189
        % ut_map_path
    )
R
risemeup1 已提交
190 191 192 193
    if os.path.exists("%s/paddle/utils" % ut_map_path):
        p_utils = subprocess.Popen(
            cmd_utils, shell=True, stdout=subprocess.DEVNULL
        )
194
    print("start wait fluid ===>")
Z
zhangbo9674 已提交
195
    p_fluid.wait()
196
    print("start wait phi ===>")
Z
zhangbo9674 已提交
197
    p_phi.wait()
198
    print("start wait utils ===>")
Z
zhangbo9674 已提交
199 200
    p_utils.wait()
    print("end wait...")
R
risemeup1 已提交
201 202
    coverage_utils_info_path = (
        "%s/paddle/utils/coverage_utils.info" % ut_map_path
203
    )
R
risemeup1 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
    if (
        os.path.exists(coverage_utils_info_path)
        and os.path.getsize(coverage_utils_info_path) != 0
    ):
        os.system(
            'cd %s && lcov -a paddle/fluid/coverage_fluid.info -a paddle/phi/coverage_phi.info -a paddle/utils/coverage_utils.info -o coverage.info --rc lcov_branch_coverage=0 > /dev/null 2>&1'
            % ut_map_path
        )
    else:
        os.system(
            'cd %s && lcov -a paddle/fluid/coverage_fluid.info -a paddle/phi/coverage_phi.info -o coverage.info --rc lcov_branch_coverage=0 > /dev/null 2>&1'
            % ut_map_path
        )
R
risemeup1 已提交
217 218 219 220 221 222 223
    coverage_info_path = ut_map_path + '/coverage.info'
    file_size = os.path.getsize(coverage_info_path)
    if file_size == 0:
        print("coverage.info is empty,collect coverage rate failed")
        return
    else:
        print("get coverage.info succesfully")
Z
zhangchunle 已提交
224
    os.system(
R
risemeup1 已提交
225
        "cd %s && lcov --extract coverage.info '/paddle/paddle/phi/*' '/paddle/paddle/utils/*' '/paddle/paddle/fluid/*' '/paddle/build/*' -o coverage.info.tmp --rc lcov_branch_coverage=0 > /dev/null 2>&1"
226 227
        % ut_map_path
    )
R
risemeup1 已提交
228 229 230 231 232 233 234 235
    coverage_info_tmp = ut_map_path + '/coverage.info.tmp'
    coverage_tmp_size = os.path.getsize(coverage_info_tmp)
    if coverage_tmp_size == 0:
        print("coverage.info.tmp is empty,collect coverage rate failed")
        return
    else:
        print("get coverage.info.tmp succesfully")

Z
zhangchunle 已提交
236 237
    os.system('rm -rf %s/paddle' % ut_map_path)
    os.system('rm -rf %s/coverage.info' % ut_map_path)
238 239 240
    if test == "simple_precision_test":
        getBaseFnda(rootPath, test)
    else:
R
risemeup1 已提交
241
        start_getFNDAFile = time.time()
242
        getFNDAFile(rootPath, test)
R
risemeup1 已提交
243 244 245
        end_getFNDAFile = time.time()
        print("getFNDAFile time:", end_getFNDAFile - start_getFNDAFile)
        start_analysisFNDAFile = time.time()
246
        analysisFNDAFile(rootPath, test)
R
risemeup1 已提交
247 248 249 250 251
        end_analysisFNDAFile = time.time()
        print(
            "analysisFNDAFile time :",
            end_analysisFNDAFile - start_analysisFNDAFile,
        )
R
risemeup1 已提交
252
    os.system('rm -rf %s/coverage.info.tmp' % ut_map_path)
Z
zhangchunle 已提交
253 254 255 256 257


if __name__ == "__main__":
    rootPath = sys.argv[1]
    case = sys.argv[2]
R
risemeup1 已提交
258
    start_getCovinfo = time.time()
Z
zhangchunle 已提交
259
    getCovinfo(rootPath, case)
R
risemeup1 已提交
260 261
    end_getCovinfo = time.time()
    print("getConvinfo time :", end_getCovinfo - start_getCovinfo)