Files
Pablo BaeyensandGitHub 3289eff42b [chore] Skip or bump some benchmarks to reduce noise (#14258)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

<!-- Issue number if applicable -->

Bumps size to 2k on some benchmarks to hopefully make those benchmarks
more reliable.

Skips a bunch of other benchmarks that are allocation-heavy and
currently are more noise than signal.

#### Link to tracking issue
Relates to #14257
2025-12-04 12:46:28 +00:00

88 lines
2.3 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// This file contains helper functions regarding compression/decompression for confighttp.
package confighttp // import "go.opentelemetry.io/collector/config/confighttp"
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
"github.com/klauspost/compress/zstd"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config/configcompression"
"go.opentelemetry.io/collector/internal/testutil"
)
func BenchmarkCompression(b *testing.B) {
testutil.SkipGCHeavyBench(b)
benchmarks := []struct {
codec configcompression.Type
name string
function func(*testing.B, configcompression.Type, *bytes.Buffer, []byte)
}{
{
codec: configcompression.TypeZstd,
name: "zstdWithConcurrency",
function: benchmarkCompression,
},
{
codec: configcompression.TypeZstd,
name: "zstdNoConcurrency",
function: benchmarkCompressionNoConcurrency,
},
}
payload := make([]byte, 10<<22)
buffer := bytes.Buffer{}
buffer.Grow(len(payload))
ts := &bytes.Buffer{}
defer func() {
fmt.Printf("input => %.2f MB\n", float64(len(payload))/(1024*1024))
fmt.Println(ts)
}()
for i := range benchmarks {
benchmark := &benchmarks[i]
b.Run(benchmark.name, func(b *testing.B) {
benchmark.function(b, benchmark.codec, &buffer, payload)
})
}
}
func benchmarkCompression(b *testing.B, _ configcompression.Type, buf *bytes.Buffer, payload []byte) {
// Concurrency Enabled
stringReader := strings.NewReader(string(payload))
stringReadCloser := io.NopCloser(stringReader)
var enc io.Writer
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(payload)))
for b.Loop() {
enc, _ = zstd.NewWriter(nil, zstd.WithEncoderConcurrency(5))
enc.(writeCloserReset).Reset(buf)
_, copyErr := io.Copy(enc, stringReadCloser)
require.NoError(b, copyErr)
}
}
func benchmarkCompressionNoConcurrency(b *testing.B, _ configcompression.Type, buf *bytes.Buffer, payload []byte) {
stringReader := strings.NewReader(string(payload))
stringReadCloser := io.NopCloser(stringReader)
var enc io.Writer
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(int64(len(payload)))
for b.Loop() {
enc, _ = zstd.NewWriter(nil, zstd.WithEncoderConcurrency(1))
enc.(writeCloserReset).Reset(buf)
_, copyErr := io.Copy(enc, stringReadCloser)
require.NoError(b, copyErr)
}
}