Skip to content

Commit

Permalink
More complete rules benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Jun 24, 2024
1 parent ac55817 commit bf53a6a
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions cmd/pint/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package main

import (
"context"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"

"github.com/cloudflare/pint/internal/config"
"github.com/cloudflare/pint/internal/discovery"
Expand Down Expand Up @@ -38,10 +43,56 @@ func BenchmarkCheckRules(b *testing.B) {
b.FailNow()
}

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/status/config":
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}`))
case "/api/v1/status/flags":
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"status":"success","data":{"storage.tsdb.retention.time": "1d"}}`))
case "/api/v1/metadata":
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"status":"success","data":{}}`))
case "/api/v1/query":
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[]}}`))
case "/api/v1/query_range":
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"status":"success","data":{"resultType":"matrix","result":[]}}`))
default:
b.Logf("%s %s", r.Method, r.URL.Path)
w.WriteHeader(404)
_, _ = w.Write([]byte(`Not found`))
}
}))
defer srv.Close()

tmp := b.TempDir()
content := []byte(fmt.Sprintf(`prometheus "prom" {
uri = "%s"
timeout = "30s"
uptime = "prometheus_ready"
concurrency = 10
rateLimit = 5000
}
`, srv.URL))
require.NoError(b, os.WriteFile(tmp+"/.pint.hcl", content, 0o644))

ctx := context.Background()
cfg, _ := config.Load("", false)
cfg, err := config.Load(tmp+"/.pint.hcl", false)
require.NoError(b, err)

gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry())
require.NoError(b, gen.GenerateStatic())

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, _ = checkRules(ctx, 10, true, gen, cfg, entries)
_, _ = checkRules(ctx, 10, false, gen, cfg, entries)
}
}

0 comments on commit bf53a6a

Please sign in to comment.