Skip to content

Commit

Permalink
Add a simple benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Nov 29, 2021
1 parent 18ea562 commit 6172746
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/pint
/.cover

dist/
/memprofile.out
/dist/
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ ifndef CI
else
go tool cover -html=$(COVER_PROFILE) -o=.cover/all.html
endif

.PHONY: benchmark
benchmark:
go test \
-v \
-count=20 \
-run=none \
-bench=. \
-benchmem \
-memprofile memprofile.out \
./cmd/pint
161 changes: 161 additions & 0 deletions cmd/pint/lint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package main

import (
"fmt"
"os"
"path"
"strings"
"testing"
)

func mockRules(dir string, filesCount, rulesPerFile int) error {
var rulePath, c string
var err error
var content strings.Builder
for i := 1; i <= filesCount; i++ {
content.Reset()
rulePath = path.Join(dir, fmt.Sprintf("%d_rules.yaml", i))
for j := 1; j <= rulesPerFile; j++ {
c = fmt.Sprintf("- record: %d_rule\n expr: sum(foo) without(instance)\n labels:\n foo: bar\n\n", j)
if _, err = content.WriteString(c); err != nil {
return err
}
}

if err = os.WriteFile(rulePath, []byte(content.String()), 0644); err != nil {
return err
}
}
return nil
}

func mockConfig(configPath string) error {
content := `
rule {
reject ".* +.*" {
label_keys = true
annotation_keys = true
}
reject "https?://.+" {
label_keys = true
label_values = true
}
vector_matching {}
}
rule {
match {
kind = "alerting"
}
annotation "summary" {
severity = "bug"
required = true
}
annotation "dashboard" {
severity = "bug"
value = "https://grafana.example.com/(.+)"
}
label "priority" {
severity = "bug"
value = "(info|warning|critical)"
required = true
}
label "notify" {
severity = "bug"
required = true
}
label "component" {
severity = "bug"
required = true
}
alerts {
range = "1d"
step = "1m"
resolve = "5m"
}
series {}
template {}
}
rule {
match {
kind = "alerting"
label "notify" {
value = "(?:.*\\s+)?(chat|pagerduty|jira)(?:\\s+.*)?"
}
}
annotation "link" {
severity = "bug"
value = "https://alert-references.(?:s3.)?cfdata.org/(.+)"
required = true
}
comparison {}
}
rule {
match {
kind = "recording"
}
aggregate ".+" {
severity = "bug"
keep = ["job"]
}
cost {
bytesPerSample = 4036
}
}
rule {
match {
kind = "recording"
path = ".*"
}
aggregate "dc(?:_.+)?:.+" {
severity = "bug"
strip = ["instance"]
}
}
`
return os.WriteFile(configPath, []byte(content), 0644)
}

func BenchmarkLint(b *testing.B) {
var err error

rulesDir := b.TempDir()
if err = mockRules(rulesDir, 100, 50); err != nil {
b.Error(err)
b.FailNow()
}

configPath := path.Join(rulesDir, ".pint.hcl")
if err = mockConfig(configPath); err != nil {
b.Error(err)
b.FailNow()
}

app := newApp()
var args = []string{"pint", "-c", configPath, "-l", "error", "lint", "--offline", rulesDir + "/*.yaml"}
for n := 0; n < b.N; n++ {
if err = app.Run(args); err != nil {
b.Error(err)
b.FailNow()
}
}
}

0 comments on commit 6172746

Please sign in to comment.