github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/internal/wyhash/wyhash_test.go (about)

     1  // Copyright 2021 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package wyhash
    16  
    17  import (
    18  	"fmt"
    19  	"runtime"
    20  	"testing"
    21  )
    22  
    23  func BenchmarkWyhash(b *testing.B) {
    24  	sizes := []int{17, 21, 24, 29, 32,
    25  		33, 64, 69, 96, 97, 128, 129, 240, 241,
    26  		512, 1024, 100 * 1024,
    27  	}
    28  
    29  	for size := 0; size <= 16; size++ {
    30  		b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
    31  			b.SetBytes(int64(size))
    32  			var (
    33  				x    uint64
    34  				data = string(make([]byte, size))
    35  			)
    36  			b.ReportAllocs()
    37  			b.ResetTimer()
    38  
    39  			for i := 0; i < b.N; i++ {
    40  				x = Sum64String(data)
    41  			}
    42  			runtime.KeepAlive(x)
    43  		})
    44  	}
    45  
    46  	for _, size := range sizes {
    47  		b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
    48  			b.SetBytes(int64(size))
    49  			var x uint64
    50  			data := string(make([]byte, size))
    51  			b.ReportAllocs()
    52  			b.ResetTimer()
    53  
    54  			for i := 0; i < b.N; i++ {
    55  				x = Sum64String(data)
    56  			}
    57  			runtime.KeepAlive(x)
    58  		})
    59  	}
    60  }