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

     1  // Package memsys_test contains the corresponding micro-benchmarks.
     2  /*
     3   * Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package memsys_test
     6  
     7  import (
     8  	rdebug "runtime/debug"
     9  	"testing"
    10  
    11  	"github.com/NVIDIA/aistore/cmn/cos"
    12  	"github.com/NVIDIA/aistore/memsys"
    13  	"github.com/NVIDIA/aistore/tools/cryptorand"
    14  )
    15  
    16  // 1. Run with all defaults:
    17  // $ go test -bench=. -benchmem
    18  //
    19  // 2. Run each bench for 10s:
    20  // $ go test -bench=. -benchtime=10s -benchmem
    21  
    22  func BenchmarkWRF(b *testing.B) {
    23  	tests := []struct{ payloadSz int64 }{
    24  		{cos.MiB * 4},
    25  		{cos.MiB},
    26  		{cos.KiB * 128},
    27  		{cos.KiB * 32},
    28  		{cos.KiB * 4},
    29  		{cos.KiB * 2},
    30  		{cos.KiB},
    31  		{128},
    32  	}
    33  	for _, test := range tests {
    34  		name := cos.ToSizeIEC(test.payloadSz, 0)
    35  		b.Run(name, func(b *testing.B) { wrf(b, test.payloadSz) })
    36  	}
    37  }
    38  
    39  func wrf(b *testing.B, payloadSz int64) {
    40  	// 1. init default MMSAs
    41  	gmm := memsys.PageMM()
    42  
    43  	// 2. equalize initial conditions
    44  	rdebug.FreeOSMemory()
    45  	buf := make([]byte, cos.KiB*128)
    46  	n, _ := cryptorand.Read(buf)
    47  	cos.Assert(n == cos.KiB*128)
    48  
    49  	// 3. select MMSA & slab for the specified payload size
    50  	mm, slab := gmm.SelectMemAndSlab(payloadSz)
    51  	slabSize := slab.Size()
    52  
    53  	// 4. run in parallel
    54  	b.SetBytes(slabSize)
    55  	b.ResetTimer()
    56  	b.RunParallel(func(pb *testing.PB) {
    57  		for pb.Next() {
    58  			sgl := mm.NewSGL(payloadSz, slabSize)
    59  			for sz := int64(0); sz < payloadSz; sz += slabSize {
    60  				n, _ := sgl.Write(buf[:slabSize])
    61  				cos.Assert(n == int(slabSize))
    62  			}
    63  			for sz := int64(0); sz < payloadSz; sz += slabSize {
    64  				n, _ := sgl.Read(buf[:slabSize])
    65  				cos.Assert(n == int(slabSize))
    66  			}
    67  			sgl.Free()
    68  		}
    69  	})
    70  }