paddle_coverage_new.sh 9.1 KB
Newer Older
Y
YUNSHEN XIE 已提交
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
#!/usr/bin/env bash

# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
# 
# 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
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# 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.

set -xe

PADDLE_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}")/../../" && pwd )"

function lcov_init(){
    # install lcov
    if [ ! -f "/root/.cache/lcov-1.14.tar.gz" ];then
        wget -P /home https://paddle-ci.gz.bcebos.com/coverage/lcov-1.14.tar.gz --no-proxy --no-check-certificate || exit 101 
        cp /home/lcov-1.14.tar.gz /root/.cache/lcov-1.14.tar.gz
    else
        cp /root/.cache/lcov-1.14.tar.gz /home/lcov-1.14.tar.gz
    fi
    tar -xf /home/lcov-1.14.tar.gz -C /
    cd /lcov-1.14
    make install
Y
YUNSHEN XIE 已提交
32
    cd -
Y
YUNSHEN XIE 已提交
33 34 35 36 37
}

function gen_cpp_covinfo(){
    # run paddle coverage
    cd /paddle/build
Y
YUNSHEN XIE 已提交
38
    python ${PADDLE_ROOT}/tools/coverage/gcda_clean.py ${GIT_PR_ID} || exit 101
Y
YUNSHEN XIE 已提交
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
    lcov --capture -d ./ -o coverage.info --rc lcov_branch_coverage=0
}


# full html report

function gen_full_html_report() {
    lcov --extract coverage.info \
        '/paddle/paddle/fluid/framework/*' \
        '/paddle/paddle/fluid/imperative/*' \
        '/paddle/paddle/fluid/inference/*' \
        '/paddle/paddle/fluid/memory/*' \
        '/paddle/paddle/fluid/operators/*' \
        '/paddle/paddle/fluid/recordio/*' \
        '/paddle/paddle/fluid/string/*' \
        '/paddle/paddle/fluid/eager/*' \
        '/paddle/paddle/phi/*' \
        '/paddle/paddle/utils/*' \
        -o coverage-full.tmp \
        --rc lcov_branch_coverage=0

    mv -f coverage-full.tmp coverage-full.info

    lcov --remove coverage-full.info \
        '/paddle/paddle/fluid/framework/*_test*' \
        '/paddle/paddle/fluid/*/*test*' \
        '/paddle/paddle/fluid/*/*/*test*' \
        '/paddle/paddle/fluid/inference/tests/*' \
        '/paddle/paddle/fluid/inference/api/demo_ci/*' \
        '/paddle/paddle/fluid/eager/tests/*' \
        '/paddle/paddle/phi/tests/*' \
        -o coverage-full.tmp \
        --rc lcov_branch_coverage=0

    mv -f coverage-full.tmp coverage-full.info
}

function gen_full_html_report_xpu() {
    lcov --extract coverage.info \
        '/paddle/paddle/fluid/operators/*xpu*' \
        '/paddle/paddle/phi/kernels/xpu/*' \
        -o coverage-full.tmp \
        --rc lcov_branch_coverage=0

    mv -f coverage-full.tmp coverage-full.info

    lcov --remove coverage-full.info \
        '/paddle/paddle/fluid/framework/*_test*' \
        '/paddle/paddle/fluid/*/*test*' \
        '/paddle/paddle/fluid/*/*/*test*' \
        '/paddle/paddle/fluid/inference/tests/*' \
        '/paddle/paddle/fluid/inference/api/demo_ci/*' \
        -o coverage-full.tmp \
        --rc lcov_branch_coverage=0

    mv -f coverage-full.tmp coverage-full.info
}

function gen_full_html_report_npu() {
    lcov --extract coverage.info \
        '/paddle/paddle/fluid/operators/*npu*' \
        -o coverage-full.tmp \
        --rc lcov_branch_coverage=0

    mv -f coverage-full.tmp coverage-full.info

    lcov --remove coverage-full.info \
        '/paddle/paddle/fluid/framework/*_test*' \
        '/paddle/paddle/fluid/*/*test*' \
        '/paddle/paddle/fluid/*/*/*test*' \
        '/paddle/paddle/fluid/inference/tests/*' \
        '/paddle/paddle/fluid/inference/api/demo_ci/*' \
        -o coverage-full.tmp \
        --rc lcov_branch_coverage=0

    mv -f coverage-full.tmp coverage-full.info
}

