Skip to content

Commit aea73fb

Browse files
authored
dev: add targets to benchmark a linter (#4761)
1 parent 95b8f48 commit aea73fb

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

Makefile

+36
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ fast_check_generated:
6060
git checkout -- go.mod go.sum # can differ between go1.16 and go1.17
6161
git diff --exit-code # check no changes
6262

63+
# Benchmark
64+
65+
# Benchmark with a local version
66+
# LINTER=gosec VERSION=v1.59.0 make bench_local
67+
bench_local: hyperfine
68+
@:$(call check_defined, LINTER VERSION, 'missing parameter(s)')
69+
@./scripts/bench/bench_local.sh $(LINTER) $(VERSION)
70+
.PHONY: bench_local
71+
72+
# Benchmark between 2 existing versions
73+
# make bench_version LINTER=gosec VERSION_OLD=v1.58.2 VERSION_NEW=v1.59.0
74+
bench_version: hyperfine
75+
@:$(call check_defined, LINTER VERSION_OLD VERSION_NEW, 'missing parameter(s)')
76+
@./scripts/bench/bench_version.sh $(LINTER) $(VERSION_OLD) $(VERSION_NEW)
77+
.PHONY: bench_version
78+
79+
hyperfine:
80+
@which hyperfine > /dev/null || (echo "Please install hyperfine https://github.com/sharkdp/hyperfine#installation" && exit 1)
81+
.PHONY: hyperfine
82+
6383
# Non-PHONY targets (real files)
6484

6585
$(BINARY): FORCE
@@ -102,3 +122,19 @@ website_dump_info:
102122
update_contributors_list:
103123
cd .github/contributors && npm run all
104124

125+
# Functions
126+
127+
# Check that given variables are set and all have non-empty values,
128+
# die with an error otherwise.
129+
#
130+
# Params:
131+
# 1. Variable name(s) to test.
132+
# 2. (optional) Error message to print.
133+
#
134+
# https://stackoverflow.com/a/10858332/8228109
135+
check_defined = \
136+
$(strip $(foreach 1,$1, \
137+
$(call __check_defined,$1,$(strip $(value 2)))))
138+
__check_defined = \
139+
$(if $(value $1),, \
140+
$(error Undefined $1$(if $2, ($2))))

scripts/bench/bench_local.sh

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash -e
2+
3+
# Benchmark with a local version
4+
# Usage: ./scripts/bench/bench_local.sh gosec v1.59.0
5+
6+
# ex: gosec
7+
LINTER=$1
8+
9+
# ex: v1.59.0
10+
VERSION=$2
11+
12+
13+
if [ -z "$LINTER" ] || [ -z "$VERSION" ]; then
14+
cat <<-EOF
15+
Missing required arguments!
16+
17+
Usage: $0 <linter> <old version> <new version>
18+
Example: $0 gosec v1.58.1 v1.58.2
19+
EOF
20+
21+
exit 1
22+
fi
23+
24+
EOF
25+
26+
## Clean
27+
28+
function cleanBinaries() {
29+
echo "Clean binaries"
30+
rm "./golangci-lint-${VERSION}"
31+
rm ./golangci-lint
32+
}
33+
34+
trap cleanBinaries EXIT
35+
36+
## Download version
37+
38+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "./temp-${VERSION}" "${VERSION}"
39+
40+
mv "temp-${VERSION}/golangci-lint" "./golangci-lint-${VERSION}"
41+
rm -rf "temp-${VERSION}"
42+
43+
## Build local version
44+
45+
make build
46+
47+
## Run
48+
49+
hyperfine \
50+
--prepare './golangci-lint cache clean' "./golangci-lint run --issues-exit-code 0 --print-issued-lines=false --enable-only ${LINTER}" \
51+
--prepare "./golangci-lint-${VERSION} cache clean" "./golangci-lint-${VERSION} run --issues-exit-code 0 --print-issued-lines=false --enable-only ${LINTER}"
52+

scripts/bench/bench_version.sh

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash -e
2+
3+
# Benchmark between 2 existing versions
4+
# Usage: ./scripts/bench/bench_version.sh gosec v1.58.1 v1.58.2
5+
6+
# ex: gosec
7+
LINTER="$1"
8+
9+
# ex: v1.58.1
10+
VERSION_OLD="$2"
11+
# ex: v1.58.2
12+
VERSION_NEW="$3"
13+
14+
if [ -z "$LINTER" ] || [ -z "$VERSION_OLD" ] || [ -z "$VERSION_NEW" ]; then
15+
cat <<-EOF
16+
Missing required arguments!
17+
18+
Usage: $0 <linter> <old version> <new version>
19+
Example: $0 gosec v1.58.1 v1.58.2
20+
EOF
21+
22+
exit 1
23+
fi
24+
25+
EOF
26+
27+
## Clean
28+
29+
function cleanBinaries() {
30+
echo "Clean binaries"
31+
rm "./golangci-lint-${VERSION_OLD}"
32+
rm "./golangci-lint-${VERSION_NEW}"
33+
}
34+
35+
trap cleanBinaries EXIT
36+
37+
## Install
38+
39+
function install() {
40+
local VERSION=$1
41+
42+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "./temp-${VERSION}" "${VERSION}"
43+
44+
mv "temp-${VERSION}/golangci-lint" "./golangci-lint-${VERSION}"
45+
rm -rf "temp-${VERSION}"
46+
}
47+
48+
## VERSION_OLD
49+
50+
install "${VERSION_OLD}"
51+
52+
## VERSION_NEW
53+
54+
install "${VERSION_NEW}"
55+
56+
## Run
57+
58+
hyperfine \
59+
--prepare "./golangci-lint-${VERSION_OLD} cache clean" "./golangci-lint-${VERSION_OLD} run --issues-exit-code 0 --print-issued-lines=false --enable-only ${LINTER}" \
60+
--prepare "./golangci-lint-${VERSION_NEW} cache clean" "./golangci-lint-${VERSION_NEW} run --issues-exit-code 0 --print-issued-lines=false --enable-only ${LINTER}"

scripts/bench/readme.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Benchmarks
2+
3+
The script use [Hyperfine](https://github.com/sharkdp/hyperfine) to benchmark the command line of golangci-lint.
4+
5+
## Benchmark one linter: with a local version
6+
7+
```bash
8+
make bench_local LINTER=gosec VERSION=v1.59.0
9+
```
10+
11+
## Benchmark one linter: between 2 existing versions
12+
13+
```bash
14+
make bench_version LINTER=gosec VERSION_OLD=v1.58.1 VERSION_NEW=v1.59.0
15+
```

0 commit comments

Comments
 (0)