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

     1  package index
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/scottcagno/data-structures/pkg/_reference/_rbtree"
     6  	"github.com/scottcagno/data-structures/pkg/_reference/idx"
     7  	"github.com/scottcagno/data-structures/pkg/treemap"
     8  	"github.com/scottcagno/storage/pkg/_junk/_index/bptree"
     9  	"github.com/scottcagno/storage/pkg/_junk/_index/rbtree"
    10  	"log"
    11  	"runtime"
    12  	"strconv"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  const (
    18  	MILLION              = 1000000
    19  	HALF_MILLION         = MILLION / 2
    20  	ONE_HUNDRED_THOUSAND = MILLION / 10
    21  )
    22  
    23  func clean() {
    24  	log.Printf("\n>>> TAKING THE GARBAGE OUT, JUST A MOMENT PLEASE...\n")
    25  	runtime.GC()
    26  	time.Sleep(5 * time.Second)
    27  }
    28  
    29  func TestMem_BPlusTree1(t *testing.T) {
    30  
    31  	fmt.Println("\n(B+Tree #1) [pkg/_reference/pbtree] TestInsertDeleteAndGet\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    32  
    33  	clean()
    34  
    35  	mem1 := new(runtime.MemStats)
    36  	runtime.ReadMemStats(mem1)
    37  
    38  	t1 := time.Now()
    39  
    40  	tr := bptree.NewTree()
    41  	for i := 0; i < MILLION; i++ {
    42  		key := []byte(strconv.Itoa(i))
    43  		tr.Set(key, key)
    44  	}
    45  
    46  	t2 := time.Now()
    47  	fmt.Printf("Insert time: %.5f sec\n", float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()))
    48  
    49  	count := 0
    50  	for i := 0; i < MILLION+HALF_MILLION; i++ {
    51  		key := []byte(strconv.Itoa(i))
    52  		if v := tr.Get(key); v != nil {
    53  			count++
    54  		}
    55  	}
    56  
    57  	t3 := time.Now()
    58  	fmt.Printf("Search time: %.5f sec with count %d\n", float64(t3.Sub(t2).Nanoseconds())/float64(time.Second.Nanoseconds()), count)
    59  
    60  	log.Printf("get count: %d, tree count: %d\n", count, tr.Len())
    61  
    62  	for i := 1; i < MILLION; i++ {
    63  		key := strconv.Itoa(i)
    64  		tr.Del([]byte(key))
    65  	}
    66  
    67  	t4 := time.Now()
    68  	fmt.Printf("Delete time: %.5f sec\n", float64(t4.Sub(t3).Nanoseconds())/float64(time.Second.Nanoseconds()))
    69  
    70  	mem2 := new(runtime.MemStats)
    71  	runtime.ReadMemStats(mem2)
    72  	if mem2.Alloc <= mem1.Alloc {
    73  		fmt.Printf("Mem allocated: 0 MB\n")
    74  	} else {
    75  		fmt.Printf("Mem allocated: %3.3f MB\n", float64(mem2.Alloc-mem1.Alloc)/(1024*1024))
    76  	}
    77  }
    78  
    79  func TestMem_BPlusTree2(t *testing.T) {
    80  
    81  	fmt.Println("\n(B+Tree #2) [pkg/_reference/idx] TestInsertDeleteAndGet\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    82  
    83  	clean()
    84  
    85  	mem1 := new(runtime.MemStats)
    86  	runtime.ReadMemStats(mem1)
    87  
    88  	t1 := time.Now()
    89  
    90  	tr := idx.NewTree()
    91  	for i := 0; i < MILLION; i++ {
    92  		key := idx.Itob(int64(i))
    93  		tr.Set(key, key)
    94  	}
    95  
    96  	t2 := time.Now()
    97  	fmt.Printf("Insert time: %.5f sec\n", float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()))
    98  
    99  	count := 0
   100  	for i := 0; i < MILLION+HALF_MILLION; i++ {
   101  		key := idx.Itob(int64(i))
   102  		if v := tr.Get(key); v != nil {
   103  			count++
   104  		}
   105  	}
   106  
   107  	t3 := time.Now()
   108  	fmt.Printf("Search time: %.5f sec with count %d\n", float64(t3.Sub(t2).Nanoseconds())/float64(time.Second.Nanoseconds()), count)
   109  
   110  	log.Printf("get count: %d, tree count: %d\n", count, tr.Count())
   111  
   112  	for i := 1; i < MILLION; i++ {
   113  		key := idx.Itob(int64(i))
   114  		tr.Del(key)
   115  	}
   116  
   117  	t4 := time.Now()
   118  	fmt.Printf("Delete time: %.5f sec\n", float64(t4.Sub(t3).Nanoseconds())/float64(time.Second.Nanoseconds()))
   119  
   120  	mem2 := new(runtime.MemStats)
   121  	runtime.ReadMemStats(mem2)
   122  	if mem2.Alloc <= mem1.Alloc {
   123  		fmt.Printf("Mem allocated: 0 MB\n")
   124  	} else {
   125  		fmt.Printf("Mem allocated: %3.3f MB\n", float64(mem2.Alloc-mem1.Alloc)/(1024*1024))
   126  	}
   127  }
   128  
   129  func TestMem_BPlusTree3(t *testing.T) {
   130  
   131  	fmt.Println("\n(B+Tree #3) [pkg/trees] TestInsertDeleteAndGet\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
   132  
   133  	clean()
   134  
   135  	mem1 := new(runtime.MemStats)
   136  	runtime.ReadMemStats(mem1)
   137  
   138  	t1 := time.Now()
   139  
   140  	tr := bptree.NewBPTree()
   141  	for i := 0; i < MILLION; i++ {
   142  		key := idx.Itob(int64(i))
   143  		tr.Set(key, key)
   144  	}
   145  
   146  	t2 := time.Now()
   147  	fmt.Printf("Insert time: %.5f sec\n", float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()))
   148  
   149  	count := 0
   150  	for i := 0; i < MILLION+HALF_MILLION; i++ {
   151  		key := idx.Itob(int64(i))
   152  		if v := tr.Get(key); v != nil {
   153  			count++
   154  		}
   155  	}
   156  
   157  	t3 := time.Now()
   158  	fmt.Printf("Search time: %.5f sec with count %d\n", float64(t3.Sub(t2).Nanoseconds())/float64(time.Second.Nanoseconds()), count)
   159  
   160  	log.Printf("get count: %d, tree count: %d\n", count, tr.Len())
   161  
   162  	for i := 1; i < MILLION; i++ {
   163  		key := idx.Itob(int64(i))
   164  		tr.Del(key)
   165  	}
   166  
   167  	t4 := time.Now()
   168  	fmt.Printf("Delete time: %.5f sec\n", float64(t4.Sub(t3).Nanoseconds())/float64(time.Second.Nanoseconds()))
   169  
   170  	mem2 := new(runtime.MemStats)
   171  	runtime.ReadMemStats(mem2)
   172  	if mem2.Alloc <= mem1.Alloc {
   173  		fmt.Printf("Mem allocated: 0 MB\n")
   174  	} else {
   175  		fmt.Printf("Mem allocated: %3.3f MB\n", float64(mem2.Alloc-mem1.Alloc)/(1024*1024))
   176  	}
   177  }
   178  
   179  func TestMem_RBTree1(t *testing.T) {
   180  
   181  	fmt.Println("\n(RBTree1) [pkg/_reference/_rbtree] TestInsertDeleteAndGet\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
   182  
   183  	clean()
   184  
   185  	mem1 := new(runtime.MemStats)
   186  	runtime.ReadMemStats(mem1)
   187  
   188  	t1 := time.Now()
   189  
   190  	tr := _rbtree.NewTree()
   191  	for i := 0; i < MILLION; i++ {
   192  		key := Arr(idx.Itob(int64(i)))
   193  		tr.Set(key, i)
   194  	}
   195  
   196  	t2 := time.Now()
   197  	fmt.Printf("Insert time: %.5f sec\n", float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()))
   198  
   199  	count := 0
   200  	for i := 0; i < MILLION+HALF_MILLION; i++ {
   201  		key := Arr(idx.Itob(int64(i)))
   202  		if v := tr.Get(key); v != nil && v == i {
   203  			count++
   204  		}
   205  	}
   206  
   207  	t3 := time.Now()
   208  	fmt.Printf("Search time: %.5f sec with count %d\n", float64(t3.Sub(t2).Nanoseconds())/float64(time.Second.Nanoseconds()), count)
   209  
   210  	log.Printf("get count: %d, tree count: %d\n", count, tr.Len())
   211  
   212  	for i := 1; i < MILLION; i++ {
   213  		key := Arr(idx.Itob(int64(i)))
   214  		tr.Del(key)
   215  	}
   216  
   217  	t4 := time.Now()
   218  	fmt.Printf("Delete time: %.5f sec\n", float64(t4.Sub(t3).Nanoseconds())/float64(time.Second.Nanoseconds()))
   219  
   220  	mem2 := new(runtime.MemStats)
   221  	runtime.ReadMemStats(mem2)
   222  	if mem2.Alloc <= mem1.Alloc {
   223  		fmt.Printf("Mem allocated: 0 MB\n")
   224  	} else {
   225  		fmt.Printf("Mem allocated: %3.3f MB\n", float64(mem2.Alloc-mem1.Alloc)/(1024*1024))
   226  	}
   227  }
   228  
   229  func TestMem_RBTree2(t *testing.T) {
   230  
   231  	fmt.Println("\n(RBTree2) [pkg/trees] TestInsertDeleteAndGet\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
   232  
   233  	clean()
   234  
   235  	mem1 := new(runtime.MemStats)
   236  	runtime.ReadMemStats(mem1)
   237  
   238  	t1 := time.Now()
   239  
   240  	tr := rbtree.NewRBTree()
   241  	for i := 0; i < MILLION; i++ {
   242  		tr.Set(idx.Itob(int64(i)), idx.Itob(int64(i)))
   243  	}
   244  
   245  	t2 := time.Now()
   246  	fmt.Printf("Insert time: %.5f sec\n", float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()))
   247  
   248  	count := 0
   249  	for i := 0; i < MILLION+HALF_MILLION; i++ {
   250  		if v := tr.Get(idx.Itob(int64(i))); v != nil {
   251  			count++
   252  		}
   253  	}
   254  
   255  	t3 := time.Now()
   256  	fmt.Printf("Search time: %.5f sec with count %d\n", float64(t3.Sub(t2).Nanoseconds())/float64(time.Second.Nanoseconds()), count)
   257  
   258  	log.Printf("get count: %d, tree count: %d\n", count, tr.Len())
   259  
   260  	for i := 1; i < MILLION; i++ {
   261  		tr.Del(idx.Itob(int64(i)))
   262  	}
   263  
   264  	t4 := time.Now()
   265  	fmt.Printf("Delete time: %.5f sec\n", float64(t4.Sub(t3).Nanoseconds())/float64(time.Second.Nanoseconds()))
   266  
   267  	mem2 := new(runtime.MemStats)
   268  	runtime.ReadMemStats(mem2)
   269  	if mem2.Alloc <= mem1.Alloc {
   270  		fmt.Printf("Mem allocated: 0 MB\n")
   271  	} else {
   272  		fmt.Printf("Mem allocated: %3.3f MB\n", float64(mem2.Alloc-mem1.Alloc)/(1024*1024))
   273  	}
   274  }
   275  
   276  func TestMem_RBTree3(t *testing.T) {
   277  
   278  	fmt.Println("\n(RBTree3) [pkg/treemap] TestInsertDeleteAndGet\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
   279  
   280  	clean()
   281  
   282  	mem1 := new(runtime.MemStats)
   283  	runtime.ReadMemStats(mem1)
   284  
   285  	t1 := time.Now()
   286  
   287  	tr := treemap.NewRBTree()
   288  	for i := 0; i < MILLION; i++ {
   289  		key := string(idx.Itob(int64(i)))
   290  		tr.Put(treemap.Entry{Key: key, Value: key})
   291  	}
   292  
   293  	t2 := time.Now()
   294  	fmt.Printf("Insert time: %.5f sec\n", float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()))
   295  
   296  	count := 0
   297  	for i := 0; i < MILLION+HALF_MILLION; i++ {
   298  		key := string(idx.Itob(int64(i)))
   299  		if v, ok := tr.Get(treemap.Entry{Key: key}); ok && v != nil {
   300  			count++
   301  		}
   302  	}
   303  
   304  	t3 := time.Now()
   305  	fmt.Printf("Search time: %.5f sec with count %d\n", float64(t3.Sub(t2).Nanoseconds())/float64(time.Second.Nanoseconds()), count)
   306  
   307  	log.Printf("get count: %d, tree count: %d\n", count, tr.Len())
   308  
   309  	for i := 1; i < MILLION; i++ {
   310  		key := string(idx.Itob(int64(i)))
   311  		tr.Del(treemap.Entry{Key: key})
   312  	}
   313  
   314  	t4 := time.Now()
   315  	fmt.Printf("Delete time: %.5f sec\n", float64(t4.Sub(t3).Nanoseconds())/float64(time.Second.Nanoseconds()))
   316  
   317  	mem2 := new(runtime.MemStats)
   318  	runtime.ReadMemStats(mem2)
   319  	if mem2.Alloc <= mem1.Alloc {
   320  		fmt.Printf("Mem allocated: 0 MB\n")
   321  	} else {
   322  		fmt.Printf("Mem allocated: %3.3f MB\n", float64(mem2.Alloc-mem1.Alloc)/(1024*1024))
   323  	}
   324  }
   325  
   326  func TestMem_Map1(t *testing.T) {
   327  
   328  	fmt.Println("\n(Map1) TestInsertDeleteAndGetMap [map[string]interface{} 0 hint]\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
   329  
   330  	clean()
   331  
   332  	mem1 := new(runtime.MemStats)
   333  	runtime.ReadMemStats(mem1)
   334  
   335  	t1 := time.Now()
   336  
   337  	tree := make(map[string]interface{})
   338  	for i := 0; i < MILLION; i++ {
   339  		key := strconv.Itoa(i)
   340  		tree[key] = 10 + i
   341  	}
   342  
   343  	t2 := time.Now()
   344  	fmt.Printf("Insert map time: %.5f sec\n", float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()))
   345  
   346  	count := 0
   347  	for i := 0; i < MILLION+HALF_MILLION; i++ {
   348  		key := strconv.Itoa(i)
   349  		_, ok := tree[key]
   350  		if ok {
   351  			count++
   352  		}
   353  	}
   354  
   355  	t3 := time.Now()
   356  	fmt.Printf("Search map time: %.5f sec with count %d\n", float64(t3.Sub(t2).Nanoseconds())/float64(time.Second.Nanoseconds()), count)
   357  
   358  	log.Printf("get count: %d, tree count: %d\n", count, len(tree))
   359  
   360  	for i := 1; i < MILLION; i++ {
   361  		key := strconv.Itoa(i)
   362  		delete(tree, key)
   363  	}
   364  
   365  	t4 := time.Now()
   366  	fmt.Printf("Delete map time: %.5f sec\n", float64(t4.Sub(t3).Nanoseconds())/float64(time.Second.Nanoseconds()))
   367  
   368  	mem2 := new(runtime.MemStats)
   369  	runtime.ReadMemStats(mem2)
   370  	if mem2.Alloc <= mem1.Alloc {
   371  		fmt.Printf("Mem map allocated: 0 MB\n")
   372  	} else {
   373  		fmt.Printf("Mem map allocated: %3.3f MB\n", float64(mem2.Alloc-mem1.Alloc)/(1024*1024))
   374  	}
   375  }
   376  
   377  func TestMem_Map2(t *testing.T) {
   378  
   379  	fmt.Println("\n(Map2) TestInsertDeleteAndGetMap [map[string]interface{} HALF_MILLION hint]\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
   380  
   381  	clean()
   382  
   383  	mem1 := new(runtime.MemStats)
   384  	runtime.ReadMemStats(mem1)
   385  
   386  	t1 := time.Now()
   387  
   388  	tree := make(map[string]interface{}, HALF_MILLION)
   389  	for i := 0; i < MILLION; i++ {
   390  		key := strconv.Itoa(i)
   391  		tree[key] = 10 + i
   392  	}
   393  
   394  	t2 := time.Now()
   395  	fmt.Printf("Insert map time: %.5f sec\n", float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()))
   396  
   397  	count := 0
   398  	for i := 0; i < MILLION+HALF_MILLION; i++ {
   399  		key := strconv.Itoa(i)
   400  		_, ok := tree[key]
   401  		if ok {
   402  			count++
   403  		}
   404  	}
   405  
   406  	t3 := time.Now()
   407  	fmt.Printf("Search map time: %.5f sec with count %d\n", float64(t3.Sub(t2).Nanoseconds())/float64(time.Second.Nanoseconds()), count)
   408  
   409  	log.Printf("get count: %d, tree count: %d\n", count, len(tree))
   410  
   411  	for i := 1; i < MILLION; i++ {
   412  		key := strconv.Itoa(i)
   413  		delete(tree, key)
   414  	}
   415  
   416  	t4 := time.Now()
   417  	fmt.Printf("Delete map time: %.5f sec\n", float64(t4.Sub(t3).Nanoseconds())/float64(time.Second.Nanoseconds()))
   418  
   419  	mem2 := new(runtime.MemStats)
   420  	runtime.ReadMemStats(mem2)
   421  	if mem2.Alloc <= mem1.Alloc {
   422  		fmt.Printf("Mem map allocated: 0 MB\n")
   423  	} else {
   424  		fmt.Printf("Mem map allocated: %3.3f MB\n", float64(mem2.Alloc-mem1.Alloc)/(1024*1024))
   425  	}
   426  }