|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package filelogreceiver |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/file" |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filelogreceiver/internal/metadata" |
| 14 | + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filelogreceiver/internal/testutil" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "go.opentelemetry.io/collector/component/componenttest" |
| 17 | + "go.opentelemetry.io/collector/consumer/consumertest" |
| 18 | + "go.opentelemetry.io/collector/receiver/receivertest" |
| 19 | +) |
| 20 | + |
| 21 | +func BenchmarkReadSingleStaticFile(b *testing.B) { |
| 22 | + testCases := []struct { |
| 23 | + numLines int |
| 24 | + }{ |
| 25 | + { |
| 26 | + numLines: 0, |
| 27 | + }, |
| 28 | + { |
| 29 | + numLines: 1, |
| 30 | + }, |
| 31 | + { |
| 32 | + numLines: 10, |
| 33 | + }, |
| 34 | + { |
| 35 | + numLines: 20, |
| 36 | + }, |
| 37 | + { |
| 38 | + numLines: 100, |
| 39 | + }, |
| 40 | + { |
| 41 | + numLines: 200, |
| 42 | + }, |
| 43 | + { |
| 44 | + numLines: 1_000, |
| 45 | + }, |
| 46 | + { |
| 47 | + numLines: 10_000, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + for _, tc := range testCases { |
| 52 | + b.Run(fmt.Sprintf("%d-lines", tc.numLines), func(b *testing.B) { |
| 53 | + benchmarkReadSingleStaticFile(b, tc.numLines) |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func benchmarkReadSingleStaticFile(b *testing.B, numLines int) { |
| 59 | + logFileGenerator := testutil.NewLogFileGenerator(b) |
| 60 | + logFilePath := logFileGenerator.GenerateLogFile(numLines, 999) |
| 61 | + |
| 62 | + cfg := &FileLogConfig{ |
| 63 | + InputConfig: func() file.Config { |
| 64 | + c := file.NewConfig() |
| 65 | + c.Include = []string{logFilePath} |
| 66 | + c.PollInterval = time.Microsecond |
| 67 | + c.StartAt = "beginning" |
| 68 | + return *c |
| 69 | + }(), |
| 70 | + } |
| 71 | + sink := new(consumertest.LogsSink) |
| 72 | + f := NewFactory() |
| 73 | + |
| 74 | + b.ResetTimer() |
| 75 | + for range b.N { |
| 76 | + rcvr, err := f.CreateLogs(context.Background(), receivertest.NewNopSettingsWithType(metadata.Type), cfg, sink) |
| 77 | + require.NoError(b, err) |
| 78 | + require.NoError(b, rcvr.Start(context.Background(), componenttest.NewNopHost())) |
| 79 | + |
| 80 | + require.Eventually(b, expectNLogs(sink, numLines), 2*time.Second, 2*time.Microsecond) |
| 81 | + sink.Reset() |
| 82 | + |
| 83 | + require.NoError(b, rcvr.Shutdown(context.Background())) |
| 84 | + } |
| 85 | +} |
0 commit comments