Skip to content

Commit 6077faf

Browse files
[ci] add coverage actions (#821)
1 parent 5df201e commit 6077faf

File tree

2 files changed

+212
-66
lines changed

2 files changed

+212
-66
lines changed

.github/actions/coverage/action.yml

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#
2+
# Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
3+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
#
5+
# This code is free software; you can redistribute it and/or modify it
6+
# under the terms of the GNU General Public License version 2 only, as
7+
# published by the Free Software Foundation. Alibaba designates this
8+
# particular file as subject to the "Classpath" exception as provided
9+
# by Oracle in the LICENSE file that accompanied this code.
10+
#
11+
# This code is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
# version 2 for more details (a copy is included in the LICENSE file that
15+
# accompanied this code).
16+
#
17+
# You should have received a copy of the GNU General Public License version
18+
# 2 along with this work; if not, write to the Free Software Foundation,
19+
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
#
21+
22+
name: 'Coverage test'
23+
24+
inputs:
25+
reset-commit-id:
26+
required: false
27+
type: string
28+
outputs:
29+
data:
30+
value: ${{ steps.get-cov-report.outputs.data }}
31+
32+
runs:
33+
using: composite
34+
steps:
35+
- name: 'Checkout source code'
36+
uses: actions/checkout@v4
37+
with:
38+
fetch-depth: 100
39+
40+
- name: 'Reset to specific commit'
41+
run: |
42+
echo "${{ inputs.reset-commit-id }}"
43+
git reset --hard ${{ inputs.reset-commit-id }}
44+
shell: bash
45+
if: ${{ inputs.reset-commit-id }} != ""
46+
47+
- name: 'Install dependencies'
48+
run: |
49+
apt --help &>/dev/null
50+
if [ $? -eq 0 ];then
51+
sudo apt-get install -y openssl libssl-dev llvm jq
52+
else
53+
yum --help &>/dev/null
54+
if [ $? -eq 0 ];then
55+
sudo yum install -y openssl openssl-devel llvm jq
56+
else
57+
exit 1
58+
fi
59+
fi
60+
shell: bash
61+
62+
- name: 'Install newer clang'
63+
run: |
64+
apt --help &>/dev/null
65+
if [ $? -eq 0 ];then
66+
sudo rm /etc/apt/sources.list.d/microsoft-prod.list
67+
sudo apt-get update -y
68+
else
69+
yum --help &>/dev/null
70+
[ $? -eq 0 ] && sudo yum update -y
71+
fi
72+
wget https://apt.llvm.org/llvm.sh -O llvm.sh
73+
chmod +x ./llvm.sh
74+
sudo ./llvm.sh 17
75+
shell: bash
76+
77+
- name: 'Build and test'
78+
id: get-cov-report
79+
run: |
80+
cp -r src/coro_rpc/tests/openssl_files .
81+
rm -rf build
82+
mkdir -p build
83+
cd build
84+
CC=clang-17 CXX=clang++-17 cmake .. -DCOVERAGE_TEST=ON -DYLT_ENABLE_SSL=ON -DBUILD_EXAMPLES=OFF -DBUILD_BENCHMARK=OFF
85+
make -j
86+
export LLVM_PROFILE_FILE="test_ylt-%m.profraw"
87+
cd output/tests
88+
find . -maxdepth 1 -type f -executable | xargs -I {} sh -c '{}'
89+
llvm-profdata merge -sparse test_ylt-*.profraw -o test_ylt.profdata
90+
if [ -n "${{ inputs.reset-commit-id }}" ];then
91+
report=base-ylt-cov-report
92+
else
93+
report=ylt-cov-report
94+
fi
95+
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
96+
echo "path=build/output/tests/$report" >> $GITHUB_OUTPUT
97+
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}')
98+
echo "coverage data: $cov_data"
99+
echo "report=$report" >> $GITHUB_OUTPUT
100+
echo "data=$cov_data" >> $GITHUB_OUTPUT
101+
shell: bash
102+
103+
- name: 'Upload coverage results'
104+
uses: actions/[email protected]
105+
with:
106+
name: ${{ steps.get-cov-report.outputs.report }}
107+
path: ${{ steps.get-cov-report.outputs.path }}
108+
109+
- name: 'Create code coverage report and comment'
110+
run: |
111+
if [ -n "${{ inputs.reset-commit-id }}" ];then
112+
title="Base Code Coverage Report"
113+
else
114+
title="Current PR Code Coverage Report"
115+
fi
116+
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 }}\`")
117+
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}\""
118+
shell: bash
119+
120+
- name: 'Checkout source code'
121+
uses: actions/checkout@v4
122+
if: ${{ inputs.reset-commit-id }} != ""

.github/workflows/linux_llvm_cov.yml

+90-66
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,106 @@
1+
#
2+
# Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
3+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
#
5+
# This code is free software; you can redistribute it and/or modify it
6+
# under the terms of the GNU General Public License version 2 only, as
7+
# published by the Free Software Foundation. Alibaba designates this
8+
# particular file as subject to the "Classpath" exception as provided
9+
# by Oracle in the LICENSE file that accompanied this code.
10+
#
11+
# This code is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
# version 2 for more details (a copy is included in the LICENSE file that
15+
# accompanied this code).
16+
#
17+
# You should have received a copy of the GNU General Public License version
18+
# 2 along with this work; if not, write to the Free Software Foundation,
19+
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
#
21+
122
name: Ubuntu 22.04 (llvm cov)
223

324
on:
4-
pull_request_target:
25+
pull_request:
526
branches:
627
- main
728
- fix_coverage_show
829

30+
concurrency:
31+
group: ${{ github.workflow }}-${{ github.ref }}
32+
cancel-in-progress: true
33+
934
jobs:
10-
build:
35+
prerequisites:
1136
runs-on: ubuntu-22.04
12-
37+
outputs:
38+
id: ${{ steps.get-base-commit.outputs.id }}
1339
steps:
14-
- name: Checkout
15-
uses: actions/checkout@v4
16-
17-
- name: Install Dependencies
40+
- name: 'Get Base Commit id'
41+
id: get-base-commit
1842
run: |
19-
sudo apt-get install openssl
20-
sudo apt-get install libssl-dev
21-
sudo apt-get install llvm
43+
sudo apt install -y jq
44+
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)
45+
echo "::set-output name=id::$base_commit_id"
46+
shell: bash
47+
if: ${{ github.event_name == 'pull_request' }}
2248

23-
- name: Install newer Clang
24-
run: |
25-
sudo rm /etc/apt/sources.list.d/microsoft-prod.list
26-
sudo apt-get update
27-
wget https://apt.llvm.org/llvm.sh
28-
chmod +x ./llvm.sh
29-
sudo ./llvm.sh 17
30-
31-
- name: Run Coverage
32-
run: |
33-
ls
34-
cp -r src/coro_rpc/tests/openssl_files .
35-
ls
36-
mkdir build && cd build
37-
CC=clang-17 CXX=clang++-17 cmake .. -DCOVERAGE_TEST=ON -DYLT_ENABLE_SSL=ON -DBUILD_EXAMPLES=OFF -DBUILD_BENCHMARK=OFF
38-
make -j
39-
export LLVM_PROFILE_FILE="test_ylt-%m.profraw"
40-
cd output
41-
cd tests
42-
./coro_io_test
43-
./coro_rpc_test
44-
./easylog_test
45-
./struct_pack_test
46-
./struct_pack_test_with_optimize
47-
./metric_test
48-
./struct_pb_test
49-
./reflection_test
50-
./coro_http_test
51-
llvm-profdata merge -sparse test_ylt-*.profraw -o test_ylt.profdata
52-
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
53-
echo "Done!"
54-
55-
56-
- name: List files in the repository
57-
run: |
58-
echo "workspace"
59-
ls ${{ github.workspace }}
60-
echo "workspace/build"
61-
ls ${{ github.workspace }}/build
49+
base-cov-test:
50+
needs: prerequisites
51+
runs-on: ubuntu-22.04
52+
permissions:
53+
contents: read
54+
issues: write
55+
pull-requests: write
56+
outputs:
57+
data: ${{ steps.base-cov.outputs.data }}
58+
steps:
59+
- name: 'Checkout source code'
60+
uses: actions/checkout@v4
61+
- name: 'Base coverage test'
62+
id: base-cov
63+
uses: ./.github/actions/coverage
64+
with:
65+
reset-commit-id: ${{ needs.prerequisites.outputs.id }}
66+
if: ${{ github.event_name == 'pull_request' }}
6267

63-
- name: Upload Coverage Results
64-
uses: actions/[email protected]
68+
cov-test:
69+
needs: prerequisites
70+
runs-on: ubuntu-22.04
71+
permissions:
72+
contents: read
73+
issues: write
74+
pull-requests: write
75+
outputs:
76+
data: ${{ steps.cov.outputs.data }}
77+
steps:
78+
- name: 'Checkout source code'
79+
uses: actions/checkout@v4
80+
- name: 'Coverage test'
81+
id: cov
82+
uses: ./.github/actions/coverage
6583
with:
66-
name: llvm-cov
67-
path: ${{github.workspace}}/build/.coverage_llvm_cov
84+
reset-commit-id: ""
6885

69-
- name: Create Code Coverage Report
70-
working-directory: ${{github.workspace}}/build/output/tests
86+
compare-cov-data:
87+
needs:
88+
- base-cov-test
89+
- cov-test
90+
runs-on: ubuntu-22.04
91+
steps:
92+
- name: 'Compare data'
7193
run: |
72-
echo "Code Coverage Report" > tmp.log
73-
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
74-
echo "\`\`\`" >> tmp.log
75-
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
76-
echo "\`\`\`" >> tmp.log
77-
78-
- name: Create Comment
79-
uses: peter-evans/create-or-update-comment@v4
80-
with:
81-
issue-number: ${{ github.event.pull_request.number }}
82-
body-file: '${{github.workspace}}/build/output/tests/tmp.log'
94+
sudo apt install -y bc
95+
result=$(echo "${{ needs.cov-test.outputs.data }} > 70" | bc)
96+
if [ "$result" -ne 1 ];then
97+
echo "coverage cannot be lower than 70%!"
98+
exit 1
99+
fi
100+
result=$(echo "${{ needs.cov-test.outputs.data }} > $(echo "${{ needs.base-cov-test.outputs.data}} * 0.97" | bc)" | bc)
101+
if [ "$result" -ne 1 ];then
102+
echo "coverage has decreased over 3%!"
103+
exit 1
104+
fi
105+
shell: bash
106+
if: ${{ github.event_name == 'pull_request' }}

0 commit comments

Comments
 (0)