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

Add benchmark using cachegrind #28

Merged
merged 23 commits into from
Oct 3, 2023
Merged
61 changes: 61 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: benchmark

on:
pull_request:

jobs:
benchmark:
name: Benchmark
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Set up
run: |
sudo apt-get update
sudo apt-get install -y valgrind
- name: Check out source code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

- name: Check out source code (main)
uses: actions/checkout@v4
with:
ref: main
path: main

- name: Run benchmark (main)
run: make benchmark
working-directory: main

- name: Run benchmark using cachegrind (main)
run: make cachegrind
working-directory: main

- name: Run octocov (main)
uses: k1LoW/octocov-action@v0
with:
config: .octocov.benchmark.main.yml
env:
OCTOCOV_GITHUB_REF: refs/heads/main
OCTOCOV_GITHUB_SHA: none
OCTOCOV_CUSTOM_METRICS_BENCHMARK: main/custom_metrics_benchmark.json
OCTOCOV_CUSTOM_METRICS_CACHEGRIND: main/custom_metrics_cachegrind.json

- name: Run benchmark
run: make benchmark

- name: Run benchmark using cachegrind
run: make cachegrind

- name: Run octocov
uses: k1LoW/octocov-action@v0
with:
config: .octocov.benchmark.yml
env:
OCTOCOV_CUSTOM_METRICS_BENCHMARK: custom_metrics_benchmark.json
OCTOCOV_CUSTOM_METRICS_CACHEGRIND: custom_metrics_cachegrind.json
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Set up
run: |
sudo apt-get update
sudo apt-get install -y valgrind

- name: Check out source code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
Expand All @@ -35,10 +40,5 @@ jobs:
- name: Run tests
run: make ci

- name: Run benchmark
run: make benchmark

- name: Run octocov
uses: k1LoW/octocov-action@v0
env:
OCTOCOV_CUSTOM_METRICS_BENCHMARK: custom_metrics_benchmark.json
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
coverage.out
cachegrind.out
custom_metrics_benchmark.json
custom_metrics_cachegrind.json
testdata/testbin/testbin
10 changes: 10 additions & 0 deletions .octocov.benchmark.main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repository: ${GITHUB_REPOSITORY}/benchmark
coverage:
if: false
codeToTestRatio:
if: false
testExecutionTime:
if: false
report:
datastores:
- local://main
14 changes: 14 additions & 0 deletions .octocov.benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repository: ${GITHUB_REPOSITORY}/benchmark
coverage:
if: false
codeToTestRatio:
if: false
testExecutionTime:
if: false
diff:
datastores:
- local://main
comment:
if: is_pull_request
summary:
if: true
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ benchmark: depsdev
go mod tidy -modfile=testdata/go_test.mod
go test -modfile=testdata/go_test.mod -bench . -benchmem -benchtime 10000x -run Benchmark | octocov-go-test-bench --tee > custom_metrics_benchmark.json

cachegrind: depsdev
cd testdata/testbin && go build -o testbin
setarch `uname -m` -R valgrind --tool=cachegrind --cachegrind-out-file=cachegrind.out --I1=32768,8,64 --D1=32768,8,64 --LL=8388608,16,64 ./testdata/testbin/testbin 10000
cat cachegrind.out | octocov-cachegrind --tee > custom_metrics_cachegrind.json

lint:
go mod tidy
golangci-lint run ./...
Expand All @@ -23,6 +28,7 @@ depsdev:
go install github.com/Songmu/ghch/cmd/ghch@latest
go install github.com/Songmu/gocredits/cmd/gocredits@latest
go install github.com/k1LoW/octocov-go-test-bench/cmd/octocov-go-test-bench@latest
go install github.com/k1LoW/octocov-cachegrind/cmd/octocov-cachegrind@latest
go install github.com/k1LoW/gostyle@latest

prerelease:
Expand Down
13 changes: 13 additions & 0 deletions testdata/testbin/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/k1LoW/rl/testdata/testbin

go 1.21.1

require (
github.com/go-chi/httprate v0.7.4
github.com/k1LoW/rl v0.6.1
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
golang.org/x/sync v0.3.0 // indirect
)
8 changes: 8 additions & 0 deletions testdata/testbin/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/go-chi/httprate v0.7.4 h1:a2GIjv8he9LRf3712zxxnRdckQCm7I8y8yQhkJ84V6M=
github.com/go-chi/httprate v0.7.4/go.mod h1:6GOYBSwnpra4CQfAKXu8sQZg+nZ0M1g9QnyFvxrAB8A=
github.com/k1LoW/rl v0.6.1 h1:97cFVVOMm3iXK9UdHxO78wO6FCmh4IA8edOoly64R8o=
github.com/k1LoW/rl v0.6.1/go.mod h1:FwcYhUX7i153XHIyR4OuOgR2a2KIzcBxrfanE9/O0S4=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
40 changes: 40 additions & 0 deletions testdata/testbin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"log"
"net/http"
"net/http/httptest"
"os"
"strconv"

"github.com/go-chi/httprate"
"github.com/k1LoW/rl"
"github.com/k1LoW/rl/testutil"
)

func main() {
n, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatal(err)
}
r := http.NewServeMux()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Hello, world"))
})
m := rl.New(
testutil.NewLimiter(10, httprate.KeyByIP, 0),
testutil.NewLimiter(10, testutil.KeyByHost, 0),
)
router := m(r)

log.Printf("start %d requests", n)
for i := 0; i < n; i++ {
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
log.Fatal(err)
}
res := httptest.NewRecorder()
router.ServeHTTP(res, req)
}
log.Println("done")
}
Loading