1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
// Copyright 2019+ Klaus Post. All rights reserved.
// License information can be found in the LICENSE file.
package zstd
import (
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
"testing"
"time"
)
var isRaceTest bool
// Fuzzing tweaks:
var fuzzStartF = flag.Int("fuzz-start", int(SpeedFastest), "Start fuzzing at this level")
var fuzzEndF = flag.Int("fuzz-end", int(SpeedBestCompression), "End fuzzing at this level (inclusive)")
var fuzzMaxF = flag.Int("fuzz-max", 1<<20, "Maximum input size")
func TestMain(m *testing.M) {
flag.Parse()
ec := m.Run()
if ec == 0 && runtime.NumGoroutine() > 2 {
n := 0
for n < 15 {
n++
time.Sleep(time.Second)
if runtime.NumGoroutine() == 2 {
os.Exit(0)
}
}
fmt.Println("goroutines:", runtime.NumGoroutine())
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
os.Exit(1)
}
os.Exit(ec)
}
func TestMatchLen(t *testing.T) {
a := make([]byte, 130)
for i := range a {
a[i] = byte(i)
}
b := append([]byte{}, a...)
check := func(x, y []byte, l int) {
if m := matchLen(x, y); m != l {
t.Error("expected", l, "got", m)
}
}
for l := range a {
a[l] = ^a[l]
check(a, b, l)
check(a[:l], b, l)
a[l] = ^a[l]
}
}
func TestWriterMemUsage(t *testing.T) {
testMem := func(t *testing.T, fn func()) {
var before, after runtime.MemStats
var w io.Writer
if false {
f, err := os.Create(strings.ReplaceAll(fmt.Sprintf("%s.pprof", t.Name()), "/", "_"))
if err != nil {
log.Fatal(err)
}
defer f.Close()
w = f
t.Logf("opened memory profile %s", t.Name())
}
runtime.GC()
runtime.ReadMemStats(&before)
fn()
runtime.GC()
runtime.ReadMemStats(&after)
if w != nil {
pprof.WriteHeapProfile(w)
}
t.Log("wrote profile")
t.Logf("%s: Memory Used: %dMB, %d allocs", t.Name(), (after.HeapInuse-before.HeapInuse)/1024/1024, after.HeapObjects-before.HeapObjects)
}
data := make([]byte, 10<<20)
t.Run("enc-all-lower", func(t *testing.T) {
for level := SpeedFastest; level <= SpeedBestCompression; level++ {
t.Run(fmt.Sprint("level-", level), func(t *testing.T) {
var zr *Encoder
var err error
dst := make([]byte, 0, len(data)*2)
testMem(t, func() {
zr, err = NewWriter(io.Discard, WithEncoderConcurrency(32), WithEncoderLevel(level), WithLowerEncoderMem(false), WithWindowSize(1<<20))
if err != nil {
t.Fatal(err)
}
for i := 0; i < 100; i++ {
_ = zr.EncodeAll(data, dst[:0])
}
})
zr.Close()
})
}
})
}
var data = []byte{1, 2, 3}
func newZstdWriter() (*Encoder, error) {
return NewWriter(
io.Discard,
WithEncoderLevel(SpeedBetterCompression),
WithEncoderConcurrency(16), // we implicitly get this concurrency level if we run on 16 core CPU
WithLowerEncoderMem(false),
WithWindowSize(1<<20),
)
}
func BenchmarkMem(b *testing.B) {
b.Run("flush", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w, err := newZstdWriter()
if err != nil {
b.Fatal(err)
}
for j := 0; j < 16; j++ {
w.Reset(io.Discard)
if _, err := w.Write(data); err != nil {
b.Fatal(err)
}
if err := w.Flush(); err != nil {
b.Fatal(err)
}
if err := w.Close(); err != nil {
b.Fatal(err)
}
}
}
})
b.Run("no-flush", func(b *testing.B) {
// Will use encodeAll for block.
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w, err := newZstdWriter()
if err != nil {
b.Fatal(err)
}
for j := 0; j < 16; j++ {
w.Reset(io.Discard)
if _, err := w.Write(data); err != nil {
b.Fatal(err)
}
if err := w.Close(); err != nil {
b.Fatal(err)
}
}
}
})
}
|