MCPcopy Index your code
hub / github.com/klauspost/compress / TestNewDecoderMemory

Function TestNewDecoderMemory

zstd/decoder_test.go:219–282  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

217}
218
219func TestNewDecoderMemory(t *testing.T) {
220 defer timeout(60 * time.Second)()
221 var testdata bytes.Buffer
222 enc, err := NewWriter(&testdata, WithWindowSize(32<<10), WithSingleSegment(false))
223 if err != nil {
224 t.Fatal(err)
225 }
226 // Write 256KB
227 for i := range 256 {
228 tmp := strings.Repeat(string([]byte{byte(i)}), 1024)
229 _, err := enc.Write([]byte(tmp))
230 if err != nil {
231 t.Fatal(err)
232 }
233 }
234 err = enc.Close()
235 if err != nil {
236 t.Fatal(err)
237 }
238
239 var n = 5000
240 if testing.Short() {
241 n = 200
242 }
243
244 // 16K buffer
245 var tmp [16 << 10]byte
246
247 var before, after runtime.MemStats
248 runtime.GC()
249 runtime.ReadMemStats(&before)
250
251 var decs = make([]*Decoder, n)
252 for i := range decs {
253 // Wrap in NopCloser to avoid shortcut.
254 input := io.NopCloser(bytes.NewBuffer(testdata.Bytes()))
255 decs[i], err = NewReader(input, WithDecoderConcurrency(1), WithDecoderLowmem(true))
256 if err != nil {
257 t.Fatal(err)
258 }
259 }
260
261 for i := range decs {
262 _, err := io.ReadFull(decs[i], tmp[:])
263 if err != nil {
264 t.Fatal(err)
265 }
266 }
267
268 runtime.GC()
269 runtime.ReadMemStats(&after)
270 size := (after.HeapInuse - before.HeapInuse) / uint64(n) / 1024
271
272 const expect = 124
273 t.Log(size, "KiB per decoder")
274 // This is not exact science, but fail if we suddenly get more than 2x what we expect.
275 if size > expect*2 && !testing.Short() {
276 t.Errorf("expected < %dKB per decoder, got %d", expect, size)

Callers

nothing calls this directly

Calls 12

WriteMethod · 0.95
CloseMethod · 0.95
timeoutFunction · 0.85
WithWindowSizeFunction · 0.85
WithSingleSegmentFunction · 0.85
WithDecoderConcurrencyFunction · 0.85
WithDecoderLowmemFunction · 0.85
FatalMethod · 0.80
NewWriterFunction · 0.70
NewReaderFunction · 0.70
BytesMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…