Skip to content

Commit

Permalink
uses grcov for code coverage; also add integration test code; add git…
Browse files Browse the repository at this point in the history
…hub actions

Change-Id: If2ccf470a3139c53745a05bd65d6cb1cdeaf02f9
  • Loading branch information
xiaosong yang committed Mar 27, 2024
1 parent a88d2ca commit 0bf85de
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 34 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env sh

if [ "${NO_RUN}" != "1" ] && [ "${NO_RUN}" != "true" ]; then

set -ex

CARGO=cargo
if [ "${CROSS}" = "1" ]; then
export CARGO_NET_RETRY=5
export CARGO_NET_TIMEOUT=10

cargo install cross
CARGO=cross
fi

# If a test crashes, we want to know which one it was.
export RUST_TEST_THREADS=1
export RUST_BACKTRACE=1

# test monoio mod
cd "${PROJECT_DIR}"/monolake

# only enable legacy driver
"${CARGO}" test --target "${TARGET}" --no-default-features --features "async-cancel,bytes,legacy,macros,utils"
"${CARGO}" test --target "${TARGET}" --no-default-features --features "async-cancel,bytes,legacy,macros,utils" --release

if [ "${TARGET}" = "x86_64-unknown-linux-gnu" ] || [ "${TARGET}" = "i686-unknown-linux-gnu" ]; then
# only enabled uring driver
"${CARGO}" test --target "${TARGET}" --no-default-features --features "async-cancel,bytes,iouring,macros,utils"
"${CARGO}" test --target "${TARGET}" --no-default-features --features "async-cancel,bytes,iouring,macros,utils" --release
fi

if [ "${TARGET}" != "aarch64-unknown-linux-gnu" ] && [ "${TARGET}" != "armv7-unknown-linux-gnueabihf" ] &&
[ "${TARGET}" != "riscv64gc-unknown-linux-gnu" ] && [ "${TARGET}" != "s390x-unknown-linux-gnu" ]; then
# enable uring+legacy driver
"${CARGO}" test --target "${TARGET}"
"${CARGO}" test --target "${TARGET}" --release
fi

if [ "${CHANNEL}" == "nightly" ] && ( [ "${TARGET}" = "x86_64-unknown-linux-gnu" ] || [ "${TARGET}" = "i686-unknown-linux-gnu" ] ); then
"${CARGO}" test --target "${TARGET}" --all-features
"${CARGO}" test --target "${TARGET}" --all-features --release
fi
fi
109 changes: 109 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
name: CI

on:
push:
paths-ignore:
- '**.md'
- '**.png'
pull_request:
paths-ignore:
- '**.md'
- '**.png'

env:
TOOLCHAIN_PROFILE: minimal
CARGO_TERM_COLOR: always

jobs:
test:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: ${{ env.TOOLCHAIN_PROFILE }}
toolchain: ${{ matrix.target == 'i686-pc-windows-gnu' && format('{0}-i686-pc-windows-gnu', matrix.channel) || matrix.channel }}
target: ${{ matrix.target }}
override: true
components: rustfmt, clippy
- name: Cache
uses: Swatinem/rust-cache@v1
- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
- name: Run cargo check with no default features
uses: actions-rs/cargo@v1
with:
command: check
args: --no-default-features
- name: Run cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
- env:
CHANNEL: ${{ matrix.channel }}
CROSS: ${{ !startsWith(matrix.target, 'x86_64') && contains(matrix.target, 'linux') && '1' || '0' }}
TARGET: ${{ matrix.target }}
OS: ${{ matrix.os }}
PROJECT_DIR: ${{ github.workspace }}
NO_RUN: ${{ matrix.no_run }}
run: sh .github/workflows/ci.sh

