diff --git a/processor/batchprocessor/splitlogs_test.go b/processor/batchprocessor/splitlogs_test.go index 229ccf5aff63..1cd428f2f715 100644 --- a/processor/batchprocessor/splitlogs_test.go +++ b/processor/batchprocessor/splitlogs_test.go @@ -127,58 +127,6 @@ func TestSplitLogsMultipleResourceLogs_split_size_greater_than_log_size(t *testi assert.Equal(t, "test-log-int-1-4", split.ResourceLogs().At(1).InstrumentationLibraryLogs().At(0).Logs().At(4).Name()) } -func BenchmarkSplitLogs(b *testing.B) { - md := pdata.NewLogs() - rms := md.ResourceLogs() - for i := 0; i < 20; i++ { - testdata.GenerateLogsManyLogRecordsSameResource(20).ResourceLogs().MoveAndAppendTo(md.ResourceLogs()) - ms := rms.At(rms.Len() - 1).InstrumentationLibraryLogs().At(0).Logs() - for i := 0; i < ms.Len(); i++ { - ms.At(i).SetName(getTestLogName(1, i)) - } - } - - if b.N > 100000 { - b.Skipf("SKIP: b.N too high, set -benchtine=x with n < 100000") - } - - clones := make([]pdata.Logs, b.N) - for n := 0; n < b.N; n++ { - clones[n] = md.Clone() - } - - b.ReportAllocs() - b.ResetTimer() - for n := 0; n < b.N; n++ { - cloneReq := clones[n] - split := splitLogs(128, cloneReq) - if split.LogRecordCount() != 128 || cloneReq.LogRecordCount() != 400-128 { - b.Fail() - } - } -} - -func BenchmarkCloneLogs(b *testing.B) { - md := pdata.NewLogs() - rms := md.ResourceLogs() - for i := 0; i < 20; i++ { - testdata.GenerateLogsManyLogRecordsSameResource(20).ResourceLogs().MoveAndAppendTo(md.ResourceLogs()) - ms := rms.At(rms.Len() - 1).InstrumentationLibraryLogs().At(0).Logs() - for i := 0; i < ms.Len(); i++ { - ms.At(i).SetName(getTestLogName(1, i)) - } - } - - b.ReportAllocs() - b.ResetTimer() - for n := 0; n < b.N; n++ { - cloneReq := md.Clone() - if cloneReq.LogRecordCount() != 400 { - b.Fail() - } - } -} - func TestSplitLogsMultipleILL(t *testing.T) { td := testdata.GenerateLogsManyLogRecordsSameResource(20) logs := td.ResourceLogs().At(0).InstrumentationLibraryLogs().At(0).Logs() @@ -208,3 +156,34 @@ func TestSplitLogsMultipleILL(t *testing.T) { assert.Equal(t, "test-log-int-0-0", split.ResourceLogs().At(0).InstrumentationLibraryLogs().At(0).Logs().At(0).Name()) assert.Equal(t, "test-log-int-0-4", split.ResourceLogs().At(0).InstrumentationLibraryLogs().At(0).Logs().At(4).Name()) } + +func BenchmarkSplitLogs(b *testing.B) { + md := pdata.NewLogs() + rms := md.ResourceLogs() + for i := 0; i < 20; i++ { + testdata.GenerateLogsManyLogRecordsSameResource(20).ResourceLogs().MoveAndAppendTo(md.ResourceLogs()) + ms := rms.At(rms.Len() - 1).InstrumentationLibraryLogs().At(0).Logs() + for i := 0; i < ms.Len(); i++ { + ms.At(i).SetName(getTestLogName(1, i)) + } + } + + if b.N > 100000 { + b.Skipf("SKIP: b.N too high, set -benchtime=x with n < 100000") + } + + clones := make([]pdata.Logs, b.N) + for n := 0; n < b.N; n++ { + clones[n] = md.Clone() + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + cloneReq := clones[n] + split := splitLogs(128, cloneReq) + if split.LogRecordCount() != 128 || cloneReq.LogRecordCount() != 400-128 { + b.Fail() + } + } +} diff --git a/processor/batchprocessor/splitmetrics.go b/processor/batchprocessor/splitmetrics.go index 04a33f65ce6e..a92461f491f9 100644 --- a/processor/batchprocessor/splitmetrics.go +++ b/processor/batchprocessor/splitmetrics.go @@ -80,42 +80,46 @@ func splitMetrics(size int, src pdata.Metrics) pdata.Metrics { } // resourceMetricsDataPointCount calculates the total number of data points. -func resourceMetricsDataPointCount(rs pdata.ResourceMetrics) (dataPointCount int) { - for k := 0; k < rs.InstrumentationLibraryMetrics().Len(); k++ { - dataPointCount += metricSliceDataPointCount(rs.InstrumentationLibraryMetrics().At(k).Metrics()) +func resourceMetricsDataPointCount(rs pdata.ResourceMetrics) int { + dataPointCount := 0 + ilms := rs.InstrumentationLibraryMetrics() + for k := 0; k < ilms.Len(); k++ { + dataPointCount += metricSliceDataPointCount(ilms.At(k).Metrics()) } - return + return dataPointCount } // metricSliceDataPointCount calculates the total number of data points. -func metricSliceDataPointCount(ms pdata.MetricSlice) (dataPointCount int) { +func metricSliceDataPointCount(ms pdata.MetricSlice) int { + dataPointCount := 0 for k := 0; k < ms.Len(); k++ { dataPointCount += metricDataPointCount(ms.At(k)) } - return + return dataPointCount } // metricDataPointCount calculates the total number of data points. -func metricDataPointCount(ms pdata.Metric) (dataPointCount int) { +func metricDataPointCount(ms pdata.Metric) int { switch ms.DataType() { case pdata.MetricDataTypeGauge: - dataPointCount = ms.Gauge().DataPoints().Len() + return ms.Gauge().DataPoints().Len() case pdata.MetricDataTypeSum: - dataPointCount = ms.Sum().DataPoints().Len() + return ms.Sum().DataPoints().Len() case pdata.MetricDataTypeHistogram: - dataPointCount = ms.Histogram().DataPoints().Len() + return ms.Histogram().DataPoints().Len() case pdata.MetricDataTypeSummary: - dataPointCount = ms.Summary().DataPoints().Len() + return ms.Summary().DataPoints().Len() } - return + return 0 } // splitMetric removes metric points from the input data and moves data of the specified size to destination. // Returns size of moved data and boolean describing, whether the metric should be removed from original slice. func splitMetric(ms, dest pdata.Metric, size int) (int, bool) { - if metricDataPointCount(ms) <= size { + mdDPC := metricDataPointCount(ms) + if mdDPC <= size { ms.MoveTo(dest) - return metricDataPointCount(dest), true + return mdDPC, true } dest.SetDataType(ms.DataType()) @@ -125,57 +129,55 @@ func splitMetric(ms, dest pdata.Metric, size int) (int, bool) { switch ms.DataType() { case pdata.MetricDataTypeGauge: - src := ms.Gauge().DataPoints() - dst := dest.Gauge().DataPoints() - dst.EnsureCapacity(size) - i := 0 - src.RemoveIf(func(dp pdata.NumberDataPoint) bool { - defer func() { i++ }() - if i < size { - dp.MoveTo(dst.AppendEmpty()) - return true - } - return false - }) + return splitNumberDataPoints(ms.Gauge().DataPoints(), dest.Gauge().DataPoints(), size) case pdata.MetricDataTypeSum: - src := ms.Sum().DataPoints() - dst := dest.Sum().DataPoints() - dst.EnsureCapacity(size) - i := 0 - src.RemoveIf(func(dp pdata.NumberDataPoint) bool { - defer func() { i++ }() - if i < size { - dp.MoveTo(dst.AppendEmpty()) - return true - } - return false - }) + return splitNumberDataPoints(ms.Sum().DataPoints(), dest.Sum().DataPoints(), size) case pdata.MetricDataTypeHistogram: - src := ms.Histogram().DataPoints() - dst := dest.Histogram().DataPoints() - dst.EnsureCapacity(size) - i := 0 - src.RemoveIf(func(dp pdata.HistogramDataPoint) bool { - defer func() { i++ }() - if i < size { - dp.MoveTo(dst.AppendEmpty()) - return true - } - return false - }) + return splitHistogramDataPoints(ms.Histogram().DataPoints(), dest.Histogram().DataPoints(), size) case pdata.MetricDataTypeSummary: - src := ms.Summary().DataPoints() - dst := dest.Summary().DataPoints() - dst.EnsureCapacity(size) - i := 0 - src.RemoveIf(func(dp pdata.SummaryDataPoint) bool { - defer func() { i++ }() - if i < size { - dp.MoveTo(dst.AppendEmpty()) - return true - } - return false - }) + return splitSummaryDataPoints(ms.Summary().DataPoints(), dest.Summary().DataPoints(), size) } return size, false } + +func splitNumberDataPoints(src, dst pdata.NumberDataPointSlice, size int) (int, bool) { + dst.EnsureCapacity(size) + i := 0 + src.RemoveIf(func(dp pdata.NumberDataPoint) bool { + if i < size { + dp.MoveTo(dst.AppendEmpty()) + i++ + return true + } + return false + }) + return size, false +} + +func splitHistogramDataPoints(src, dst pdata.HistogramDataPointSlice, size int) (int, bool) { + dst.EnsureCapacity(size) + i := 0 + src.RemoveIf(func(dp pdata.HistogramDataPoint) bool { + if i < size { + dp.MoveTo(dst.AppendEmpty()) + i++ + return true + } + return false + }) + return size, false +} + +func splitSummaryDataPoints(src, dst pdata.SummaryDataPointSlice, size int) (int, bool) { + dst.EnsureCapacity(size) + i := 0 + src.RemoveIf(func(dp pdata.SummaryDataPoint) bool { + if i < size { + dp.MoveTo(dst.AppendEmpty()) + i++ + return true + } + return false + }) + return size, false +} diff --git a/processor/batchprocessor/splitmetrics_test.go b/processor/batchprocessor/splitmetrics_test.go index fee708927c28..3218e707ed7a 100644 --- a/processor/batchprocessor/splitmetrics_test.go +++ b/processor/batchprocessor/splitmetrics_test.go @@ -136,58 +136,6 @@ func TestSplitMetricsMultipleResourceSpans_SplitSizeGreaterThanMetricSize(t *tes assert.Equal(t, "test-metric-int-1-4", split.ResourceMetrics().At(1).InstrumentationLibraryMetrics().At(0).Metrics().At(4).Name()) } -func BenchmarkSplitMetrics(b *testing.B) { - md := pdata.NewMetrics() - rms := md.ResourceMetrics() - for i := 0; i < 20; i++ { - testdata.GenerateMetricsManyMetricsSameResource(20).ResourceMetrics().MoveAndAppendTo(md.ResourceMetrics()) - ms := rms.At(rms.Len() - 1).InstrumentationLibraryMetrics().At(0).Metrics() - for i := 0; i < ms.Len(); i++ { - ms.At(i).SetName(getTestMetricName(1, i)) - } - } - - if b.N > 100000 { - b.Skipf("SKIP: b.N too high, set -benchtine=x with n < 100000") - } - - dataPointCount := metricDataPointCount(md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0)) - clones := make([]pdata.Metrics, b.N) - for n := 0; n < b.N; n++ { - clones[n] = md.Clone() - } - b.ReportAllocs() - b.ResetTimer() - for n := 0; n < b.N; n++ { - cloneReq := clones[n] - split := splitMetrics(128*dataPointCount, cloneReq) - if split.MetricCount() != 128 || cloneReq.MetricCount() != 400-128 { - b.Fail() - } - } -} - -func BenchmarkCloneMetrics(b *testing.B) { - md := pdata.NewMetrics() - rms := md.ResourceMetrics() - for i := 0; i < 20; i++ { - testdata.GenerateMetricsManyMetricsSameResource(20).ResourceMetrics().MoveAndAppendTo(md.ResourceMetrics()) - ms := rms.At(rms.Len() - 1).InstrumentationLibraryMetrics().At(0).Metrics() - for i := 0; i < ms.Len(); i++ { - ms.At(i).SetName(getTestMetricName(1, i)) - } - } - - b.ReportAllocs() - b.ResetTimer() - for n := 0; n < b.N; n++ { - cloneReq := md.Clone() - if cloneReq.MetricCount() != 400 { - b.Fail() - } - } -} - func TestSplitMetricsUneven(t *testing.T) { md := testdata.GenerateMetricsManyMetricsSameResource(10) metrics := md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics() @@ -274,3 +222,34 @@ func TestSplitMetricsMultipleILM(t *testing.T) { assert.Equal(t, "test-metric-int-0-0", split.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0).Name()) assert.Equal(t, "test-metric-int-0-4", split.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(4).Name()) } + +func BenchmarkSplitMetrics(b *testing.B) { + md := pdata.NewMetrics() + rms := md.ResourceMetrics() + for i := 0; i < 20; i++ { + testdata.GenerateMetricsManyMetricsSameResource(20).ResourceMetrics().MoveAndAppendTo(md.ResourceMetrics()) + ms := rms.At(rms.Len() - 1).InstrumentationLibraryMetrics().At(0).Metrics() + for i := 0; i < ms.Len(); i++ { + ms.At(i).SetName(getTestMetricName(1, i)) + } + } + + if b.N > 100000 { + b.Skipf("SKIP: b.N too high, set -benchtime=x with n < 100000") + } + + dataPointCount := metricDataPointCount(md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0)) + clones := make([]pdata.Metrics, b.N) + for n := 0; n < b.N; n++ { + clones[n] = md.Clone() + } + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + cloneReq := clones[n] + split := splitMetrics(128*dataPointCount, cloneReq) + if split.MetricCount() != 128 || cloneReq.MetricCount() != 400-128 { + b.Fail() + } + } +} diff --git a/processor/batchprocessor/splittraces_test.go b/processor/batchprocessor/splittraces_test.go index 6de10da95987..df4c8b7e0394 100644 --- a/processor/batchprocessor/splittraces_test.go +++ b/processor/batchprocessor/splittraces_test.go @@ -127,36 +127,6 @@ func TestSplitTracesMultipleResourceSpans_SplitSizeGreaterThanSpanSize(t *testin assert.Equal(t, "test-span-1-4", split.ResourceSpans().At(1).InstrumentationLibrarySpans().At(0).Spans().At(4).Name()) } -func BenchmarkSplitTraces(b *testing.B) { - td := pdata.NewTraces() - rms := td.ResourceSpans() - for i := 0; i < 20; i++ { - testdata.GenerateTracesManySpansSameResource(20).ResourceSpans().MoveAndAppendTo(td.ResourceSpans()) - ms := rms.At(rms.Len() - 1).InstrumentationLibrarySpans().At(0).Spans() - for i := 0; i < ms.Len(); i++ { - ms.At(i).SetName(getTestMetricName(1, i)) - } - } - - if b.N > 100000 { - b.Skipf("SKIP: b.N too high, set -benchtine=x with n < 100000") - } - - clones := make([]pdata.Traces, b.N) - for n := 0; n < b.N; n++ { - clones[n] = td.Clone() - } - b.ReportAllocs() - b.ResetTimer() - for n := 0; n < b.N; n++ { - cloneReq := clones[n] - split := splitTraces(128, cloneReq) - if split.SpanCount() != 128 || cloneReq.SpanCount() != 400-128 { - b.Fail() - } - } -} - func BenchmarkCloneSpans(b *testing.B) { td := pdata.NewTraces() rms := td.ResourceSpans() @@ -207,3 +177,33 @@ func TestSplitTracesMultipleILS(t *testing.T) { assert.Equal(t, "test-span-0-0", split.ResourceSpans().At(0).InstrumentationLibrarySpans().At(0).Spans().At(0).Name()) assert.Equal(t, "test-span-0-4", split.ResourceSpans().At(0).InstrumentationLibrarySpans().At(0).Spans().At(4).Name()) } + +func BenchmarkSplitTraces(b *testing.B) { + td := pdata.NewTraces() + rms := td.ResourceSpans() + for i := 0; i < 20; i++ { + testdata.GenerateTracesManySpansSameResource(20).ResourceSpans().MoveAndAppendTo(td.ResourceSpans()) + ms := rms.At(rms.Len() - 1).InstrumentationLibrarySpans().At(0).Spans() + for i := 0; i < ms.Len(); i++ { + ms.At(i).SetName(getTestMetricName(1, i)) + } + } + + if b.N > 100000 { + b.Skipf("SKIP: b.N too high, set -benchtime=x with n < 100000") + } + + clones := make([]pdata.Traces, b.N) + for n := 0; n < b.N; n++ { + clones[n] = td.Clone() + } + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + cloneReq := clones[n] + split := splitTraces(128, cloneReq) + if split.SpanCount() != 128 || cloneReq.SpanCount() != 400-128 { + b.Fail() + } + } +}