github.com/scottcagno/storage@v1.8.0/pkg/_junk/_index/index_test.go (about)

     1  package index
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  )
     7  
     8  type entry1 struct {
     9  	key   string
    10  	value []byte
    11  }
    12  
    13  type ds1 struct {
    14  	data map[string]entry1
    15  }
    16  
    17  func NewDS1() *ds1 {
    18  	return &ds1{
    19  		data: make(map[string]entry1, 0),
    20  	}
    21  }
    22  
    23  func (d *ds1) Put(e1 entry1) {
    24  	d.data[e1.key] = e1
    25  }
    26  
    27  type entry2 interface {
    28  	Key() string
    29  }
    30  
    31  type rec struct {
    32  	key   string
    33  	value []byte
    34  }
    35  
    36  func (r rec) Key() string {
    37  	return r.key
    38  }
    39  
    40  type ds2 struct {
    41  	data map[string]entry2
    42  }
    43  
    44  func NewDS2() *ds2 {
    45  	return &ds2{
    46  		data: make(map[string]entry2, 0),
    47  	}
    48  }
    49  
    50  func (d *ds2) Put(e2 entry2) {
    51  	d.data[e2.Key()] = e2
    52  }
    53  
    54  func randByteKey(length int) []byte {
    55  	key := make([]byte, length)
    56  	rand.Read(key)
    57  	return key
    58  }
    59  
    60  func randStringKey(length int) string {
    61  	return string(randByteKey(length))
    62  }
    63  
    64  func BenchmarkTestDS1(b *testing.B) {
    65  	keys := make([][]byte, b.N)
    66  	for i := 0; i < b.N; i++ {
    67  		keys[i] = randByteKey(4)
    68  	}
    69  	b.ResetTimer()
    70  	b.ReportAllocs()
    71  	ds := NewDS1()
    72  	for i := 0; i < b.N; i++ {
    73  		e1 := entry1{string(keys[i]), keys[i]}
    74  		ds.Put(e1)
    75  	}
    76  	ds = nil
    77  }
    78  
    79  func BenchmarkTestDS2(b *testing.B) {
    80  	keys := make([][]byte, b.N)
    81  	for i := 0; i < b.N; i++ {
    82  		keys[i] = randByteKey(4)
    83  	}
    84  	b.ResetTimer()
    85  	b.ReportAllocs()
    86  	ds := NewDS2()
    87  	for i := 0; i < b.N; i++ {
    88  		r := rec{string(keys[i]), keys[i]}
    89  		ds.Put(r)
    90  	}
    91  	ds = nil
    92  }