strategy:
fail-fast: false
matrix:
target: [
x86_64-unknown-linux-gnu,
i686-unknown-linux-gnu,
aarch64-unknown-linux-gnu,
armv7-unknown-linux-gnueabihf,
riscv64gc-unknown-linux-gnu,
s390x-unknown-linux-gnu,
# mips64-unknown-linux-muslabi64,
# loongarch64-unknown-linux-gnu,

x86_64-apple-darwin,
aarch64-apple-darwin,

x86_64-pc-windows-gnu,
x86_64-pc-windows-msvc,
i686-pc-windows-gnu,
i686-pc-windows-msvc,
]
channel: [stable, nightly]
include:
- target: x86_64-unknown-linux-gnu
os: ubuntu-latest
- target: i686-unknown-linux-gnu
os: ubuntu-latest
- target: aarch64-unknown-linux-gnu
os: ubuntu-latest
- target: armv7-unknown-linux-gnueabihf
os: ubuntu-latest
- target: riscv64gc-unknown-linux-gnu
os: ubuntu-latest
- target: s390x-unknown-linux-gnu
os: ubuntu-latest
# - target: mips64-unknown-linux-muslabi64
# os: ubuntu-latest
# - target: loongarch64-unknown-linux-gnu
# os: ubuntu-latest

- target: x86_64-apple-darwin
os: macos-latest
- target: aarch64-apple-darwin
os: macos-14

- target: x86_64-pc-windows-msvc
os: windows-latest
- target: x86_64-pc-windows-gnu
os: windows-latest
- target: i686-pc-windows-msvc
os: windows-latest
- target: i686-pc-windows-gnu
os: windows-latest
41 changes: 41 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Code Coverage

on:
push:
branches:
- master
paths-ignore:
- '**.md'
- '**.png'

env:
RUST_TOOLCHAIN: nightly
TOOLCHAIN_PROFILE: minimal

