get_single_test_cov.py 5.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 15 16 17 18 19 20 21 22 23
# 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.

import os
import sys
import re


def getFNDAFile(rootPath, test):
    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 已提交
24 25 26 27 28 29
    try:
        f = open(filename)
        print("oepn %s succesfully" % filename)
    except FileNotFoundError:
        print("%s is not found." % filename)
        return
Z
zhangchunle 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42
    lines = f.readlines()
    for line in lines:
        line = line.replace('\n', '')
        if line.startswith(('SF:')):
            os.system('echo %s >> %s' % (line, fn_filename))
        elif line.startswith(('FNDA:')):
            hit = int(line.split('FNDA:')[1].split(',')[0])
            if hit != 0:
                os.system('echo %s >> %s' % (line, fn_filename))
    f.close()


def analysisFNDAFile(rootPath, test):
43 44 45 46 47
    related_ut_map_file = '%s/build/ut_map/%s/related_%s.txt' % (
        rootPath,
        test,
        test,
    )
Z
zhangchunle 已提交
48
    notrelated_ut_map_file = '%s/build/ut_map/%s/notrelated_%s.txt' % (
49 50 51 52
        rootPath,
        test,
        test,
    )
Z
zhangchunle 已提交
53 54
    os.system('touch %s' % related_ut_map_file)
    os.system('touch %s' % notrelated_ut_map_file)
R
risemeup1 已提交
55 56 57 58 59 60 61 62 63

    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 已提交
64
    fn_filename = '%s/build/ut_map/%s/fnda.tmp' % (rootPath, test)
R
risemeup1 已提交
65 66 67 68 69 70
    try:
        f = open(fn_filename)
        print("oepn %s succesfully" % fn_filename)
    except FileNotFoundError:
        print("%s is not found." % fn_filename)
        return
Z
zhangchunle 已提交
71
    data = f.read().split('SF:')
Z
zhangchunle 已提交
72
    related_file_list = []
Z
zhangchunle 已提交
73
    for message in data:
Z
zhangchunle 已提交
74 75 76 77 78 79 80 81
        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 已提交
82
        if 'FNDA:' in message:
Z
zhangchunle 已提交
83
            OP_REGIST = True
Z
zhangchunle 已提交
84 85 86 87
            for i in range(1, len(message_list) - 1):
                fn = message_list[i]
                matchObj = re.match(
                    r'(.*)Maker(.*)|(.*)Touch(.*)Regist(.*)|(.*)Touch(.*)JitKernel(.*)|(.*)converterC2Ev(.*)',
88 89 90
                    fn,
                    re.I,
                )
Z
zhangchunle 已提交
91
                if matchObj == None:
Z
zhangchunle 已提交
92
                    OP_REGIST = False
Z
zhangchunle 已提交
93
                    break
94
            if not OP_REGIST:
Z
zhangchunle 已提交
95
                related_file_list.append(clazz_filename)
96 97 98
                os.system(
                    'echo %s >> %s' % (clazz_filename, related_ut_map_file)
                )
Z
zhangchunle 已提交
99
            else:
100 101 102
                os.system(
                    'echo %s >> %s' % (clazz_filename, notrelated_ut_map_file)
                )
Z
zhangchunle 已提交
103 104
        else:
            if clazz_filename != '':
105 106 107 108 109 110 111
                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 已提交
112 113 114 115 116 117 118
    f.close()


def getCovinfo(rootPath, test):
    ut_map_path = '%s/build/ut_map/%s' % (rootPath, test)
    os.system(
        'cd %s && lcov --capture -d . -o coverage.info --rc lcov_branch_coverage=0 > /dev/null 2>&1'
119 120
        % ut_map_path
    )
R
risemeup1 已提交
121 122 123 124 125 126 127
    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 已提交
128
    os.system(
R
risemeup1 已提交
129
        "cd %s && lcov --extract coverage.info '/paddle/paddle/phi/*' '/paddle/paddle/utils/*' '/paddle/paddle/fluid/framework/*' '/paddle/paddle/fluid/imperative/*' '/paddle/paddle/fluid/inference/*' '/paddle/paddle/fluid/memory/*' '/paddle/paddle/fluid/operators/*' '/paddle/paddle/fluid/string/*' '/paddle/paddle/fluid/distributed/*' '/paddle/paddle/fluid/platform/*' '/paddle/paddle/fluid/pybind/*' '/paddle/build/*' -o coverage.info.tmp --rc lcov_branch_coverage=0 > /dev/null 2>&1"
130 131
        % ut_map_path
    )
R
risemeup1 已提交
132 133 134 135 136 137 138 139
    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 已提交
140 141 142 143
    os.system('rm -rf %s/paddle' % ut_map_path)
    os.system('rm -rf %s/coverage.info' % ut_map_path)
    getFNDAFile(rootPath, test)
    analysisFNDAFile(rootPath, test)
R
risemeup1 已提交
144
    os.system('rm -rf %s/coverage.info.tmp' % ut_map_path)
Z
zhangchunle 已提交
145 146 147 148 149 150


if __name__ == "__main__":
    rootPath = sys.argv[1]
    case = sys.argv[2]
    getCovinfo(rootPath, case)