github.com/moontrade/nogc@v0.1.7/collections/tree/cmd/main.go (about)

     1  package main
     2  
     3  /*
     4  #include "api.h"
     5  //#include "robinhood.h"
     6  //#include "simpleclass.h"
     7  #include <stdlib.h>
     8  
     9  */
    10  import "C"
    11  import (
    12  	"fmt"
    13  	"github.com/moontrade/nogc"
    14  	"github.com/moontrade/nogc/alloc/rpmalloc"
    15  	"github.com/moontrade/nogc/collections/rhmap"
    16  	"github.com/moontrade/nogc/collections/tree"
    17  	"strconv"
    18  	"time"
    19  )
    20  
    21  func main() {
    22  	rpmalloc.Malloc(128)
    23  	println("malloc")
    24  	C.malloc(128)
    25  
    26  	for i := 0; i < 5; i++ {
    27  		go func() {
    28  			//runtime.LockOSThread()
    29  			C.malloc(128)
    30  		}()
    31  	}
    32  
    33  	println("ART")
    34  	tree, _ := tree.New()
    35  	////println("tree", uint(uintptr(unsafe.Pointer(tree))))
    36  	key := nogc.BytesOfString("hello")
    37  	value := nogc.BytesOfString("world!")
    38  	existing := tree.Insert(key.Pointer, key.Len(), value.Pointer)
    39  	println("existing", uint(uintptr(existing.Unsafe())))
    40  	existing = tree.InsertBytes(key, nogc.BytesOfString("world 2!").Pointer)
    41  	println("existing", nogc.BytesRef(existing).String())
    42  	existing = tree.FindBytes(key)
    43  	println("found", nogc.BytesRef(existing).String())
    44  	tree.Free()
    45  	println("ART size", tree.Size())
    46  	println("ART done")
    47  	//
    48  	for i := 0; i < 10; i++ {
    49  		//benchmarkCGO(100000000)
    50  		println("running...")
    51  		benchmarkARTInsert(1000000)
    52  		//benchmarkGoMapInsert(1000000)
    53  		benchmarkRHMapInsert(10000000)
    54  		benchmarkRHMapInt64Insert(10000000)
    55  		//time.Sleep(time.Second)
    56  	}
    57  	time.Sleep(time.Hour)
    58  }
    59  
    60  func benchmarkCGO(iterations int) {
    61  	start := time.Now().UnixNano()
    62  	for i := 0; i < iterations; i++ {
    63  		C.do_stub()
    64  	}
    65  	end := time.Now().UnixNano() - start
    66  	//println(float64(end)/float64(iterations))
    67  	fmt.Printf("CGO overhead %d -> %.1fns\n", iterations, float64(end)/float64(iterations))
    68  	//fmt.Printf("%.1fns\n", float64(end)/float64(iterations))
    69  }
    70  
    71  func benchmarkARTInsert(iterations int) {
    72  	tree, _ := tree.New()
    73  	key := nogc.AllocBytes(8)
    74  
    75  	start := time.Now().UnixNano()
    76  	for i := 0; i < iterations; i++ {
    77  		key.SetInt64BE(0, int64(i))
    78  		tree.InsertBytes(key, 0)
    79  	}
    80  	end := time.Now().UnixNano() - start
    81  	//println(float64(end)/float64(iterations))
    82  	fmt.Printf("ART insert %d -> %.1fns\n", iterations, float64(end)/float64(iterations))
    83  
    84  	key.SetInt64BE(0, 500) //int64(iterations/2))
    85  	start = time.Now().UnixNano()
    86  	for i := 0; i < iterations; i++ {
    87  		tree.FindBytes(key)
    88  	}
    89  	end = time.Now().UnixNano() - start
    90  	//println(float64(end)/float64(iterations))
    91  	fmt.Printf("ART get %d -> %.1fns\n", iterations, float64(end)/float64(iterations))
    92  
    93  	tree.Free()
    94  	key.Free()
    95  	//fmt.Printf("%.1fns\n", float64(end)/float64(iterations))
    96  }
    97  
    98  func benchmarkGoMapInsert(iterations int) {
    99  	mp := make(map[string]struct{})
   100  	key := nogc.AllocBytes(8)
   101  
   102  	start := time.Now().UnixNano()
   103  	for i := 0; i < iterations; i++ {
   104  		//key.SetInt64BE(0, int64(i))
   105  		mp[strconv.Itoa(i)] = struct{}{}
   106  	}
   107  	end := time.Now().UnixNano() - start
   108  	//println(float64(end)/float64(iterations))
   109  	fmt.Printf("GoMap insert %d -> %.1fns\n", iterations, float64(end)/float64(iterations))
   110  
   111  	key.SetInt64BE(0, 500) //int64(iterations/2))
   112  	searchKey := strconv.Itoa(iterations / 2)
   113  	start = time.Now().UnixNano()
   114  	for i := 0; i < iterations; i++ {
   115  		_, _ = mp[searchKey]
   116  	}
   117  	end = time.Now().UnixNano() - start
   118  	//println(float64(end)/float64(iterations))
   119  	fmt.Printf("GoMap get %d -> %.1fns\n", iterations, float64(end)/float64(iterations))
   120  
   121  	key.Free()
   122  	//fmt.Printf("%.1fns\n", float64(end)/float64(iterations))
   123  }
   124  
   125  func benchmarkRHMapInsert(iterations int) {
   126  	mp := rhmap.NewMap(uintptr(iterations * 2))
   127  	key := nogc.AllocBytes(8)
   128  
   129  	start := time.Now().UnixNano()
   130  	for i := 0; i < iterations; i++ {
   131  		key.SetInt64BE(0, int64(i))
   132  		mp.Set(key, nogc.Bytes{0})
   133  	}
   134  	end := time.Now().UnixNano() - start
   135  	//println(float64(end)/float64(iterations))
   136  	fmt.Printf("RHMap insert %d -> %.1fns\n", iterations, float64(end)/float64(iterations))
   137  
   138  	key.SetInt64BE(0, int64(iterations/2)) //int64(iterations/2))
   139  	start = time.Now().UnixNano()
   140  	for i := 0; i < iterations; i++ {
   141  		mp.Get(key)
   142  	}
   143  	end = time.Now().UnixNano() - start
   144  	//println(float64(end)/float64(iterations))
   145  	fmt.Printf("RHMap get %d -> %.1fns\n", iterations, float64(end)/float64(iterations))
   146  
   147  	_ = mp.Close()
   148  	key.Free()
   149  	//fmt.Printf("%.1fns\n", float64(end)/float64(iterations))
   150  }
   151  
   152  func benchmarkRHMapInt64Insert(iterations int64) {
   153  	mp := rhmap.NewMapInt64(uintptr(iterations * 2))
   154  
   155  	start := time.Now().UnixNano()
   156  	for i := int64(1); i < iterations; i++ {
   157  		mp.Set(i, 1)
   158  	}
   159  	end := time.Now().UnixNano() - start
   160  	//println(float64(end)/float64(iterations))
   161  	fmt.Printf("RHMapInt64 insert %d -> %.1fns\n", iterations, float64(end)/float64(iterations))
   162  
   163  	key := iterations / 2
   164  	start = time.Now().UnixNano()
   165  	for i := int64(1); i < iterations; i++ {
   166  		mp.Get(key)
   167  	}
   168  	end = time.Now().UnixNano() - start
   169  	//println(float64(end)/float64(iterations))
   170  	fmt.Printf("RHMapInt64 get %d -> %.1fns\n", iterations, float64(end)/float64(iterations))
   171  
   172  	_ = mp.Close()
   173  	//fmt.Printf("%.1fns\n", float64(end)/float64(iterations))
   174  }