Skip to content

Commit

Permalink
add micro benchmark test
Browse files Browse the repository at this point in the history
  • Loading branch information
ridwanmsharif committed Sep 10, 2024
1 parent 2adedca commit a60c45e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
8 changes: 6 additions & 2 deletions exporter/collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func (me *MetricsExporter) exportToTimeSeries(ctx context.Context, req *monitori
func (me *MetricsExporter) export(ctx context.Context, req *monitoringpb.CreateTimeSeriesRequest) error {
// if this is an empty request, skip it
// empty requests are used by the WAL to signal the end of pending data
if req == nil || (req.TimeSeries == nil && req.Name == "") {
if isEmptyReq(req) {
return nil
}

Expand Down Expand Up @@ -504,7 +504,7 @@ func (me *MetricsExporter) readWALAndExport(ctx context.Context) error {
// If we are at the last index, and this last index is not an empty request
// (we use empty requests to fill out the end of a log, and if we didn't check for them
// this would loop constantly adding empty requests onto the end)
if readIndex == writeIndex && (req != nil && (req.Name != "" || req.TimeSeries != nil)) {
if readIndex == writeIndex && !isEmptyReq(req) {

Check warning on line 507 in exporter/collector/metrics.go

View check run for this annotation

Codecov / codecov/patch

exporter/collector/metrics.go#L507

Added line #L507 was not covered by tests
// This indicates that we are trying to truncate the last item in the WAL.
// If that is the case, write an empty request so we can truncate the last real request
// (the WAL library requires at least 1 entry).
Expand Down Expand Up @@ -1562,6 +1562,10 @@ func metricPointValueType(pt pmetric.NumberDataPointValueType) metricpb.MetricDe
}
}

func isEmptyReq(req *monitoringpb.CreateTimeSeriesRequest) bool {
return (req == nil || (req.Name == "" && req.TimeSeries == nil))
}

func (me *metricMapper) mapMetricPointKind(m pmetric.Metric) (metricpb.MetricDescriptor_MetricKind, metricpb.MetricDescriptor_ValueType) {
var kind metricpb.MetricDescriptor_MetricKind
var typ metricpb.MetricDescriptor_ValueType
Expand Down
48 changes: 48 additions & 0 deletions exporter/collector/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2605,3 +2605,51 @@ func TestPushMetricsOntoWAL(t *testing.T) {
err = proto.Unmarshal(bytes, req)
require.NoError(t, err)
}

func Benchmark_TestExport(b *testing.B) {
for n := 0; n < b.N; n++ {
mExp := &MetricsExporter{
cfg: DefaultConfig(),
exportFunc: func(ctx context.Context, req *monitoringpb.CreateTimeSeriesRequest) error {
return nil
},
}

req := &monitoringpb.CreateTimeSeriesRequest{Name: "foo"}
_, err := proto.Marshal(req)
require.NoError(b, err)

err = mExp.export(context.Background(), req)
require.NoError(b, err)
}
}

func Benchmark_TestReadWALAndExport(b *testing.B) {
for n := 0; n < b.N; n++ {
tmpDir, _ := os.MkdirTemp("", "wal-test-")
mExp := &MetricsExporter{
cfg: Config{
MetricConfig: MetricConfig{
WALConfig: &WALConfig{
Directory: tmpDir,
},
},
},
exportFunc: func(ctx context.Context, req *monitoringpb.CreateTimeSeriesRequest) error {
return nil
},
}

_, _, err := mExp.setupWAL()
require.NoError(b, err)

req := &monitoringpb.CreateTimeSeriesRequest{Name: "foo"}
bytes, err := proto.Marshal(req)
require.NoError(b, err)
err = mExp.wal.Write(1, bytes)
require.NoError(b, err)

err = mExp.readWALAndExport(context.Background())
require.NoError(b, err)
}
}

0 comments on commit a60c45e

Please sign in to comment.