structure/hashmap/hashmap_test.go

135 lines
2.1 KiB
Go
Raw Normal View History

2019-04-16 19:22:07 +00:00
package hashmap
2019-04-18 01:39:44 +00:00
import (
2019-04-22 19:05:25 +00:00
"bytes"
"encoding/gob"
2019-04-18 01:39:44 +00:00
"fmt"
2019-04-22 19:05:25 +00:00
"io/ioutil"
"log"
2019-04-18 01:39:44 +00:00
"runtime"
"testing"
"474420502.top/eson/structure/compare"
)
2019-04-16 19:22:07 +00:00
2019-04-22 19:05:25 +00:00
func loadTestData() []int {
log.SetFlags(log.Lshortfile)
data, err := ioutil.ReadFile("../l.log")
if err != nil {
log.Println(err)
}
var l []int
decoder := gob.NewDecoder(bytes.NewReader(data))
decoder.Decode(&l)
return l
}
2019-04-16 19:22:07 +00:00
func TestCount(t *testing.T) {
2019-04-18 01:39:44 +00:00
hm := New(HashInt, compare.Int)
2019-04-22 19:05:25 +00:00
for i := 0; i < 100000; i++ {
2019-04-18 01:39:44 +00:00
hm.Put(i, i)
}
2019-04-22 06:06:19 +00:00
2019-04-28 10:32:01 +00:00
// t.Error(hm.Get(4))
2019-04-18 01:39:44 +00:00
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
2019-04-22 19:05:25 +00:00
var executeCount = 5
var compareSize = 100000
2019-04-21 20:46:07 +00:00
func BenchmarkPut(b *testing.B) {
2019-04-28 10:32:01 +00:00
b.StopTimer()
2019-04-22 19:05:25 +00:00
2019-04-28 10:32:01 +00:00
l := loadTestData()
2019-04-18 01:39:44 +00:00
hm := New(HashInt, compare.Int)
2019-04-22 19:05:25 +00:00
b.N = len(l) * executeCount
2019-04-28 10:32:01 +00:00
for i := 0; i < len(l); i++ {
v := l[i]
hm.Put(v, v)
}
b.StartTimer()
2019-04-22 19:05:25 +00:00
for c := 0; c < executeCount; c++ {
for i := 0; i < len(l); i++ {
v := l[i]
hm.Put(v, v)
}
2019-04-18 01:39:44 +00:00
}
2019-04-21 20:46:07 +00:00
//b.Log(len(hm.table), hm.size)
//PrintMemUsage()
2019-04-18 01:39:44 +00:00
}
2019-04-22 19:05:25 +00:00
func BenchmarkGoPut(b *testing.B) {
l := loadTestData()
hm := make(map[int]int)
b.N = len(l) * executeCount
for c := 0; c < executeCount; c++ {
for i := 0; i < len(l); i++ {
v := l[i]
hm[v] = v
}
}
//b.Log(len(m))
//PrintMemUsage()
}
2019-04-21 20:46:07 +00:00
func BenchmarkGet(b *testing.B) {
2019-04-28 10:32:01 +00:00
2019-04-21 20:46:07 +00:00
b.StopTimer()
2019-04-28 10:32:01 +00:00
l := loadTestData()
2019-04-21 20:46:07 +00:00
hm := New(HashInt, compare.Int)
2019-04-28 10:32:01 +00:00
b.N = len(l) * executeCount
for i := 0; i < len(l); i++ {
v := l[i]
hm.Put(v, v)
2019-04-21 20:46:07 +00:00
}
b.StartTimer()
2019-04-28 10:32:01 +00:00
2019-04-21 20:46:07 +00:00
for i := 0; i < b.N; i++ {
hm.Get(i)
}
//b.Log(len(hm.table), hm.size)
//PrintMemUsage()
}
func BenchmarkGoGet(b *testing.B) {
b.StopTimer()
2019-04-28 10:32:01 +00:00
l := loadTestData()
hm := make(map[int]int)
b.N = len(l) * executeCount
for i := 0; i < len(l); i++ {
v := l[i]
hm[v] = v
2019-04-21 20:46:07 +00:00
}
2019-04-28 10:32:01 +00:00
2019-04-21 20:46:07 +00:00
b.StartTimer()
for i := 0; i < b.N; i++ {
2019-04-28 10:32:01 +00:00
if _, ok := hm[i]; !ok {
2019-04-21 20:46:07 +00:00
}
}
//b.Log(len(m))
//PrintMemUsage()
2019-04-16 19:22:07 +00:00
}