github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/internal/caching/caching_test.go (about)

     1  /**
     2   * Copyright 2023 CloudWeGo Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package caching
    18  
    19  import (
    20  	"math/rand"
    21  	"testing"
    22  	"time"
    23  	"unsafe"
    24  )
    25  
    26  var (
    27  	charTable       = []byte(`._-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`)
    28  	charTableLength = len(charTable)
    29  )
    30  
    31  func getSample(minLength, maxLength, count int) (ret []string) {
    32  	rand.Seed(time.Now().Unix())
    33  	for i := 0; i < count; i++ {
    34  		l := rand.Intn(maxLength + 1)
    35  		if l < minLength {
    36  			l = minLength
    37  		}
    38  		buf := make([]byte, l)
    39  		for j := 0; j < l; j++ {
    40  			buf[j] = charTable[rand.Intn(charTableLength)]
    41  		}
    42  		ret = append(ret, string(buf))
    43  	}
    44  	return
    45  }
    46  
    47  var (
    48  	maxLength = 20
    49  	minLength = 1
    50  	count     = 20
    51  	samples   = getSample(minLength, maxLength, count)
    52  )
    53  
    54  func TestStdMapGet(t *testing.T) {
    55  	var i int
    56  	var p = unsafe.Pointer(&i)
    57  	m := map[string]unsafe.Pointer{}
    58  	for _, v := range samples {
    59  		m[v] = p
    60  	}
    61  	for _, v := range samples {
    62  		if m[v] != p {
    63  			t.Fatal(v)
    64  		}
    65  	}
    66  }
    67  
    68  func TestHashMapGet(t *testing.T) {
    69  	var i int
    70  	var p = unsafe.Pointer(&i)
    71  	m := NewHashMap(count, 4)
    72  	for _, v := range samples {
    73  		m.Set(v, p)
    74  	}
    75  	m.Set("", p)
    76  	m.Set("", p)
    77  	if m.Size() != len(samples)+2 {
    78  		t.Fatal(m.Size())
    79  	}
    80  	for _, v := range samples {
    81  		if m.Get(v) != p {
    82  			t.Fatal(v)
    83  		}
    84  	}
    85  }
    86  
    87  func TestTrieTreeGet(t *testing.T) {
    88  	var i int
    89  	var p = unsafe.Pointer(&i)
    90  	m := TrieTree{
    91  		Positions: []int{10},
    92  		TrieNode: TrieNode{
    93  			Leaves: &[]Pair{},
    94  		},
    95  	}
    96  	for _, v := range samples {
    97  		m.Set(v, p)
    98  	}
    99  	m.Set("", p)
   100  	m.Set("", p)
   101  	// if m.Size() != len(samples)+1 {
   102  	// 	t.Fatal(m.Size())
   103  	// }
   104  	for _, v := range samples {
   105  		if m.Get(v) != p {
   106  			t.Fatal(v)
   107  		}
   108  	}
   109  }
   110  
   111  func BenchmarkStdMapGet(b *testing.B) {
   112  	var i int
   113  	var p = unsafe.Pointer(&i)
   114  	m := map[string]unsafe.Pointer{}
   115  	for _, v := range samples {
   116  		m[v] = p
   117  	}
   118  	b.ResetTimer()
   119  	for i := 0; i < b.N; i++ {
   120  		for _, v := range samples {
   121  			_ = m[v]
   122  		}
   123  	}
   124  }
   125  
   126  func BenchmarkHashMapGet(b *testing.B) {
   127  	var i int
   128  	var p = unsafe.Pointer(&i)
   129  	m := NewHashMap(count, 2)
   130  	for _, v := range samples {
   131  		m.Set(v, p)
   132  	}
   133  
   134  	b.ResetTimer()
   135  	for i := 0; i < b.N; i++ {
   136  		for _, v := range samples {
   137  			_ = m.Get(v)
   138  		}
   139  	}
   140  }
   141  
   142  func BenchmarkTrieTreeGet(b *testing.B) {
   143  	var i int
   144  	var p = unsafe.Pointer(&i)
   145  	m := TrieTree{
   146  		Positions: []int{10},
   147  	}
   148  	for _, v := range samples {
   149  		m.Set(v, p)
   150  	}
   151  
   152  	b.ResetTimer()
   153  	for i := 0; i < b.N; i++ {
   154  		for _, v := range samples {
   155  			_ = m.Get(v)
   156  		}
   157  	}
   158  }
   159  
   160  func BenchmarkStdMapGet_Parallel(b *testing.B) {
   161  	var i int
   162  	var p = unsafe.Pointer(&i)
   163  	m := map[string]unsafe.Pointer{}
   164  	for _, v := range samples {
   165  		m[v] = p
   166  	}
   167  	b.ResetTimer()
   168  	b.RunParallel(func(pb *testing.PB) {
   169  		for pb.Next() {
   170  			for _, v := range samples {
   171  				_ = m[v]
   172  			}
   173  		}
   174  	})
   175  }
   176  
   177  func BenchmarkHashMapGet_Parallel(b *testing.B) {
   178  	var i int
   179  	var p = unsafe.Pointer(&i)
   180  	m := NewHashMap(count, 2)
   181  	for _, v := range samples {
   182  		m.Set(v, p)
   183  	}
   184  
   185  	b.ResetTimer()
   186  	b.RunParallel(func(pb *testing.PB) {
   187  		for pb.Next() {
   188  			for _, v := range samples {
   189  				_ = m.Get(v)
   190  			}
   191  		}
   192  	})
   193  }
   194  
   195  func BenchmarkTrieTreeGet_Parallel(b *testing.B) {
   196  	var i int
   197  	var p = unsafe.Pointer(&i)
   198  	m := TrieTree{
   199  		Positions: []int{10},
   200  	}
   201  	for _, v := range samples {
   202  		m.Set(v, p)
   203  	}
   204  
   205  	b.ResetTimer()
   206  	b.RunParallel(func(pb *testing.PB) {
   207  		for pb.Next() {
   208  			for _, v := range samples {
   209  				_ = m.Get(v)
   210  			}
   211  		}
   212  	})
   213  }