Skip to content

Commit

Permalink
Add benchmarks for marshalling
Browse files Browse the repository at this point in the history
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
  • Loading branch information
zenador committed Feb 1, 2023
1 parent 14a8016 commit 01880dc
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 261 deletions.
80 changes: 47 additions & 33 deletions model/value_float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,42 @@ import (
"testing"
)

var (
samplePairMatrixPlain = `[{"metric":{"__name__":"test_metric"},"values":[[1234.567,"123.1"],[12345.678,"123.12"]]},{"metric":{"foo":"bar"},"values":[[2234.567,"223.1"],[22345.678,"223.12"]]}]`
samplePairMatrixValue = Matrix{
&SampleStream{
Metric: Metric{
MetricNameLabel: "test_metric",
},
Values: []SamplePair{
{
Value: 123.1,
Timestamp: 1234567,
},
{
Value: 123.12,
Timestamp: 12345678,
},
},
},
&SampleStream{
Metric: Metric{
"foo": "bar",
},
Values: []SamplePair{
{
Value: 223.1,
Timestamp: 2234567,
},
{
Value: 223.12,
Timestamp: 22345678,
},
},
},
}
)

func TestEqualValues(t *testing.T) {
tests := map[string]struct {
in1, in2 SampleValue
Expand Down Expand Up @@ -231,39 +267,8 @@ func TestMatrixJSON(t *testing.T) {
value: Matrix{},
},
{
plain: `[{"metric":{"__name__":"test_metric"},"values":[[1234.567,"123.1"],[12345.678,"123.12"]]},{"metric":{"foo":"bar"},"values":[[2234.567,"223.1"],[22345.678,"223.12"]]}]`,
value: Matrix{
&SampleStream{
Metric: Metric{
MetricNameLabel: "test_metric",
},
Values: []SamplePair{
{
Value: 123.1,
Timestamp: 1234567,
},
{
Value: 123.12,
Timestamp: 12345678,
},
},
},
&SampleStream{
Metric: Metric{
"foo": "bar",
},
Values: []SamplePair{
{
Value: 223.1,
Timestamp: 2234567,
},
{
Value: 223.12,
Timestamp: 22345678,
},
},
},
},
plain: samplePairMatrixPlain,
value: samplePairMatrixValue,
},
}

Expand Down Expand Up @@ -291,3 +296,12 @@ func TestMatrixJSON(t *testing.T) {
}
}
}

func BenchmarkJSONMarshallingSamplePairMatrix(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := json.Marshal(samplePairMatrixValue)
if err != nil {
b.Fatal("error marshalling")
}
}
}
Loading

0 comments on commit 01880dc

Please sign in to comment.