get_ut_file_map.py 8.4 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 18 19 20 21
import os
import sys


def get_all_paddle_file(rootPath):
    """get all file in Paddle repo: paddle/fluild, python"""
Z
zhangchunle 已提交
22
    traverse_files = ['%s' % rootPath]
Z
zhangchunle 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36
    all_file_paddle = '%s/build/all_file_paddle' % rootPath
    all_file_paddle_list = []
    with open(all_file_paddle, 'w') as f:
        for filename in traverse_files:
            g = os.walk(filename)
            for path, dir_list, file_list in g:
                for file_name in file_list:
                    all_file_paddle_list.append(os.path.join(path, file_name))
    return all_file_paddle_list


def get_all_uts(rootPath):
    all_uts_paddle = '%s/build/all_uts_paddle' % rootPath
    os.system(
37
        r'cd %s/build && ctest -N -V | grep -Ei "Test[ \t]+#" | grep -oEi "\w+$" > %s'
38 39
        % (rootPath, all_uts_paddle)
    )
Z
zhangchunle 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58


def remove_useless_file(rootPath):
    """remove useless file in ut_file_map.json"""
    all_file_paddle_list = get_all_paddle_file(rootPath)
    ut_file_map_new = {}
    ut_file_map = "%s/build/ut_file_map.json" % rootPath
    with open(ut_file_map, 'r') as load_f:
        load_dict = json.load(load_f)
    for key in load_dict:
        if key in all_file_paddle_list:
            ut_file_map_new[key] = load_dict[key]

    with open("%s/build/ut_file_map.json" % rootPath, "w") as f:
        json.dump(ut_file_map_new, f, indent=4)
        print("remove_useless_file ut_file_map success!!")


def handle_ut_file_map(rootPath):
Z
zhangchunle 已提交
59
    utNotSuccess_list = []
Z
zhangchunle 已提交
60 61 62 63 64
    ut_map_path = "%s/build/ut_map" % rootPath
    files = os.listdir(ut_map_path)
    ut_file_map = {}
    count = 0
    not_success_file = open("%s/build/prec_delta" % rootPath, 'w')
R
risemeup1 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    # if testdir is not made,write the test into prec_delta
    get_all_uts(rootPath)
    all_ut = '%s/build/all_uts_paddle' % rootPath
    with open(all_ut, 'r') as f:
        all_ut_list = []
        for ut in f.readlines():
            ut = ut.replace('\n', '')
            all_ut_list.append(ut.strip())
        f.close()
    for ut in all_ut_list:
        filedir = '%s/build/ut_map/%s' % (rootPath, ut)
        if not os.path.exists(filedir):
            not_success_file.write('%s\n' % ut)
            utNotSuccess_list.append(ut)
    # if fnda.tmp not exists,write the test into prec_delta
Z
zhangchunle 已提交
80 81 82
    for ut in files:
        count = count + 1
        print("ut %s: %s" % (count, ut))
R
risemeup1 已提交
83
        coverage_info = '%s/%s/fnda.tmp' % (ut_map_path, ut)
Z
zhangchunle 已提交
84
        if os.path.exists(coverage_info):
Z
zhangchunle 已提交
85
            filename = '%s/%s/related_%s.txt' % (ut_map_path, ut, ut)
R
risemeup1 已提交
86 87 88 89 90 91
            try:
                f = open(filename)
                print("oepn %s succesfully" % filename)
            except FileNotFoundError:
                print("%s is not found." % filename)
                return
Z
zhangchunle 已提交
92 93 94 95 96 97 98
            lines = f.readlines()
            for line in lines:
                line = line.replace('\n', '').strip()
                if line == '':
                    continue
                elif line.startswith('/paddle/build'):
                    source_file = line.replace('/build', '')
99
                    # source_file = re.sub('.pb.*', '.proto', source_file)
Z
zhangchunle 已提交
100
                elif 'precise test map fileeee:' in line:
101 102 103
                    source_file = line.split('precise test map fileeee:')[
                        1
                    ].strip()
Z
zhangchunle 已提交
104 105 106 107 108 109
                else:
                    source_file = line
                if source_file not in ut_file_map:
                    ut_file_map[source_file] = []
                if ut not in ut_file_map[source_file]:
                    ut_file_map[source_file].append(ut)
R
risemeup1 已提交
110
            f.close()
Z
zhangchunle 已提交
111 112
        else:
            not_success_file.write('%s\n' % ut)
Z
zhangchunle 已提交
113
            utNotSuccess_list.append(ut)
Z
zhangchunle 已提交
114 115
    not_success_file.close()

Z
zhangchunle 已提交
116 117 118 119 120 121
    print("utNotSuccess:")
    print(utNotSuccess_list)

    for ut in files:
        if ut not in utNotSuccess_list:
            filename = '%s/%s/notrelated_%s.txt' % (ut_map_path, ut, ut)
R
risemeup1 已提交
122 123 124 125 126
            try:
                f = open(filename)
                print("oepn %s succesfully" % filename)
            except FileNotFoundError:
                print("%s is not found." % filename)
Z
zhangchunle 已提交
127 128 129 130 131 132 133 134 135 136 137
            lines = f.readlines()
            for line in lines:
                line = line.replace('\n', '').strip()
                if line == '':
                    continue
                elif line.startswith('/paddle/build'):
                    source_file = line.replace('/build', '')
                else:
                    source_file = line
                if source_file not in ut_file_map:
                    ut_file_map[source_file] = []
