github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/memsys/c_test.go (about)

     1  // Package memsys provides memory management and Slab allocation
     2  // with io.Reader and io.Writer interfaces on top of a scatter-gather lists
     3  // (of reusable buffers)
     4  /*
     5   * Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved.
     6   */
     7  package memsys_test
     8  
     9  // to run: go test -v -run=SGLS -verbose=true
    10  
    11  import (
    12  	"bytes"
    13  	"io"
    14  	"sync"
    15  	"testing"
    16  
    17  	"github.com/NVIDIA/aistore/memsys"
    18  	"github.com/NVIDIA/aistore/tools/tassert"
    19  	"github.com/NVIDIA/aistore/tools/tlog"
    20  )
    21  
    22  const (
    23  	objsize = 100
    24  	objects = 10000
    25  	workers = 1000
    26  )
    27  
    28  // creates 2 SGL, put some data to one of them and them copy from SGL to SGL
    29  func TestSGLStressN(t *testing.T) {
    30  	mem := &memsys.MMSA{Name: "cmem", MinPctFree: 50}
    31  	mem.Init(0)
    32  	defer mem.Terminate(false)
    33  	num := objects
    34  	if testing.Short() {
    35  		num = objects / 10
    36  	}
    37  	wg := &sync.WaitGroup{}
    38  	fn := func() {
    39  		defer wg.Done()
    40  		for i := range num {
    41  			sglR := mem.NewSGL(128)
    42  			sglW := mem.NewSGL(128)
    43  			bufR := make([]byte, objsize)
    44  
    45  			// fill buffer with "unique content"
    46  			for j := range objsize {
    47  				bufR[j] = byte('A') + byte(i%26)
    48  			}
    49  
    50  			// save buffer to SGL
    51  			br := bytes.NewReader(bufR)
    52  			_, err := io.Copy(sglR, br)
    53  			tassert.CheckFatal(t, err)
    54  
    55  			// copy SGL to SGL
    56  			rr := memsys.NewReader(sglR)
    57  			_, err = io.Copy(sglW, rr)
    58  			tassert.CheckFatal(t, err)
    59  
    60  			// read SGL from destination and compare with the original
    61  			var bufW []byte
    62  			bufW, err = io.ReadAll(memsys.NewReader(sglW))
    63  			tassert.CheckFatal(t, err)
    64  			for j := range objsize {
    65  				if bufW[j] != bufR[j] {
    66  					tlog.Logf("IN : %s\nOUT: %s\n", string(bufR), string(bufW))
    67  					t.Errorf("Step %d failed", i)
    68  					return
    69  				}
    70  			}
    71  			sglR.Free() // removing these two lines fixes the test
    72  			sglW.Free()
    73  		}
    74  	}
    75  	for range workers {
    76  		wg.Add(1)
    77  		go fn()
    78  	}
    79  	wg.Wait()
    80  }