Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ci] add coverage actions #821

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions .github/actions/coverage/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#
# Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation. Alibaba designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#

name: 'Coverage test'

inputs:
reset-commit-id:
required: false
type: string
outputs:
data:
value: ${{ steps.get-cov-report.outputs.data }}

runs:
using: composite
steps:
- name: 'Checkout source code'
uses: actions/checkout@v4
with:
fetch-depth: 100

- name: 'Reset to specific commit'
run: |
echo "${{ inputs.reset-commit-id }}"
git reset --hard ${{ inputs.reset-commit-id }}
shell: bash
if: ${{ inputs.reset-commit-id }} != ""

- name: 'Install dependencies'
run: |
apt --help &>/dev/null
if [ $? -eq 0 ];then
sudo apt-get install -y openssl libssl-dev llvm jq
else
yum --help &>/dev/null
if [ $? -eq 0 ];then
sudo yum install -y openssl openssl-devel llvm jq
else
exit 1
fi
fi
shell: bash

- name: 'Install newer clang'
run: |
apt --help &>/dev/null
if [ $? -eq 0 ];then
sudo rm /etc/apt/sources.list.d/microsoft-prod.list
sudo apt-get update -y
else
yum --help &>/dev/null
[ $? -eq 0 ] && sudo yum update -y
fi
wget https://apt.llvm.org/llvm.sh -O llvm.sh
chmod +x ./llvm.sh
sudo ./llvm.sh 17
shell: bash

- name: 'Build and test'
id: get-cov-report
run: |
cp -r src/coro_rpc/tests/openssl_files .
rm -rf build
mkdir -p build
cd build
CC=clang-17 CXX=clang++-17 cmake .. -DCOVERAGE_TEST=ON -DYLT_ENABLE_SSL=ON -DBUILD_EXAMPLES=OFF -DBUILD_BENCHMARK=OFF
make -j
export LLVM_PROFILE_FILE="test_ylt-%m.profraw"
cd output/tests
find . -maxdepth 1 -type f -executable | xargs -I {} sh -c '{}'
llvm-profdata merge -sparse test_ylt-*.profraw -o test_ylt.profdata
if [ -n "${{ inputs.reset-commit-id }}" ];then
report=base-ylt-cov-report
else
report=ylt-cov-report
fi
llvm-cov show $(find . -maxdepth 1 -type f -executable | awk '{print "-object " $0}' | xargs) -instr-profile=test_ylt.profdata -format=html -output-dir=$report -ignore-filename-regex='thirdparty|src|template_switch' -show-instantiations=false
echo "path=build/output/tests/$report" >> $GITHUB_OUTPUT
cov_data=$(grep -w '<pre>Totals</pre>' $report/index.html | awk -F 'Totals' '{print $NF}' | cut -d ')' -f 2 | awk -F '>' '{print $NF}' | awk -F '%' '{print $1}')
echo "coverage data: $cov_data"
echo "report=$report" >> $GITHUB_OUTPUT
echo "data=$cov_data" >> $GITHUB_OUTPUT
shell: bash

- name: 'Upload coverage results'
uses: actions/[email protected]
with:
name: ${{ steps.get-cov-report.outputs.report }}
path: ${{ steps.get-cov-report.outputs.path }}

- name: 'Create code coverage report and comment'
run: |
if [ -n "${{ inputs.reset-commit-id }}" ];then
title="Base Code Coverage Report"
else
title="Current PR Code Coverage Report"
fi
content=$(echo "for detail, [goto summary](https://github.com/${{ github.repository_owner }}/${{ github.event.repository.name }}/actions/runs/${{github.run_id}}) download Artifacts \`${{ steps.get-cov-report.outputs.report }}\`")
curl -L -X POST "https://api.github.com/repos/${{github.repository}}/issues/${{github.event.pull_request.number}}/comments" -H "Authorization: Bearer ${{github.token}}" -H 'Content-Type: application/json' -d "{\"body\": \"${title}\\n$content}\""
shell: bash

- name: 'Checkout source code'
uses: actions/checkout@v4
if: ${{ inputs.reset-commit-id }} != ""
156 changes: 90 additions & 66 deletions .github/workflows/linux_llvm_cov.yml
Original file line number Diff line number Diff line change
@@ -1,82 +1,106 @@
#
# Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation. Alibaba designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#

name: Ubuntu 22.04 (llvm cov)

on:
pull_request_target:
pull_request:
branches:
- main
- fix_coverage_show

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
prerequisites:
runs-on: ubuntu-22.04