# if [ ${WITH_XPU:-OFF} == "ON" ]; then
#     gen_full_html_report_xpu || true
# elif [ ${WITH_ASCEND_CL:-OFF} == "ON" ]; then
#     gen_full_html_report_npu || true
# else
#     gen_full_html_report || true
# fi

# diff html report

function gen_diff_html_report() {
    if [ "${GIT_PR_ID}" != "" ]; then

Y
YUNSHEN XIE 已提交
130
        COVERAGE_DIFF_PATTERN="`python ${PADDLE_ROOT}/tools/coverage/pull_request.py files ${GIT_PR_ID}`"
Y
YUNSHEN XIE 已提交
131

Y
YUNSHEN XIE 已提交
132
        python ${PADDLE_ROOT}/tools/coverage/pull_request.py diff ${GIT_PR_ID} > git-diff.out
Y
YUNSHEN XIE 已提交
133 134 135 136 137 138 139
    fi

    lcov --extract coverage-full.info \
        ${COVERAGE_DIFF_PATTERN} \
        -o coverage-diff.info \
        --rc lcov_branch_coverage=0

Y
YUNSHEN XIE 已提交
140
    python ${PADDLE_ROOT}/tools/coverage/coverage_diff.py coverage-diff.info git-diff.out > coverage-diff.tmp
Y
YUNSHEN XIE 已提交
141 142 143 144 145 146 147 148 149 150 151 152

    mv -f coverage-diff.tmp coverage-diff.info

    genhtml -o coverage-diff -t 'Diff Coverage' --no-function-coverage --no-branch-coverage coverage-diff.info
}

# gen_diff_html_report || true

function gen_py_covinfo(){
    # python coverage

    export COVERAGE_FILE=/paddle/build/python-coverage.data
Y
YUNSHEN XIE 已提交
153 154
    coverage combine $(ls python-coverage.data.*) || NO_PYTHON_COVERAGE_DATA=1
    $(coverage xml -i -o python-coverage.xml) || [[ "${NO_PYTHON_COVERAGE_DATA}" == "1" ]]
Y
YUNSHEN XIE 已提交
155
    sed -i 's/mnt\/paddle/paddle/g' python-coverage.xml
Y
YUNSHEN XIE 已提交
156
    $(python ${PADDLE_ROOT}/tools/coverage/python_coverage.py > python-coverage.info) || [[ "${NO_PYTHON_COVERAGE_DATA}" == "1" ]]
Y
YUNSHEN XIE 已提交
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
}


# python full html report
#
function gen_python_full_html_report() {
    lcov --extract python-coverage.info \
        '/paddle/python/*' \
        -o python-coverage-full.tmp \
        --rc lcov_branch_coverage=0

    mv -f python-coverage-full.tmp python-coverage-full.info

    lcov --remove python-coverage-full.info \
        '/*/tests/*' \
        -o python-coverage-full.tmp \
        --rc lcov_branch_coverage=0

    mv -f python-coverage-full.tmp python-coverage-full.info
}

# gen_python_full_html_report || true

# python diff html report

function gen_python_diff_html_report() {
    if [ "${GIT_PR_ID}" != "" ]; then
Y
YUNSHEN XIE 已提交
184
        COVERAGE_DIFF_PATTERN="`python ${PADDLE_ROOT}/tools/coverage/pull_request.py files ${GIT_PR_ID}`"
Y
YUNSHEN XIE 已提交
185

Y
YUNSHEN XIE 已提交
186
        python ${PADDLE_ROOT}/tools/coverage/pull_request.py diff ${GIT_PR_ID} > python-git-diff.out
Y
YUNSHEN XIE 已提交
187 188 189 190 191 192 193
    fi

    lcov --extract python-coverage-full.info \
        ${COVERAGE_DIFF_PATTERN} \
        -o python-coverage-diff.info \
        --rc lcov_branch_coverage=0

Y
YUNSHEN XIE 已提交
194
    python ${PADDLE_ROOT}/tools/coverage/coverage_diff.py python-coverage-diff.info python-git-diff.out > python-coverage-diff.tmp
Y
YUNSHEN XIE 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    mv -f python-coverage-diff.tmp python-coverage-diff.info

    genhtml -o python-coverage-diff \
        -t 'Python Diff Coverage' \
        --no-function-coverage \
        --no-branch-coverage \
        --ignore-errors source \
        python-coverage-diff.info
}

