Skip to content

Commit

Permalink
Merge pull request #822 from cloudflare/benchmarks
Browse files Browse the repository at this point in the history
Add benchmark-action/github-action-benchmark
  • Loading branch information
prymitive authored Dec 18, 2023
2 parents 955e080 + ee77767 commit 48dc4d2
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 167 deletions.
1 change: 1 addition & 0 deletions .github/spellcheck/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ promql
PromQL
PRs
prymitive
samber
SNI
symlink
symlinked
Expand Down
33 changes: 24 additions & 9 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: Benchmark Go code

on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
pull-requests: write

jobs:
test-go:
runs-on: ubuntu-latest
Expand All @@ -22,11 +22,26 @@ jobs:
with:
go-version-file: go.mod

- name: Test
run: make benchmark
- name: Fetch test rules
run: make -C cmd/pint/bench fetch

- name: CPU
run: go tool pprof -top cpu.prof
- name: Benchmark PR branch
run: |
make benchmark | tee new.txt
- name: Memory
run: go tool pprof -top mem.prof
- name: Benchmark main branch
run: |
git fetch origin main
git reset --hard FETCH_HEAD
make benchmark | tee old.txt
- name: Diff benchmarks
run: |
git reset --hard ${GITHUB_SHA}
make benchmark-diff
- name: Report
uses: thollander/actions-comment-pull-request@v2
with:
filePath: benchstat.txt
comment_tag: benchstat
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/pint
/.cover
/memprofile.out
/dist/
/.vscode/
/cmd/pint/bench/rules
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,18 @@ coverhtml: test
.PHONY: benchmark
benchmark:
go test \
-v \
-count=10 \
-run=none \
-bench=. \
-benchmem \
-cpuprofile cpu.prof \
-memprofile mem.prof \
./cmd/pint

$(GOBIN)/benchstat: tools/benchstat/go.mod tools/benchstat/go.sum
go install -modfile=tools/benchstat/go.mod golang.org/x/perf/cmd/benchstat
.PHONY: benchmark-diff
benchmark-diff: $(GOBIN)/benchstat
echo "Benchmark diff:" | tee benchstat.txt
echo "" | tee -a benchstat.txt
echo '```' | tee -a benchstat.txt
$(GOBIN)/benchstat old.txt new.txt | tee -a benchstat.txt
echo '```' | tee -a benchstat.txt
9 changes: 9 additions & 0 deletions cmd/pint/bench/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
REVISION := 31a27fb9e0e778bd8fe6097aa58c8ea598fe9cec

.PHONE: fetch
fetch:
curl -sL -o archive.tar.gz https://github.com/samber/awesome-prometheus-alerts/archive/$(REVISION).tar.gz
tar -xf archive.tar.gz
rm -fr rules
mv awesome-prometheus-alerts-$(REVISION)/dist/rules rules
rm -fr awesome-prometheus-alerts-$(REVISION) archive.tar.gz
7 changes: 7 additions & 0 deletions cmd/pint/bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Benchmark data

A collection of Prometheus rules copied from
[samber/awesome-prometheus-alerts](https://github.com/samber/awesome-prometheus-alerts)
and used to benchmark pint.

Run `make fetch` first to download and unpack rules into `rules` folder.
47 changes: 47 additions & 0 deletions cmd/pint/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"
"log/slog"
"testing"

"github.com/prometheus/client_golang/prometheus"

"github.com/cloudflare/pint/internal/config"
"github.com/cloudflare/pint/internal/discovery"
"github.com/cloudflare/pint/internal/git"
"github.com/cloudflare/pint/internal/log"
)

func BenchmarkFindEntries(b *testing.B) {
log.Setup(slog.LevelError, true)

finder := discovery.NewGlobFinder(
[]string{"bench/rules"},
git.NewPathFilter(nil, nil, nil),
)
for n := 0; n < b.N; n++ {
_, _ = finder.Find()
}
}

func BenchmarkCheckRules(b *testing.B) {
log.Setup(slog.LevelError, true)

finder := discovery.NewGlobFinder(
[]string{"bench/rules"},
git.NewPathFilter(nil, nil, nil),
)
entries, err := finder.Find()
if err != nil {
b.Errorf("Find() error: %s", err)
b.FailNow()
}

ctx := context.Background()
cfg, _ := config.Load("", false)
gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry())
for n := 0; n < b.N; n++ {
_, _ = checkRules(ctx, 10, true, gen, cfg, entries)
}
}
154 changes: 0 additions & 154 deletions cmd/pint/lint_test.go

This file was deleted.

7 changes: 7 additions & 0 deletions tools/benchstat/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module _

go 1.21.4

require golang.org/x/perf v0.0.0-20231127181059-b53752263861

require github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 // indirect
4 changes: 4 additions & 0 deletions tools/benchstat/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 h1:xlwdaKcTNVW4PtpQb8aKA4Pjy0CdJHEqvFbAnvR5m2g=
github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794/go.mod h1:7e+I0LQFUI9AXWxOfsQROs9xPhoJtbsyWcjJqDd4KPY=
golang.org/x/perf v0.0.0-20231127181059-b53752263861 h1:oWUgm6RG0ZUKQeX7wxjnX+TFCrXDSheBtpiy5vpUdZg=
golang.org/x/perf v0.0.0-20231127181059-b53752263861/go.mod h1:sKqUfjZtdM78wIFCYFnmu2FhhanvRUbp67Zgmj3TZPM=
7 changes: 7 additions & 0 deletions tools/benchstat/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build tools

package tools

import (
_ "golang.org/x/perf/cmd/benchstat"
)

0 comments on commit 48dc4d2

Please sign in to comment.