outputs:
id: ${{ steps.get-base-commit.outputs.id }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Dependencies
- name: 'Get Base Commit id'
id: get-base-commit
run: |
sudo apt-get install openssl
sudo apt-get install libssl-dev
sudo apt-get install llvm
sudo apt install -y jq
base_commit_id=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }} | jq -r .base.sha)
echo "::set-output name=id::$base_commit_id"
shell: bash
if: ${{ github.event_name == 'pull_request' }}

- name: Install newer Clang
run: |
sudo rm /etc/apt/sources.list.d/microsoft-prod.list
sudo apt-get update
wget https://apt.llvm.org/llvm.sh
chmod +x ./llvm.sh
sudo ./llvm.sh 17
- name: Run Coverage
run: |
ls
cp -r src/coro_rpc/tests/openssl_files .
ls
mkdir build && cd build
CC=clang-17 CXX=clang++-17 cmake .. -DCOVERAGE_TEST=ON -DYLT_ENABLE_SSL=ON -DBUILD_EXAMPLES=OFF -DBUILD_BENCHMARK=OFF
make -j
export LLVM_PROFILE_FILE="test_ylt-%m.profraw"
cd output
cd tests
./coro_io_test
./coro_rpc_test
./easylog_test
./struct_pack_test
./struct_pack_test_with_optimize
./metric_test
./struct_pb_test
./reflection_test
./coro_http_test
llvm-profdata merge -sparse test_ylt-*.profraw -o test_ylt.profdata
llvm-cov show -object coro_io_test -object coro_rpc_test -object easylog_test -object struct_pack_test -object struct_pack_test_with_optimize -object metric_test -object struct_pb_test -object reflection_test -object coro_http_test -instr-profile=test_ylt.profdata -format=html -output-dir=../../.coverage_llvm_cov -ignore-filename-regex="thirdparty|src|template_switch" -show-instantiations=false
echo "Done!"
- name: List files in the repository
run: |
echo "workspace"
ls ${{ github.workspace }}
echo "workspace/build"
ls ${{ github.workspace }}/build
base-cov-test:
needs: prerequisites
runs-on: ubuntu-22.04
permissions:
contents: read
issues: write
pull-requests: write
outputs:
data: ${{ steps.base-cov.outputs.data }}
steps:
- name: 'Checkout source code'
uses: actions/checkout@v4
- name: 'Base coverage test'
id: base-cov
uses: ./.github/actions/coverage
with:
reset-commit-id: ${{ needs.prerequisites.outputs.id }}
if: ${{ github.event_name == 'pull_request' }}

- name: Upload Coverage Results
uses: actions/[email protected]
cov-test:
needs: prerequisites
runs-on: ubuntu-22.04
permissions:
contents: read
issues: write
pull-requests: write
outputs:
data: ${{ steps.cov.outputs.data }}
steps:
- name: 'Checkout source code'
uses: actions/checkout@v4
- name: 'Coverage test'
id: cov
uses: ./.github/actions/coverage
with:
name: llvm-cov
path: ${{github.workspace}}/build/.coverage_llvm_cov
reset-commit-id: ""

- name: Create Code Coverage Report
working-directory: ${{github.workspace}}/build/output/tests
compare-cov-data:
needs:
- base-cov-test
- cov-test
runs-on: ubuntu-22.04
steps:
- name: 'Compare data'
run: |
echo "Code Coverage Report" > tmp.log
echo "for detail, [goto summary](https://github.com/${{ github.repository_owner }}/${{ github.event.repository.name }}/actions/runs/${{github.run_id}}) download Artifacts `llvm-cov`" >> tmp.log
echo "\`\`\`" >> tmp.log
llvm-cov report -object coro_io_test -object coro_rpc_test -object easylog_test -object struct_pack_test -object struct_pack_test_with_optimize -object metric_test -object struct_pb_test -object reflection_test -object coro_http_test -instr-profile=test_ylt.profdata -ignore-filename-regex="thirdparty|src|template_switch" -show-region-summary=false >> tmp.log
echo "\`\`\`" >> tmp.log
- name: Create Comment
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body-file: '${{github.workspace}}/build/output/tests/tmp.log'
sudo apt install -y bc
result=$(echo "${{ needs.cov-test.outputs.data }} > 70" | bc)
if [ "$result" -ne 1 ];then
echo "coverage cannot be lower than 70%!"
exit 1
fi
result=$(echo "${{ needs.cov-test.outputs.data }} > $(echo "${{ needs.base-cov-test.outputs.data}} * 0.97" | bc)" | bc)
if [ "$result" -ne 1 ];then
echo "coverage has decreased over 3%!"
exit 1
fi
shell: bash
if: ${{ github.event_name == 'pull_request' }}
Loading