github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/tests/runtime/memhash_test.go (about)

     1  package main
     2  
     3  import (
     4  	"hash/maphash"
     5  	"strconv"
     6  	"testing"
     7  )
     8  
     9  var buf [8192]byte
    10  
    11  func BenchmarkMaphash(b *testing.B) {
    12  	var h maphash.Hash
    13  	benchmarkHash(b, "maphash", h)
    14  }
    15  
    16  func benchmarkHash(b *testing.B, str string, h maphash.Hash) {
    17  	var sizes = []int{1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 1024, 8192}
    18  	for _, n := range sizes {
    19  		b.Run(strconv.Itoa(n), func(b *testing.B) { benchmarkHashn(b, int64(n), h) })
    20  	}
    21  }
    22  
    23  var total uint64
    24  
    25  func benchmarkHashn(b *testing.B, size int64, h maphash.Hash) {
    26  	b.SetBytes(size)
    27  
    28  	sum := make([]byte, 4)
    29  
    30  	for i := 0; i < b.N; i++ {
    31  		h.Reset()
    32  		h.Write(buf[:size])
    33  		sum = h.Sum(sum[:0])
    34  		total += uint64(sum[0])
    35  	}
    36  }