jobs:
coverage:
name: Run cargo coverage
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: ${{ env.TOOLCHAIN_PROFILE }}
toolchain: ${{ env.RUST_TOOLCHAIN }}
override: true
components: llvm-tools-preview
- name: Install grcov
uses: actions-rs/cargo@v1
with:
command: install
args: grcov
- name: Cache
uses: Swatinem/rust-cache@v1
- name: Run cargo test
run: sudo bash -c "ulimit -Sl 512 && ulimit -Hl 512 && sudo -u runner RUSTUP_TOOLCHAIN=nightly RUSTFLAGS="-Cinstrument-coverage" LLVM_PROFILE_FILE="coverage-%p-%m.profraw" /home/runner/.cargo/bin/cargo test --all-features"
- name: Run grcov
run: grcov . --binary-path ./target/debug/ -s . -t lcov --branch --ignore-not-existing --ignore "*cargo*" -o lcov.info
- name: Upload coverage
run: bash <(curl -s https://codecov.io/bash) -f lcov.info
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.idea
.swp
*.pem
*.profraw

/examples/certs
/docs/cloudwego.github.io
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ anyhow = "1"
serde = "1"
tracing = "0.1"
bytes = "1"
cargo-llvm-cov = "0.6.8"
# cargo-llvm-cov = "0.6.8"
grcov = "0.8.19"

[profile.release-lto]
inherits = "release"
Expand Down
51 changes: 41 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,63 @@ RUST_LOG=debug cargo run --package monolake -- -c examples/config.toml
curl -vvv --cacert examples/certs/rootCA.crt --resolve "gateway.monolake.rs:8082:127.0.0.1" https://gateway.monolake.rs:8082/
```

## code coverage test
## manual code coverage test

### install llvm-cov
cargo install cargo-llvm-cov
When PR is merged into main branch, unit test code coverage will be automatically run. But to get more code coverage rate, we need to run code coverage test manually.

### It is already done: adding llvm-cov to Cargo.toml dependencies foelds:
### install coverage tool
cargo install grcov # or "cargo install cargo-llvm-cov" for llvm-cov

### adding coverage tool to Cargo.toml dependencies foelds:
cargo add grcov --package monolake
#### or for llvm-cov: add 'cargo-llvm-cov = "0.6.8"' to [dependencies]
[dependencies]
cargo-llvm-cov = "0.6.8"

### grcov only: setup code coverage test for all unit tests
export RUSTFLAGS="-Cinstrument-coverage"
export LLVM_PROFILE_FILE="<your_name>-%p-%m.profraw"
cargo build

### run code coverage test for all unit tests (in the code)
cargo test
#### or for llvm-cov run "cargo llvm-cov"
cargo llvm-cov

### run code coverage test for monolake functions
cargo llvm-cov --html run -- -c examples/config-2.toml
### run code coverage test for integration test
RUST_LOG=info target/debug/code-coverage-monolake -c examples/config-2.toml &
#### or for llvm-cov run "cargo llvm-cov --html run -- --bin code-coverage-monolake -c examples/config-2.toml"
cargo llvm-cov --html run -- --bin code-coverage-monolake -c examples/config-2.toml

### integration test
curl http://localhost:8402 # ip/port depends on config
####
curl -k https://localhost:6442 # ip/port depends on config
####
./wrk 'http://localhost:8402' -d 10s -c 10 -t 1 # ip/port depends on config
####
./wrk 'https://localhost:6442' -d 10s -c 10 -t 1 # ip/port depends on config

ps -A | grep monolake # find pid-of-monolake
kill -15 <pid-of-monolake> # send SIGTERM to quit monolake
### manually kill the monolake process
kill -15 $(ps aux | grep 'code-coverage-monolake' | awk '{print $2}')

### grcov only: merge code coverage report
grcov . -s . --binary-path ./target/debug/ -t html --branch --ignore-not-existing -o ./target/debug/coverage/

### browse code coverage report
open target/debug/coverage/index.html
#### or for llvm-cov "open target/llvm-cov/index.html"
open target/llvm-cov/index.html

open target/llvm-cov/html/index.html # show code coverage in browser page
### if it is not the first run, use target/debug/coverage/html/index.html
open target/debug/coverage/html/index.html
#### or for llvm-cov "open target/llvm-cov/html/index.html"
open target/llvm-cov/html/index.html

cargo llvm-cov clean --workspace # clean the result for the next run
### clean coverage report result for the next run
rm *.profraw */*.profraw
#### or for llvm-cov "cargo llvm-cov clean --workspace"
cargo llvm-cov clean --workspace

## Limitations

Expand Down
4 changes: 2 additions & 2 deletions examples/config-2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ entries = 4096
upstreams = [{ endpoint = { type = "uri", value = "http://127.0.0.1:10085" } }]

[servers.server_tls2]
tls = { chain = "examples/certs/cert.pem", key = "examples/certs/key.pem" }
tls = { chain = "examples/certs/cert.pem", key = "examples/certs/key.pem", stack = "rustls" }
name = "monolake.cloudwego.io"
listener = { type = "socket", value = "0.0.0.0:6442" }
[[servers.server_tls2.routes]]
Expand All @@ -56,7 +56,7 @@ entries = 4096
upstreams = [{ endpoint = { type = "uri", value = "http://127.0.0.1:10084" } }]

[servers.server_tls5]
tls = { chain = "examples/certs/cert.pem", key = "examples/certs/key.pem" }
tls = { chain = "examples/certs/cert.pem", key = "examples/certs/key.pem", stack = "native_tls" }
name = "monolake.cloudwego.io"
listener = { type = "socket", value = "0.0.0.0:6445" }
[[servers.server_tls5.routes]]
Expand Down
8 changes: 8 additions & 0 deletions monolake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
clap = { version = "4", features = ['derive'] }
serde_json = "1"
toml = "0"

[[bin]]
name = "code-coverage-monolake"
path = "src/code-coverage-main.rs"

[[bin]]
name = "monolake"
path = "src/main.rs"
Loading

0 comments on commit 0bf85de

Please sign in to comment.