github.com/xgzlucario/GigaCache@v0.0.0-20240508025442-54204e9c8a6b/bench_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  const N = 100 * 10000
     9  
    10  func getStdmap(num int) map[string][]byte {
    11  	m := map[string][]byte{}
    12  	for i := 0; i < num; i++ {
    13  		k, v := genKV(i)
    14  		m[k] = v
    15  	}
    16  	return m
    17  }
    18  
    19  func getCache(num int, options ...Options) *GigaCache {
    20  	opt := DefaultOptions
    21  	if len(options) > 0 {
    22  		opt = options[0]
    23  	}
    24  	m := New(opt)
    25  	for i := 0; i < num; i++ {
    26  		k, v := genKV(i)
    27  		m.Set(k, v)
    28  	}
    29  	return m
    30  }
    31  
    32  func BenchmarkSet(b *testing.B) {
    33  	b.Run("stdmap", func(b *testing.B) {
    34  		m := map[string][]byte{}
    35  		for i := 0; i < b.N; i++ {
    36  			k, v := genKV(i)
    37  			m[k] = v
    38  		}
    39  	})
    40  	b.Run("cache", func(b *testing.B) {
    41  		m := New(DefaultOptions)
    42  		for i := 0; i < b.N; i++ {
    43  			k, v := genKV(i)
    44  			m.Set(k, v)
    45  		}
    46  	})
    47  	b.Run("cache/disableEvict", func(b *testing.B) {
    48  		options := DefaultOptions
    49  		options.DisableEvict = true
    50  		m := New(options)
    51  		for i := 0; i < b.N; i++ {
    52  			k, v := genKV(i)
    53  			m.Set(k, v)
    54  		}
    55  	})
    56  }
    57  
    58  func BenchmarkGet(b *testing.B) {
    59  	b.Run("stdmap", func(b *testing.B) {
    60  		m := getStdmap(N)
    61  		b.ResetTimer()
    62  		for i := 0; i < b.N; i++ {
    63  			k, _ := genKV(i)
    64  			_ = m[k]
    65  		}
    66  	})
    67  	b.Run("cache", func(b *testing.B) {
    68  		m := getCache(N)
    69  		b.ResetTimer()
    70  		for i := 0; i < b.N; i++ {
    71  			k, _ := genKV(i)
    72  			m.Get(k)
    73  		}
    74  	})
    75  }
    76  
    77  func BenchmarkScan(b *testing.B) {
    78  	b.Run("stdmap", func(b *testing.B) {
    79  		m := getStdmap(N)
    80  		b.ResetTimer()
    81  		for i := 0; i < b.N; i++ {
    82  			for k, v := range m {
    83  				_, _ = k, v
    84  			}
    85  		}
    86  	})
    87  	b.Run("cache", func(b *testing.B) {
    88  		m := getCache(N)
    89  		b.ResetTimer()
    90  		for i := 0; i < b.N; i++ {
    91  			m.Scan(func(s, b []byte, i int64) bool {
    92  				return true
    93  			})
    94  		}
    95  	})
    96  }
    97  
    98  func BenchmarkRemove(b *testing.B) {
    99  	b.Run("stdmap", func(b *testing.B) {
   100  		m := getStdmap(N)
   101  		b.ResetTimer()
   102  		for i := 0; i < b.N; i++ {
   103  			k, _ := genKV(i)
   104  			delete(m, k)
   105  		}
   106  	})
   107  	b.Run("cache", func(b *testing.B) {
   108  		m := getCache(N)
   109  		b.ResetTimer()
   110  		for i := 0; i < b.N; i++ {
   111  			k, _ := genKV(i)
   112  			m.Remove(k)
   113  		}
   114  	})
   115  }
   116  
   117  func BenchmarkIdx(b *testing.B) {
   118  	b.Run("newIdx", func(b *testing.B) {
   119  		idx := newIdx(1024, time.Now().Unix())
   120  		for i := 0; i < b.N; i++ {
   121  			newIdx(idx.start(), idx.TTL())
   122  		}
   123  	})
   124  	b.Run("newIdxx", func(b *testing.B) {
   125  		idx := newIdx(1024, time.Now().Unix())
   126  		for i := 0; i < b.N; i++ {
   127  			newIdxx(idx.start(), idx)
   128  		}
   129  	})
   130  }