Skip to content

Commit 19cacb0

Browse files
authored
[chore][pkg/stanza/adapter] Fix converter benchmark (#21928)
1 parent c4ea0bc commit 19cacb0

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

pkg/stanza/adapter/converter.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,35 @@ type Converter struct {
7474
logger *zap.Logger
7575
}
7676

77-
func NewConverter(logger *zap.Logger) *Converter {
78-
return &Converter{
77+
type converterOption interface {
78+
apply(*Converter)
79+
}
80+
81+
func withWorkerCount(workerCount int) converterOption {
82+
return workerCountOption{workerCount}
83+
}
84+
85+
type workerCountOption struct {
86+
workerCount int
87+
}
88+
89+
func (o workerCountOption) apply(c *Converter) {
90+
c.workerCount = o.workerCount
91+
}
92+
93+
func NewConverter(logger *zap.Logger, opts ...converterOption) *Converter {
94+
c := &Converter{
7995
workerChan: make(chan []*entry.Entry),
8096
workerCount: int(math.Max(1, float64(runtime.NumCPU()/4))),
8197
pLogsChan: make(chan plog.Logs),
8298
stopChan: make(chan struct{}),
8399
flushChan: make(chan plog.Logs),
84100
logger: logger,
85101
}
102+
for _, opt := range opts {
103+
opt.apply(c)
104+
}
105+
return c
86106
}
87107

88108
func (c *Converter) Start() {

pkg/stanza/adapter/converter_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,12 +822,11 @@ func BenchmarkConverter(b *testing.B) {
822822
b.Run(fmt.Sprintf("worker_count=%d", wc), func(b *testing.B) {
823823
for i := 0; i < b.N; i++ {
824824

825-
converter := NewConverter(zap.NewNop())
825+
converter := NewConverter(zap.NewNop(), withWorkerCount(wc))
826826
converter.Start()
827827
defer converter.Stop()
828828

829829
b.ReportAllocs()
830-
b.ResetTimer()
831830

832831
go func() {
833832
for from := 0; from < entryCount; from += int(batchSize) {

0 commit comments

Comments
 (0)