-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2413346
commit c9e1529
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package confighttp // import "go.opentelemetry.io/collector/config/confighttp" | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/klauspost/compress/zstd" | ||
"go.opentelemetry.io/collector/config/configcompression" | ||
) | ||
|
||
func BenchmarkCompression(b *testing.B) { | ||
benchmarks := []struct { | ||
codec configcompression.Type | ||
function func(*testing.B, configcompression.Type, *bytes.Buffer, []byte) | ||
}{ | ||
{ | ||
codec: configcompression.TypeZstd, | ||
function: benchmarkCompression, | ||
}, | ||
{ | ||
codec: configcompression.TypeZstd, | ||
function: benchmarkCompressionNoConcurrency, | ||
}, | ||
} | ||
payload := make([]byte, 10<<20) | ||
|
||
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(fmt.Sprintf(string(benchmark.codec)), func(b *testing.B) { | ||
benchmark.function(b, benchmark.codec, &buffer, payload) | ||
}) | ||
|
||
} | ||
} | ||
|
||
func benchmarkCompression(b *testing.B, codec configcompression.Type, buf *bytes.Buffer, payload []byte) { | ||
// In case only the decompression benchmark are run, we use this flags to | ||
// detect whether we have to compress the payload before the decompression | ||
// benchmarks. | ||
|
||
b.Run("compress", func(b *testing.B) { | ||
stringReader := strings.NewReader(string(payload)) | ||
stringReadCloser := io.NopCloser(stringReader) | ||
var enc io.Writer | ||
/*compressor, err := newCompressor(codec) | ||
if err != nil { | ||
b.Fatal(err) | ||
}*/ | ||
b.ResetTimer() | ||
b.ReportAllocs() | ||
b.SetBytes(int64(len(payload))) | ||
for i := 0; i < b.N; i++ { | ||
// compressor.compress(buf, stringReadCloser) | ||
enc, _ = zstd.NewWriter(nil, zstd.WithEncoderConcurrency(5)) | ||
enc.(writeCloserReset).Reset(buf) | ||
io.Copy(enc, stringReadCloser) | ||
} | ||
}) | ||
} | ||
|
||
func benchmarkCompressionNoConcurrency(b *testing.B, codec configcompression.Type, buf *bytes.Buffer, payload []byte) { | ||
// In case only the decompression benchmark are run, we use this flags to | ||
// detect whether we have to compress the payload before the decompression | ||
// benchmarks. | ||
|
||
b.Run("compress", func(b *testing.B) { | ||
stringReader := strings.NewReader(string(payload)) | ||
stringReadCloser := io.NopCloser(stringReader) | ||
var enc io.Writer | ||
b.ResetTimer() | ||
b.ReportAllocs() | ||
b.SetBytes(int64(len(payload))) | ||
for i := 0; i < b.N; i++ { | ||
enc, _ = zstd.NewWriter(nil, zstd.WithEncoderConcurrency(1)) | ||
enc.(writeCloserReset).Reset(buf) | ||
io.Copy(enc, stringReadCloser) | ||
} | ||
}) | ||
} |