github.com/junjiefly/jmap@v0.0.0-20230114031449-c8c3949af2f5/jmap_test.go (about)

     1  package jmap
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  )
     8  
     9  var jMap *JMap
    10  
    11  var needleCount = 10000000
    12  
    13  var mmap map[uint64]int
    14  
    15  func TestMain(m *testing.M) {
    16  	jMap = NewJMap(1024)
    17  	mmap = make(map[uint64]int,1024)
    18  	for i := 0; i < needleCount; i++ {
    19  		jMap.Set(uint64(i), i)
    20  		mmap[uint64(i)] = i
    21  	}
    22  	fmt.Println("j-map ready! len:", jMap.count)
    23  	fmt.Println("goMap ready! len:", len(mmap))
    24  	m.Run()
    25  }
    26  
    27  func BenchmarkJMapSet(b *testing.B) {
    28  	k := rand.Intn(needleCount)
    29  	jMap.Set(uint64(k), 0)
    30  }
    31  
    32  func BenchmarkJMapGet(b *testing.B) {
    33  	k := rand.Intn(needleCount)
    34  	_, _ = jMap.Get(uint64(k))
    35  }
    36  
    37  func BenchmarkJMapDelete(b *testing.B) {
    38  	k := rand.Intn(needleCount)
    39  	_ = jMap.Delete(uint64(k))
    40  }
    41  
    42  func BenchmarkMapSet(b *testing.B) {
    43  	k := rand.Intn(needleCount)
    44  	mmap[uint64(k)] = 0
    45  }
    46  
    47  func BenchmarkMapGet(b *testing.B) {
    48  	k := rand.Intn(needleCount)
    49  	_, _ = mmap[uint64(k)]
    50  }
    51  
    52  func BenchmarkMapDelete(b *testing.B) {
    53  	k := rand.Intn(needleCount)
    54  	delete(mmap, uint64(k))
    55  }