Skip to content

Commit

Permalink
chore: use testify instead of t.Fatal
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Nov 8, 2024
1 parent fcfad5c commit a433956
Show file tree
Hide file tree
Showing 37 changed files with 221 additions and 475 deletions.
22 changes: 7 additions & 15 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"net/http/httptest"
"net/url"
"testing"

"github.com/stretchr/testify/require"
)

func TestConfig(t *testing.T) {
c := Config{}
if c.roundTripper() != DefaultRoundTripper {
t.Fatalf("expected default roundtripper for nil RoundTripper field")
}
require.Equalf(t, c.roundTripper(), DefaultRoundTripper, "expected default roundtripper for nil RoundTripper field")
}

func TestClientURL(t *testing.T) {
Expand Down Expand Up @@ -99,9 +99,7 @@ func TestClientURL(t *testing.T) {

for _, test := range tests {
ep, err := url.Parse(test.address)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

hclient := &httpClient{
endpoint: ep,
Expand Down Expand Up @@ -140,22 +138,16 @@ func BenchmarkClient(b *testing.B) {
client, err := NewClient(Config{
Address: testServer.URL,
})
if err != nil {
b.Fatalf("Failed to initialize client: %v", err)
}
require.NoErrorf(b, err, "Failed to initialize client: %v", err)
url, err := url.Parse(testServer.URL + "/prometheus/api/v1/query?query=up")
if err != nil {
b.Fatalf("Failed to parse url: %v", err)
}
require.NoErrorf(b, err, "Failed to parse url: %v", err)
req := &http.Request{
URL: url,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err := client.Do(ctx, req)
if err != nil {
b.Fatalf("Query failed: %v", err)
}
require.NoErrorf(b, err, "Query failed: %v", err)
}
b.StopTimer()
})
Expand Down
1 change: 0 additions & 1 deletion api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"unsafe"

json "github.com/json-iterator/go"

"github.com/prometheus/common/model"

"github.com/prometheus/client_golang/api"
Expand Down
46 changes: 15 additions & 31 deletions api/prometheus/v1/api_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"time"

jsoniter "github.com/json-iterator/go"

"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
)

func generateData(timeseries, datapoints int) (floatMatrix, histogramMatrix model.Matrix) {
Expand Down Expand Up @@ -111,45 +111,37 @@ func BenchmarkSamplesJsonSerialization(b *testing.B) {
floats, histograms := generateData(timeseriesCount, datapointCount)

floatBytes, err := json.Marshal(floats)
if err != nil {
b.Fatalf("Error marshaling: %v", err)
}
require.NoErrorf(b, err, "Error marshaling: %v", err)
histogramBytes, err := json.Marshal(histograms)
if err != nil {
b.Fatalf("Error marshaling: %v", err)
}
require.NoErrorf(b, err, "Error marshaling: %v", err)

b.Run("marshal", func(b *testing.B) {
b.Run("encoding/json/floats", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(floats); err != nil {
b.Fatal(err)
}
_, err := json.Marshal(floats)
require.NoError(b, err)
}
})
b.Run("jsoniter/floats", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jsoniter.Marshal(floats); err != nil {
b.Fatal(err)
}
_, err := jsoniter.Marshal(floats)
require.NoError(b, err)
}
})
b.Run("encoding/json/histograms", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(histograms); err != nil {
b.Fatal(err)
}
_, err := json.Marshal(histograms)
require.NoError(b, err)
}
})
b.Run("jsoniter/histograms", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jsoniter.Marshal(histograms); err != nil {
b.Fatal(err)
}
_, err := jsoniter.Marshal(histograms)
require.NoError(b, err)
}
})
})
Expand All @@ -159,36 +151,28 @@ func BenchmarkSamplesJsonSerialization(b *testing.B) {
b.ReportAllocs()
var m model.Matrix
for i := 0; i < b.N; i++ {
if err := json.Unmarshal(floatBytes, &m); err != nil {
b.Fatal(err)
}
require.NoError(b, json.Unmarshal(floatBytes, &m))
}
})
b.Run("jsoniter/floats", func(b *testing.B) {
b.ReportAllocs()
var m model.Matrix
for i := 0; i < b.N; i++ {
if err := jsoniter.Unmarshal(floatBytes, &m); err != nil {
b.Fatal(err)
}
require.NoError(b, jsoniter.Unmarshal(floatBytes, &m))
}
})
b.Run("encoding/json/histograms", func(b *testing.B) {
b.ReportAllocs()
var m model.Matrix
for i := 0; i < b.N; i++ {
if err := json.Unmarshal(histogramBytes, &m); err != nil {
b.Fatal(err)
}
require.NoError(b, json.Unmarshal(histogramBytes, &m))
}
})
b.Run("jsoniter/histograms", func(b *testing.B) {
b.ReportAllocs()
var m model.Matrix
for i := 0; i < b.N; i++ {
if err := jsoniter.Unmarshal(histogramBytes, &m); err != nil {
b.Fatal(err)
}
require.NoError(b, jsoniter.Unmarshal(histogramBytes, &m))
}
})
})
Expand Down
Loading

0 comments on commit a433956

Please sign in to comment.