github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/bench/tools/aisloader/test/objnamegetter_test.go (about)

     1  // Package test provides tests of aisloader package
     2  /*
     3   * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package test_test
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/NVIDIA/aistore/bench/tools/aisloader/namegetter"
    15  	"github.com/NVIDIA/aistore/cmn/cos"
    16  	"github.com/NVIDIA/aistore/tools/tassert"
    17  )
    18  
    19  // Running these benchmarks with different objNamesSize returns different results
    20  // Almost for every number of objects, permutation strategies outcompete random
    21  
    22  const (
    23  	objNamesSize    = 200000
    24  	smallSampleSize = 1000
    25  )
    26  
    27  var objNames []string
    28  
    29  func TestMain(m *testing.M) {
    30  	flag.Parse()
    31  	if testing.Short() {
    32  		fmt.Println("skipping bench/tools/aisloader/namegetter in short mode")
    33  		os.Exit(0)
    34  	}
    35  	objNames = make([]string, objNamesSize)
    36  	for i := range objNamesSize {
    37  		objNames[i] = fmt.Sprintf("test-%d", i)
    38  	}
    39  	m.Run()
    40  }
    41  
    42  func BenchmarkRandomUniqueNameGetter(b *testing.B) {
    43  	ng := &namegetter.RandomUniqueNameGetter{}
    44  	ng.Init(objNames, cos.NowRand())
    45  	b.ResetTimer()
    46  	for range b.N {
    47  		ng.ObjName()
    48  	}
    49  }
    50  
    51  func BenchmarkRandomUniqueIterNameGetter(b *testing.B) {
    52  	ng := &namegetter.RandomUniqueIterNameGetter{}
    53  	ng.Init(objNames, cos.NowRand())
    54  	b.ResetTimer()
    55  	for range b.N {
    56  		ng.ObjName()
    57  	}
    58  }
    59  
    60  func BenchmarkPermutationUniqueNameGetter(b *testing.B) {
    61  	ng := &namegetter.PermutationUniqueNameGetter{}
    62  	ng.Init(objNames, cos.NowRand())
    63  	b.ResetTimer()
    64  	for range b.N {
    65  		ng.ObjName()
    66  	}
    67  }
    68  
    69  func BenchmarkPermutationImprovedUniqueNameGetter(b *testing.B) {
    70  	ng := &namegetter.PermutationUniqueImprovedNameGetter{}
    71  	ng.Init(objNames, cos.NowRand())
    72  	b.ResetTimer()
    73  	for range b.N {
    74  		ng.ObjName()
    75  	}
    76  }
    77  
    78  func TestRandomUniqueNameGetter(t *testing.T) {
    79  	ng := &namegetter.RandomUniqueNameGetter{}
    80  
    81  	checkGetsAllObjNames(t, ng, "RandomUniqueNameGetter")
    82  	checkSmallSampleRandomness(t, ng, "RandomUniqueNameGetter")
    83  }
    84  
    85  func TestRandomUniqueIterNameGetter(t *testing.T) {
    86  	ng := &namegetter.RandomUniqueIterNameGetter{}
    87  
    88  	checkGetsAllObjNames(t, ng, "RandomUniqueIterNameGetter")
    89  	checkSmallSampleRandomness(t, ng, "RandomUniqueIterNameGetter")
    90  }
    91  
    92  func TestPermutationUniqueNameGetter(t *testing.T) {
    93  	ng := &namegetter.PermutationUniqueNameGetter{}
    94  
    95  	checkGetsAllObjNames(t, ng, "PermutationUniqueNameGetter")
    96  	checkSmallSampleRandomness(t, ng, "PermutationUniqueNameGetter")
    97  }
    98  
    99  func TestPermutationUniqueImprovedNameGetter(t *testing.T) {
   100  	ng := &namegetter.PermutationUniqueImprovedNameGetter{}
   101  
   102  	checkGetsAllObjNames(t, ng, "PermutationUniqueImprovedNameGetter")
   103  	checkSmallSampleRandomness(t, ng, "PermutationUniqueImprovedNameGetter")
   104  }
   105  
   106  func checkGetsAllObjNames(t *testing.T, getter namegetter.ObjectNameGetter, name string) {
   107  	getter.Init(objNames, cos.NowRand())
   108  	m := make(map[string]struct{})
   109  
   110  	// Should visit every objectName once
   111  	for range objNamesSize {
   112  		m[getter.ObjName()] = struct{}{}
   113  	}
   114  
   115  	tassert.Fatalf(t, len(m) == objNamesSize, "%s has not visited every element; got %d, expected %d", name, len(m), objNamesSize)
   116  
   117  	// Check that starting operation for the beginning still works as expected
   118  	m = make(map[string]struct{})
   119  	for range objNamesSize {
   120  		m[getter.ObjName()] = struct{}{}
   121  	}
   122  	tassert.Fatalf(t, len(m) == objNamesSize, "%s has not visited every element for second time; got %d, expected %d", name, len(m), objNamesSize)
   123  }
   124  
   125  func checkSmallSampleRandomness(t *testing.T, getter namegetter.ObjectNameGetter, name string) {
   126  	s1 := make([]string, smallSampleSize)
   127  	s2 := make([]string, smallSampleSize)
   128  
   129  	rnd := cos.NowRand()
   130  
   131  	getter.Init(objNames, rnd)
   132  	for i := range smallSampleSize {
   133  		s1[i] = getter.ObjName()
   134  	}
   135  	getter.Init(objNames, rnd)
   136  	for i := range smallSampleSize {
   137  		s2[i] = getter.ObjName()
   138  	}
   139  
   140  	tassert.Fatalf(t, !reflect.DeepEqual(s1, s2), name+" is not random!")
   141  }