R
risemeup1 已提交
138
            f.close()
Z
zhangchunle 已提交
139 140 141 142 143 144 145 146 147
    with open("%s/build/ut_file_map.json" % rootPath, "w") as f:
        json.dump(ut_file_map, f, indent=4)


def notsuccessfuc(rootPath):
    utNotSuccess = ''
    ut_map_path = "%s/build/ut_map" % rootPath
    files = os.listdir(ut_map_path)
    count = 0
R
risemeup1 已提交
148

Z
zhangchunle 已提交
149 150
    # ut failed!!
    for ut in files:
R
risemeup1 已提交
151 152
        if ut == 'simple_precise_test':
            continue
R
risemeup1 已提交
153
        coverage_info = '%s/%s/fnda.tmp' % (ut_map_path, ut)
Z
zhangchunle 已提交
154 155 156 157 158 159 160
        if os.path.exists(coverage_info):
            pass
        else:
            count = count + 1
            utNotSuccess = utNotSuccess + '^%s$|' % ut

    # ut not exec
R
risemeup1 已提交
161

Z
zhangchunle 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    get_all_uts(rootPath)
    with open("/paddle/build/all_uts_paddle", "r") as f:
        data = f.readlines()
    for ut in data:
        ut = ut.replace('\n', '').strip()
        if ut not in files:
            print(ut)
            count = count + 1
            utNotSuccess = utNotSuccess + '^%s$|' % ut

    if utNotSuccess != '':
        print("utNotSuccess count: %s" % count)
        f = open('%s/build/utNotSuccess' % rootPath, 'w')
        f.write(utNotSuccess[:-1])
        f.close()


def ut_file_map_supplement(rootPath):
    ut_file_map_new = "%s/build/ut_file_map.json" % rootPath
R
risemeup1 已提交
181 182
    precision_test_map_store_dir = "/precision_test_map_store"
    os.system('mkdir %s' % precision_test_map_store_dir)
Z
zhangchunle 已提交
183
    os.system(
R
risemeup1 已提交
184 185
        'cd %s && wget --no-proxy https://paddle-docker-tar.bj.bcebos.com/tmp_test/ut_file_map.json --no-check-certificate'
        % precision_test_map_store_dir
Z
zhangchunle 已提交
186
    )
R
risemeup1 已提交
187
    ut_file_map_old = "%s/ut_file_map.json" % precision_test_map_store_dir
Z
zhangchunle 已提交
188 189 190
    with open(ut_file_map_new, 'r') as load_f:
        load_dict_new = json.load(load_f)

Z
zhangchunle 已提交
191
    all_uts_paddle = '%s/build/all_uts_paddle' % rootPath
R
risemeup1 已提交
192

Z
zhangchunle 已提交
193 194 195 196 197
    with open(all_uts_paddle, 'r') as f:
        all_uts_paddle_list = []
        for ut in f.readlines():
            all_uts_paddle_list.append(ut.strip())
        f.close()
Z
zhangchunle 已提交
198

R
risemeup1 已提交
199
    with open("%s/ut_file_map.json" % precision_test_map_store_dir, "w") as f:
Z
zhangchunle 已提交
200 201
        json.dump(load_dict_new, f, indent=4)
        print("load_dict_new success!!")
Z
zhangchunle 已提交
202 203

    os.system(
R
risemeup1 已提交
204 205
        'cd %s && wget --no-proxy https://paddle-docker-tar.bj.bcebos.com/tmp_test/prec_delta --no-check-certificate'
        % precision_test_map_store_dir
Z
zhangchunle 已提交
206 207 208
    )
    prec_delta_new = "%s/build/prec_delta" % rootPath
    with open(prec_delta_new, 'r') as f:
Z
zhangchunle 已提交
209 210 211
        prec_delta_new_list = []
        for ut in f.readlines():
            prec_delta_new_list.append(ut.strip())
Z
zhangchunle 已提交
212
        f.close()
Z
zhangchunle 已提交
213
    prec_delta_new_list.append(
214 215
        'test_py_reader_error_msg'
    )  # add a python case for pycoverage
R
risemeup1 已提交
216
    prec_delta_file = open("%s/prec_delta" % precision_test_map_store_dir, 'w')
Z
zhangchunle 已提交
217
    for ut in prec_delta_new_list:
Z
zhangchunle 已提交
218 219
        prec_delta_file.write(ut + '\n')
    print("prec_delta_file success!!")
Z
zhangchunle 已提交
220 221 222
    prec_delta_file.close()


Z
zhangchunle 已提交
223 224 225 226 227 228 229 230 231
def utmap_analysis(rootPath):
    ut_file_map_new = "%s/build/ut_file_map.json" % rootPath
    with open(ut_file_map_new, 'r') as load_f:
        load_dict_new = json.load(load_f)
    print(len(load_dict_new))
    for filename in load_dict_new:
        print(filename, len(load_dict_new[filename]))


Z
zhangchunle 已提交
232 233 234 235 236 237 238 239 240 241
if __name__ == "__main__":
    func = sys.argv[1]
    if func == 'get_not_success_ut':
        rootPath = sys.argv[2]
        notsuccessfuc(rootPath)
    elif func == 'get_ut_map':
        rootPath = sys.argv[2]
        handle_ut_file_map(rootPath)
        remove_useless_file(rootPath)
        ut_file_map_supplement(rootPath)