# gen_python_diff_html_report || true

# assert coverage lines

function covinfo_combine_full(){
Y
YUNSHEN XIE 已提交
211 212
    if [ -f "other-coverage.info" ] && [ -s "other-coverage.info" ];then
        if [ -f "infer-coverage.info" ] && [ -s "infer-coverage.info" ];then
Y
YUNSHEN XIE 已提交
213 214 215 216
            lcov -a other-coverage.info -a infer-coverage.info -o coverage.info
        else
            mv other-coverage.info coverage.info
        fi
Y
YUNSHEN XIE 已提交
217
    elif [ -f "infer-coverage.info" ] && [ -s "infer-coverage.info" ];then
Y
YUNSHEN XIE 已提交
218 219 220 221 222
        mv infer-coverage.info coverage.info
    else
        echo "Cannot found coverage.info"
    fi

Y
YUNSHEN XIE 已提交
223 224 225
    if [ -f "other-python-coverage.info" ] && [ -s "other-python-coverage.info" ];then
        if [ -f "infer-python-coverage.info" ] && [ -s "other-python-coverage.info" ];then
            lcov -a other-python-coverage.info -a infer-python-coverage.info -o python-coverage.info
Y
YUNSHEN XIE 已提交
226
        else
Y
YUNSHEN XIE 已提交
227
            mv other-python-coverage.info python-coverage.info
Y
YUNSHEN XIE 已提交
228
        fi
Y
YUNSHEN XIE 已提交
229 230
    elif [ -f "infer-python-coverage.info" ] && [ -s "infer-python-coverage.info" ];then
        mv infer-python-coverage.info python-coverage.info
Y
YUNSHEN XIE 已提交
231 232 233
    else
        echo "Cannot found python coverage.info"
    fi  
Y
YUNSHEN XIE 已提交
234 235
    gen_python_full_html_report || true
    gen_full_html_report || true
Y
YUNSHEN XIE 已提交
236 237 238 239
}

function cov_rate_judge(){
    echo "Assert CPP Diff Coverage"
Y
YUNSHEN XIE 已提交
240
    python ${PADDLE_ROOT}/tools/coverage/coverage_lines.py coverage-diff.info 0.9 || COVERAGE_LINES_ASSERT=1
Y
YUNSHEN XIE 已提交
241 242 243 244 245 246 247 248 249

    echo "Assert Python Diff Coverage"

    if [ ${WITH_XPU:-OFF} == "ON" ]; then
        echo "XPU has no python coverage!"
    elif [ ${WITH_ASCEND_CL:-OFF} == "ON" ]; then
        echo "NPU has no python coverage!"
    else
        if [[ python-coverage-diff.info ]];then
Y
YUNSHEN XIE 已提交
250
            python ${PADDLE_ROOT}/tools/coverage/coverage_lines.py python-coverage-diff.info 0.9 || PYTHON_COVERAGE_LINES_ASSERT=1
Y
YUNSHEN XIE 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
        fi
    fi
    if [ "$COVERAGE_LINES_ASSERT" = "1" ] || [ "$PYTHON_COVERAGE_LINES_ASSERT" = "1" ]; then
        echo "exit 9" > /tmp/paddle_coverage.result
        exit 9
    fi
}

function print_usage() {
    echo -e "\n${RED}Usage${NONE}:
    ${BOLD}${SCRIPT_NAME}${NONE} [OPTION]"

    echo -e "\n${RED}Options${NONE}:
    ${BLUE}gen_cov_info${NONE}: generate coverage info
    ${BLUE}test${NONE}: coverage info combine
    "
}

function main () {
    local CMD=$1
    lcov_init
    case $CMD in 
      gen_cov_info)
        gen_cpp_covinfo
        gen_py_covinfo
        ;;
      combine_cov_info)
      covinfo_combine_full
Y
YUNSHEN XIE 已提交
279 280
      gen_diff_html_report || true
      gen_python_diff_html_report || true
Y
YUNSHEN XIE 已提交
281 282 283 284 285 286 287 288 289 290
      cov_rate_judge
        ;;
      *)
        print_usage
        exit 1
        ;;
      esac
}

main $@