github.com/coyove/sdss@v0.0.0-20231129015646-c2ec58cca6a2/contrib/plru/lru_test.go (about)

     1  package plru
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strconv"
     7  	"testing"
     8  	"time"
     9  	_ "unsafe"
    10  )
    11  
    12  func init() {
    13  	rand.Seed(time.Now().Unix())
    14  }
    15  
    16  func TestPLRU(t *testing.T) {
    17  	N := 10000
    18  	c := New[int, int](N, Hash.Int, nil)
    19  	for i := 0; i < N*2; i++ {
    20  		c.Add(i, i)
    21  	}
    22  	x := 0
    23  	c.Range(func(k, v int) bool {
    24  		if k < N {
    25  			x++
    26  		}
    27  		return true
    28  	})
    29  	fmt.Println(x, N)
    30  }
    31  
    32  func TestRHMapFixedFull(t *testing.T) {
    33  	const N = 1000
    34  	m := NewMap[int, int](N*0.5, Hash.Int)
    35  	m.Fixed = true
    36  
    37  	start := time.Now()
    38  	for i := 0; i < N; i++ {
    39  		m.Set(i, i+1)
    40  	}
    41  	fmt.Println(time.Since(start))
    42  	// fmt.Println(m)
    43  }
    44  
    45  func TestRHMapInt64a(t *testing.T) {
    46  	const N = 1e2
    47  	m := NewMap[int64, int](N, Hash.Int64a)
    48  
    49  	for i := 0; i < N*2; i++ {
    50  		if rand.Intn(10) > 0 {
    51  			m.Set(rand.Int63(), i+1)
    52  		} else {
    53  			m.Set(int64(i), i+1)
    54  		}
    55  	}
    56  
    57  	fmt.Println(m.GoString())
    58  }
    59  
    60  func TestRHMap(t *testing.T) {
    61  	const N = 1e6
    62  	m2 := map[string]int{}
    63  	m := NewMap[string, int](N*0.5, Hash.Str)
    64  
    65  	for i := 0; i < N; i++ {
    66  		is := strconv.Itoa(i)
    67  		m2[is] = i + 1
    68  		m.Set(is, i+1)
    69  	}
    70  
    71  	for i := 0; i < N/10; i++ {
    72  		for k := range m2 {
    73  			delete(m2, k)
    74  			m.Delete(k)
    75  			break
    76  		}
    77  	}
    78  
    79  	for k, v := range m2 {
    80  		v2, _ := m.Find(k)
    81  		if v2 != v {
    82  			t.Fatal(k)
    83  		}
    84  	}
    85  
    86  	fmt.Println(m.density())
    87  	// fmt.Println(m)
    88  }
    89  
    90  const BN = 1e6
    91  
    92  func BenchmarkGoMapRandomGet(b *testing.B) {
    93  	b.StopTimer()
    94  	m2 := map[int]int{}
    95  
    96  	for i := 0; i < BN; i++ {
    97  		m2[i] = i + 1
    98  	}
    99  	b.StartTimer()
   100  
   101  	for i := 0; i < b.N; i++ {
   102  		x := rand.Intn(BN * 2)
   103  		if m2[x] == -1 {
   104  			b.Fatal(i)
   105  		}
   106  	}
   107  }
   108  
   109  func BenchmarkRHMapRandomGet(b *testing.B) {
   110  	b.StopTimer()
   111  	m := NewMap[int, int](BN, Hash.Int)
   112  
   113  	for i := 0; i < BN; i++ {
   114  		m.Set(i, i+1)
   115  	}
   116  	b.StartTimer()
   117  
   118  	for i := 0; i < b.N; i++ {
   119  		x := rand.Intn(BN * 2)
   120  		if v, _ := m.Find(x); v == -1 {
   121  			b.Fatal(i)
   122  		}
   123  	}
   124  }
   125  
   126  func BenchmarkRHMapInt64aRandomGet(b *testing.B) {
   127  	b.StopTimer()
   128  	m := NewMap[int64, int](BN, Hash.Int64a)
   129  
   130  	for i := 0; i < BN; i++ {
   131  		m.Set(int64(i), i+1)
   132  	}
   133  	b.StartTimer()
   134  
   135  	for i := 0; i < b.N; i++ {
   136  		x := rand.Int63n(BN * 2)
   137  		if v, _ := m.Find(x); v == -1 {
   138  			b.Fatal(i)
   139  		}
   140  	}
   141  }
   142  
   143  func BenchmarkGoMapAddPrealloc(b *testing.B) {
   144  	for i := 0; i < b.N; i++ {
   145  		m2 := make(map[int]int, BN/10)
   146  		for i := 0; i < BN/10; i++ {
   147  			m2[i] = i + 1
   148  		}
   149  	}
   150  }
   151  
   152  func BenchmarkRHMapAddPrealloc(b *testing.B) {
   153  	for i := 0; i < b.N; i++ {
   154  		m := NewMap[int, int](BN/10, Hash.Int)
   155  		for i := 0; i < BN/10; i++ {
   156  			m.Set(i, i+1)
   157  		}
   158  	}
   159  }
   160  
   161  func BenchmarkGoMapAdd(b *testing.B) {
   162  	for i := 0; i < b.N; i++ {
   163  		m2 := map[int]int{}
   164  		for i := 0; i < BN/10; i++ {
   165  			m2[i] = i + 1
   166  		}
   167  	}
   168  }
   169  
   170  func BenchmarkRHMapAdd(b *testing.B) {
   171  	for i := 0; i < b.N; i++ {
   172  		m := NewMap[int, int](8, Hash.Int)
   173  		for i := 0; i < BN/10; i++ {
   174  			m.Set(i, i+1)
   175  		}
   176  	}
   177  }
   178  
   179  func BenchmarkRHMapFirstNext(b *testing.B) {
   180  	b.StopTimer()
   181  	m := NewMap[int, int](8, Hash.Int)
   182  	for i := 0; i < 100; i++ {
   183  		m.Set(i, i+1)
   184  	}
   185  	b.StartTimer()
   186  	for i := 0; i < b.N; i++ {
   187  		c := 0
   188  		for iter := m.First(); iter != nil; iter = m.Next(iter) {
   189  			c++
   190  		}
   191  		if c != m.Len() {
   192  			b.Fail()
   193  		}
   194  	}
   195  }
   196  
   197  func BenchmarkRHMapForeach(b *testing.B) {
   198  	b.StopTimer()
   199  	m := NewMap[int, int](8, Hash.Int)
   200  	for i := 0; i < 100; i++ {
   201  		m.Set(i, i+1)
   202  	}
   203  	b.StartTimer()
   204  	for i := 0; i < b.N; i++ {
   205  		c := 0
   206  		m.Foreach(func(k int, v *int) bool {
   207  			c++
   208  			return true
   209  		})
   210  		if c != m.Len() {
   211  			b.Fail()
   212  		}
   213  	}
   214  }
   215  
   216  func BenchmarkGoMapForeach(b *testing.B) {
   217  	b.StopTimer()
   218  	m := map[int]int{}
   219  	for i := 0; i < 100; i++ {
   220  		m[i] = i + 1
   221  	}
   222  	b.StartTimer()
   223  	for i := 0; i < b.N; i++ {
   224  		c := 0
   225  		for range m {
   226  			c++
   227  		}
   228  		if c != len(m) {
   229  			b.Fail()
   230  		}
   231  	}
   232  }