From a60c45ebae626529277dbeb3b214824bfc59bbfd Mon Sep 17 00:00:00 2001 From: Ridwan Sharif Date: Tue, 10 Sep 2024 17:44:34 +0000 Subject: [PATCH] add micro benchmark test --- exporter/collector/metrics.go | 8 +++-- exporter/collector/metrics_test.go | 48 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/exporter/collector/metrics.go b/exporter/collector/metrics.go index e9b458840..e5dc6bee4 100644 --- a/exporter/collector/metrics.go +++ b/exporter/collector/metrics.go @@ -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 } @@ -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) { // 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). @@ -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 diff --git a/exporter/collector/metrics_test.go b/exporter/collector/metrics_test.go index 5f58697e8..e86571f48 100644 --- a/exporter/collector/metrics_test.go +++ b/exporter/collector/metrics_test.go @@ -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) + } +}