github.com/lightlus/netstack@v1.2.0/tcpip/hash/jenkins/jenkins_test.go (about)

     1  // Copyright 2018 The gVisor Authors.
     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  package jenkins
    15  
    16  import (
    17  	"bytes"
    18  	"encoding/binary"
    19  	"hash"
    20  	"hash/fnv"
    21  	"math"
    22  	"testing"
    23  )
    24  
    25  func TestGolden32(t *testing.T) {
    26  	var golden32 = []struct {
    27  		out []byte
    28  		in  string
    29  	}{
    30  		{[]byte{0x00, 0x00, 0x00, 0x00}, ""},
    31  		{[]byte{0xca, 0x2e, 0x94, 0x42}, "a"},
    32  		{[]byte{0x45, 0xe6, 0x1e, 0x58}, "ab"},
    33  		{[]byte{0xed, 0x13, 0x1f, 0x5b}, "abc"},
    34  	}
    35  
    36  	hash := New32()
    37  
    38  	for _, g := range golden32 {
    39  		hash.Reset()
    40  		done, error := hash.Write([]byte(g.in))
    41  		if error != nil {
    42  			t.Fatalf("write error: %s", error)
    43  		}
    44  		if done != len(g.in) {
    45  			t.Fatalf("wrote only %d out of %d bytes", done, len(g.in))
    46  		}
    47  		if actual := hash.Sum(nil); !bytes.Equal(g.out, actual) {
    48  			t.Errorf("hash(%q) = 0x%x want 0x%x", g.in, actual, g.out)
    49  		}
    50  	}
    51  }
    52  
    53  func TestIntegrity32(t *testing.T) {
    54  	data := []byte{'1', '2', 3, 4, 5}
    55  
    56  	h := New32()
    57  	h.Write(data)
    58  	sum := h.Sum(nil)
    59  
    60  	if size := h.Size(); size != len(sum) {
    61  		t.Fatalf("Size()=%d but len(Sum())=%d", size, len(sum))
    62  	}
    63  
    64  	if a := h.Sum(nil); !bytes.Equal(sum, a) {
    65  		t.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum, a)
    66  	}
    67  
    68  	h.Reset()
    69  	h.Write(data)
    70  	if a := h.Sum(nil); !bytes.Equal(sum, a) {
    71  		t.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum, a)
    72  	}
    73  
    74  	h.Reset()
    75  	h.Write(data[:2])
    76  	h.Write(data[2:])
    77  	if a := h.Sum(nil); !bytes.Equal(sum, a) {
    78  		t.Fatalf("Sum()=0x%x, but with partial writes, Sum()=0x%x", sum, a)
    79  	}
    80  
    81  	sum32 := h.(hash.Hash32).Sum32()
    82  	if sum32 != binary.BigEndian.Uint32(sum) {
    83  		t.Fatalf("Sum()=0x%x, but Sum32()=0x%x", sum, sum32)
    84  	}
    85  }
    86  
    87  func BenchmarkJenkins32KB(b *testing.B) {
    88  	h := New32()
    89  
    90  	b.SetBytes(1024)
    91  	data := make([]byte, 1024)
    92  	for i := range data {
    93  		data[i] = byte(i)
    94  	}
    95  	in := make([]byte, 0, h.Size())
    96  
    97  	b.ResetTimer()
    98  	for i := 0; i < b.N; i++ {
    99  		h.Reset()
   100  		h.Write(data)
   101  		h.Sum(in)
   102  	}
   103  }
   104  
   105  func BenchmarkFnv32(b *testing.B) {
   106  	arr := make([]int64, 1000)
   107  	for i := 0; i < b.N; i++ {
   108  		var payload [8]byte
   109  		binary.BigEndian.PutUint32(payload[:4], uint32(i))
   110  		binary.BigEndian.PutUint32(payload[4:], uint32(i))
   111  
   112  		h := fnv.New32()
   113  		h.Write(payload[:])
   114  		idx := int(h.Sum32()) % len(arr)
   115  		arr[idx]++
   116  	}
   117  	b.StopTimer()
   118  	c := 0
   119  	if b.N > 1000000 {
   120  		for i := 0; i < len(arr)-1; i++ {
   121  			if math.Abs(float64(arr[i]-arr[i+1]))/float64(arr[i]) > float64(0.1) {
   122  				if c == 0 {
   123  					b.Logf("i %d val[i] %d val[i+1] %d b.N %b\n", i, arr[i], arr[i+1], b.N)
   124  				}
   125  				c++
   126  			}
   127  		}
   128  		if c > 0 {
   129  			b.Logf("Unbalanced buckets: %d", c)
   130  		}
   131  	}
   132  }
   133  
   134  func BenchmarkSum32(b *testing.B) {
   135  	arr := make([]int64, 1000)
   136  	for i := 0; i < b.N; i++ {
   137  		var payload [8]byte
   138  		binary.BigEndian.PutUint32(payload[:4], uint32(i))
   139  		binary.BigEndian.PutUint32(payload[4:], uint32(i))
   140  		h := Sum32(0)
   141  		h.Write(payload[:])
   142  		idx := int(h.Sum32()) % len(arr)
   143  		arr[idx]++
   144  	}
   145  	b.StopTimer()
   146  	if b.N > 1000000 {
   147  		for i := 0; i < len(arr)-1; i++ {
   148  			if math.Abs(float64(arr[i]-arr[i+1]))/float64(arr[i]) > float64(0.1) {
   149  				b.Logf("val[%3d]=%8d\tval[%3d]=%8d\tb.N=%b\n", i, arr[i], i+1, arr[i+1], b.N)
   150  				break
   151  			}
   152  		}
   153  	}
   154  }
   155  
   156  func BenchmarkNew32(b *testing.B) {
   157  	arr := make([]int64, 1000)
   158  	for i := 0; i < b.N; i++ {
   159  		var payload [8]byte
   160  		binary.BigEndian.PutUint32(payload[:4], uint32(i))
   161  		binary.BigEndian.PutUint32(payload[4:], uint32(i))
   162  		h := New32()
   163  		h.Write(payload[:])
   164  		idx := int(h.Sum32()) % len(arr)
   165  		arr[idx]++
   166  	}
   167  	b.StopTimer()
   168  	if b.N > 1000000 {
   169  		for i := 0; i < len(arr)-1; i++ {
   170  			if math.Abs(float64(arr[i]-arr[i+1]))/float64(arr[i]) > float64(0.1) {
   171  				b.Logf("val[%3d]=%8d\tval[%3d]=%8d\tb.N=%b\n", i, arr[i], i+1, arr[i+1], b.N)
   172  				break
   173  			}
   174  		}
   175  	}
   176  }