From 61727467762942d67bc7857f40ea03d6c8fedd6a Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Mon, 29 Nov 2021 12:05:27 +0000 Subject: [PATCH] Add a simple benchmark --- .gitignore | 4 +- Makefile | 11 +++ cmd/pint/lint_test.go | 161 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 cmd/pint/lint_test.go diff --git a/.gitignore b/.gitignore index 1a2631f4..ff9165d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /pint /.cover - -dist/ +/memprofile.out +/dist/ diff --git a/Makefile b/Makefile index a1a148d7..c7f3f219 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cmd/pint/lint_test.go b/cmd/pint/lint_test.go new file mode 100644 index 00000000..ff24d9f6 --- /dev/null +++ b/cmd/pint/lint_test.go @@ -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() + } + } +}