Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit d1a8962

Browse files
Add support for running and comparing benchmarks in CI.
Signed-off-by: Harkishen-Singh <[email protected]>
1 parent 4152f3a commit d1a8962

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

.github/workflows/benchmark.yaml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Benchmarks
2+
3+
on:
4+
pull_request:
5+
branches: [ '**' ]
6+
7+
env:
8+
PROMSCALE_LOGGING: false
9+
PROMSCALE_BENCHMARK: true
10+
golang-version: 1.18.4
11+
12+
jobs:
13+
benchmark_current:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout current branch
17+
uses: actions/checkout@v3
18+
19+
- name: Set up Go ${{ env.golang-version }}
20+
uses: actions/[email protected]
21+
with:
22+
go-version: ${{ env.golang-version }}
23+
id: go
24+
25+
- name: Benchmark
26+
run: go test -bench=. -run=^$ -benchmem -cpu=1 ./... | tee current.txt
27+
28+
- name: Upload result
29+
uses: actions/upload-artifact@v3
30+
with:
31+
name: current
32+
path: current.txt
33+
34+
benchmark_master:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- name: Checkout master branch
38+
uses: actions/checkout@v3
39+
with:
40+
ref: 'master'
41+
42+
- name: Set up Go ${{ env.golang-version }}
43+
uses: actions/[email protected]
44+
with:
45+
go-version: ${{ env.golang-version }}
46+
id: go
47+
48+
- name: Benchmark
49+
run: go test -bench=. -run=^$ -benchmem -cpu=1 ./... | tee master.txt
50+
51+
- name: Upload result
52+
uses: actions/upload-artifact@v3
53+
with:
54+
name: master
55+
path: master.txt
56+
57+
compare_and_publish_results:
58+
runs-on: ubuntu-latest
59+
needs: [benchmark_current, benchmark_master]
60+
steps:
61+
- name: Set up Go ${{ env.golang-version }}
62+
uses: actions/[email protected]
63+
with:
64+
go-version: ${{ env.golang-version }}
65+
id: go
66+
67+
- name: Download result for benchmark_current
68+
uses: actions/download-artifact@v3
69+
with:
70+
name: current
71+
72+
- name: Download result for benchmark_master
73+
uses: actions/download-artifact@v3
74+
with:
75+
name: master
76+
77+
- name: Install benchstat
78+
run: go install -a golang.org/x/perf/cmd/benchstat@latest
79+
80+
- name: Compare
81+
id: compare
82+
run: |
83+
OUTPUT=$(benchstat -delta-test=none master.txt current.txt)
84+
echo "${OUTPUT}"
85+
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
86+
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
87+
echo "::set-output name=result::$OUTPUT"
88+
89+
- name: Publish results as comment
90+
uses: marocchino/sticky-pull-request-comment@v2
91+
with:
92+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
93+
header: compare
94+
message: |
95+
@${{ github.event.pull_request.user.login }}
96+
## Benchmark Result for **${{ github.sha }}**
97+
### Master vs PR
98+
```
99+
${{ steps.compare.outputs.result }}
100+
```

pkg/log/log.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"flag"
1010
"fmt"
1111
"os"
12+
"strconv"
1213
"sync"
1314
"time"
1415

@@ -43,10 +44,24 @@ func ParseFlags(fs *flag.FlagSet, cfg *Config) *Config {
4344
return cfg
4445
}
4546

47+
func shouldLog() bool {
48+
value := os.Getenv("PROMSCALE_LOGGING")
49+
enabled, err := strconv.ParseBool(value)
50+
if err != nil {
51+
// If we cannot parse, then assume to continue logging.
52+
return true
53+
}
54+
return enabled
55+
}
56+
4657
// InitDefault is used to start the logger with sane defaults before we can configure it.
4758
// It's useful in instances where we want to log stuff before the configuration
4859
// has been successfully parsed. Calling Init function later on overrides this.
4960
func InitDefault() {
61+
if !shouldLog() {
62+
logger = log.NewNopLogger()
63+
return
64+
}
5065
l := level.NewFilter(
5166
log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)),
5267
level.AllowInfo(),
@@ -57,6 +72,10 @@ func InitDefault() {
5772
// Init starts logging given the configuration. By default, it uses logfmt format
5873
// and minimum logging level.
5974
func Init(cfg Config) error {
75+
if !shouldLog() {
76+
logger = log.NewNopLogger()
77+
return nil
78+
}
6079
var l log.Logger
6180
switch cfg.Format {
6281
case "logfmt", "":

pkg/promql/bench_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package promql
1616
import (
1717
"context"
1818
"fmt"
19+
"os"
1920
"strconv"
2021
"strings"
2122
"testing"
@@ -26,7 +27,15 @@ import (
2627
"github.com/prometheus/prometheus/storage"
2728
)
2829

30+
func ignore() bool {
31+
value := os.Getenv("PROMSCALE_BENCHMARK")
32+
return value != ""
33+
}
34+
2935
func BenchmarkRangeQuery(b *testing.B) {
36+
if ignore() {
37+
return
38+
}
3039
teststorage := NewTestStorage(b)
3140
defer teststorage.Close()
3241
opts := EngineOpts{
@@ -233,6 +242,9 @@ func BenchmarkRangeQuery(b *testing.B) {
233242
}
234243

235244
func BenchmarkParser(b *testing.B) {
245+
if ignore() {
246+
return
247+
}
236248
cases := []string{
237249
"a",
238250
"metric",

0 commit comments

Comments
 (0)