Skip to content

Commit

Permalink
dns: start adding benchmarks for performance regressions (#1325)
Browse files Browse the repository at this point in the history
* dont use log colours in CI

* dns: start adding benchmarks

* fix output?

* fix benchmark output

* cleanup

* more benchmarks

* separate benchmarks
  • Loading branch information
BeryJu authored Nov 26, 2024
1 parent 89a6362 commit 0f9f425
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 3 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Benchmark

on:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: write
deployments: write

jobs:
test:
name: Benchmark
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: true
- uses: actions/setup-node@v4
with:
node-version-file: web/package.json
cache: "npm"
cache-dependency-path: web/package-lock.json
- run: make web-install web-build
- run: make test-env-start
- run: make install-deps
- run: make bench
- uses: benchmark-action/github-action-benchmark@v1
with:
name: Gravity Benchmark
tool: 'go'
output-file-path: test-output
github-token: ${{ secrets.GITHUB_TOKEN }}
auto-push: true
# Show alert with commit comment on detecting possible performance regression
alert-threshold: '200%'
comment-on-alert: true
fail-on-alert: true
alert-comment-cc-users: '@BeryJu'
summary-always: true
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ test: internal/resources/macoui internal/resources/blocky internal/resources/tft
export ETCD_ENDPOINT="localhost:2385"
export DEBUG="true"
export LISTEN_ONLY="true"
go run -v ${PWD} cli etcdctl del --prefix /
export CI="true"
go test \
-p 1 \
-coverprofile=coverage.txt \
Expand All @@ -179,3 +179,16 @@ test: internal/resources/macoui internal/resources/blocky internal/resources/tft
$(shell go list ./... | grep -v ./api) \
2>&1 | tee test-output
go tool cover -html coverage.txt -o coverage.html

bench: internal/resources/macoui internal/resources/blocky internal/resources/tftp
export BOOTSTRAP_ROLES="dns;dhcp;api;discovery;backup;debug;tsdb;tftp"
export ETCD_ENDPOINT="localhost:2385"
export LISTEN_ONLY="true"
export LOG_LEVEL="FATAL"
export CI="true"
go test \
-run=^$$ \
-bench=^Benchmark \
-benchmem \
$(shell go list ./... | grep -v ./api) \
| tee test-output
3 changes: 3 additions & 0 deletions pkg/extconfig/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (e *ExtConfig) BuildLoggerWithLevel(l zapcore.Level) *zap.Logger {
config.EncoderConfig = zap.NewDevelopmentEncoderConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
if CI() {
config.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
}
config.EncoderConfig.EncodeDuration = zapcore.MillisDurationEncoder
log, err := config.Build()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/extconfig/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ var (
BuildHash = ""
)

func CI() bool {
return strings.EqualFold(os.Getenv("CI"), "true")
}

func FullVersion() string {
if os.Getenv("CI") == "true" {
if CI() {
Version = "99.99.99"
BuildHash = "test"
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/roles/dns/handler_etcd_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dns_test

import (
"testing"

"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/dns"
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/tests"
d "github.com/miekg/dns"
)

func BenchmarkRoleDNS_Etcd(b *testing.B) {
defer tests.Setup(nil)()
rootInst := instance.New()
ctx := tests.Context()
inst := rootInst.ForRole("dns", ctx)
tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyZones,
TestZone,
).String(),
tests.MustJSON(dns.Zone{
HandlerConfigs: []map[string]interface{}{
{
"type": "etcd",
},
},
}),
))
tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyZones,
TestZone,
"foo",
types.DNSRecordTypeA,
"0",
).String(),
tests.MustJSON(dns.Record{
Data: "10.1.2.3",
}),
))

role := dns.New(inst)
_ = role.Start(ctx, RoleConfig())
defer role.Stop()

b.ResetTimer()
for i := 0; i < b.N; i++ {
fw := NewNullDNSWriter()
role.Handler(fw, &d.Msg{
Question: []d.Question{
{
Name: "foo.example.com.",
Qtype: d.TypeA,
Qclass: d.ClassINET,
},
},
})
_ = fw.Msg()
}
}
66 changes: 66 additions & 0 deletions pkg/roles/dns/handler_memory_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dns_test

import (
"testing"

"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/dns"
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/tests"
d "github.com/miekg/dns"
)

func BenchmarkRoleDNS_Memory(b *testing.B) {
defer tests.Setup(nil)()
rootInst := instance.New()
ctx := tests.Context()
inst := rootInst.ForRole("dns", ctx)
tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyZones,
".",
).String(),
tests.MustJSON(dns.Zone{
HandlerConfigs: []map[string]interface{}{
{
"type": "memory",
},
},
}),
))
tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyZones,
".",
"foo",
types.DNSRecordTypeA,
"0",
).String(),
tests.MustJSON(dns.Record{
Data: "10.1.2.3",
}),
))

role := dns.New(inst)
_ = role.Start(ctx, RoleConfig())
defer role.Stop()

b.ResetTimer()
for i := 0; i < b.N; i++ {
fw := NewNullDNSWriter()
role.Handler(fw, &d.Msg{
Question: []d.Question{
{
Name: "foo.",
Qtype: d.TypeA,
Qclass: d.ClassINET,
},
},
})
_ = fw.Msg()
}
}
60 changes: 60 additions & 0 deletions pkg/roles/dns/role_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package dns_test

import (
"testing"

"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/dns"
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/tests"
d "github.com/miekg/dns"
)

func BenchmarkRoleDNS_DefaultRootZone(b *testing.B) {
defer tests.Setup(nil)()
rootInst := instance.New()
ctx := tests.Context()
inst := rootInst.ForRole("dns", ctx)
tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyZones,
".",
).String(),
tests.MustJSON(dns.Zone{
HandlerConfigs: []map[string]interface{}{
{
"type": "memory",
},
{
"type": "etcd",
},
{
"type": "forward_ip",
"to": []string{"127.0.0.1:1053"},
"cache_ttl": 3600,
},
},
}),
))

role := dns.New(inst)
_ = role.Start(ctx, RoleConfig())
defer role.Stop()

b.ResetTimer()
for i := 0; i < b.N; i++ {
fw := NewNullDNSWriter()
role.Handler(fw, &d.Msg{
Question: []d.Question{
{
Name: "gravity.beryju.io.",
Qtype: d.TypeA,
Qclass: d.ClassINET,
},
},
})
_ = fw.Msg()
}
}
4 changes: 3 additions & 1 deletion pkg/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ func ResetEtcd(t *testing.T) {
"/",
clientv3.WithPrefix(),
)
assert.NoError(t, err)
if t != nil {
assert.NoError(t, err)
}
}

func Setup(t *testing.T) func() {
Expand Down

0 comments on commit 0f9f425

Please sign in to comment.