tailscale/smallzstd/zstd_test.go
Will Norris 71029cea2d all: update copyright and license headers
This updates all source files to use a new standard header for copyright
and license declaration.  Notably, copyright no longer includes a date,
and we now use the standard SPDX-License-Identifier header.

This commit was done almost entirely mechanically with perl, and then
some minimal manual fixes.

Updates #6865

Signed-off-by: Will Norris <will@tailscale.com>
2023-01-27 15:36:29 -08:00

131 lines
2.8 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package smallzstd
import (
"os"
"testing"
"github.com/klauspost/compress/zstd"
)
func BenchmarkSmallEncoder(b *testing.B) {
benchEncoder(b, func() (*zstd.Encoder, error) { return NewEncoder(nil) })
}
func BenchmarkSmallEncoderWithBuild(b *testing.B) {
benchEncoderWithConstruction(b, func() (*zstd.Encoder, error) { return NewEncoder(nil) })
}
func BenchmarkStockEncoder(b *testing.B) {
benchEncoder(b, func() (*zstd.Encoder, error) { return zstd.NewWriter(nil) })
}
func BenchmarkStockEncoderWithBuild(b *testing.B) {
benchEncoderWithConstruction(b, func() (*zstd.Encoder, error) { return zstd.NewWriter(nil) })
}
func BenchmarkSmallDecoder(b *testing.B) {
benchDecoder(b, func() (*zstd.Decoder, error) { return NewDecoder(nil) })
}
func BenchmarkSmallDecoderWithBuild(b *testing.B) {
benchDecoderWithConstruction(b, func() (*zstd.Decoder, error) { return NewDecoder(nil) })
}
func BenchmarkStockDecoder(b *testing.B) {
benchDecoder(b, func() (*zstd.Decoder, error) { return zstd.NewReader(nil) })
}
func BenchmarkStockDecoderWithBuild(b *testing.B) {
benchDecoderWithConstruction(b, func() (*zstd.Decoder, error) { return zstd.NewReader(nil) })
}
func benchEncoder(b *testing.B, mk func() (*zstd.Encoder, error)) {
b.ReportAllocs()
in := testdata(b)
out := make([]byte, 0, 10<<10) // 10kiB
e, err := mk()
if err != nil {
b.Fatalf("making encoder: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
e.EncodeAll(in, out)
}
}
func benchEncoderWithConstruction(b *testing.B, mk func() (*zstd.Encoder, error)) {
b.ReportAllocs()
in := testdata(b)
out := make([]byte, 0, 10<<10) // 10kiB
b.ResetTimer()
for i := 0; i < b.N; i++ {
e, err := mk()
if err != nil {
b.Fatalf("making encoder: %v", err)
}
e.EncodeAll(in, out)
}
}
func benchDecoder(b *testing.B, mk func() (*zstd.Decoder, error)) {
b.ReportAllocs()
in := compressedTestdata(b)
out := make([]byte, 0, 10<<10)
d, err := mk()
if err != nil {
b.Fatalf("creating decoder: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
d.DecodeAll(in, out)
}
}
func benchDecoderWithConstruction(b *testing.B, mk func() (*zstd.Decoder, error)) {
b.ReportAllocs()
in := compressedTestdata(b)
out := make([]byte, 0, 10<<10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
d, err := mk()
if err != nil {
b.Fatalf("creating decoder: %v", err)
}
d.DecodeAll(in, out)
}
}
func testdata(b *testing.B) []byte {
b.Helper()
in, err := os.ReadFile("testdata")
if err != nil {
b.Fatalf("reading testdata: %v", err)
}
return in
}
func compressedTestdata(b *testing.B) []byte {
b.Helper()
uncomp := testdata(b)
e, err := NewEncoder(nil)
if err != nil {
b.Fatalf("creating encoder: %v", err)
}
return e.EncodeAll(uncomp, nil)
}