Skip to content

Commit

Permalink
[chore][pkg/stanza/adapter] Fix converter benchmark (#21928)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored May 18, 2023
1 parent c4ea0bc commit 19cacb0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
24 changes: 22 additions & 2 deletions pkg/stanza/adapter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,35 @@ type Converter struct {
logger *zap.Logger
}

func NewConverter(logger *zap.Logger) *Converter {
return &Converter{
type converterOption interface {
apply(*Converter)
}

func withWorkerCount(workerCount int) converterOption {
return workerCountOption{workerCount}
}

type workerCountOption struct {
workerCount int
}

func (o workerCountOption) apply(c *Converter) {
c.workerCount = o.workerCount
}

func NewConverter(logger *zap.Logger, opts ...converterOption) *Converter {
c := &Converter{
workerChan: make(chan []*entry.Entry),
workerCount: int(math.Max(1, float64(runtime.NumCPU()/4))),
pLogsChan: make(chan plog.Logs),
stopChan: make(chan struct{}),
flushChan: make(chan plog.Logs),
logger: logger,
}
for _, opt := range opts {
opt.apply(c)
}
return c
}

func (c *Converter) Start() {
Expand Down
3 changes: 1 addition & 2 deletions pkg/stanza/adapter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,12 +822,11 @@ func BenchmarkConverter(b *testing.B) {
b.Run(fmt.Sprintf("worker_count=%d", wc), func(b *testing.B) {
for i := 0; i < b.N; i++ {

converter := NewConverter(zap.NewNop())
converter := NewConverter(zap.NewNop(), withWorkerCount(wc))
converter.Start()
defer converter.Stop()

b.ReportAllocs()
b.ResetTimer()

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

0 comments on commit 19cacb0

Please sign in to comment.