github.com/maruel/nin@v0.0.0-20220112143044-f35891e3ce7e/cmd/hash_collision_bench/hash_collision_bench.go (about)

     1  // Copyright 2012 Google Inc. All Rights Reserved.
     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 main
    16  
    17  import (
    18  	"fmt"
    19  	"math/rand"
    20  	"sort"
    21  	"time"
    22  
    23  	"github.com/maruel/nin"
    24  )
    25  
    26  func random(low, high int) int {
    27  	return rand.Intn(high-low+1) + low
    28  }
    29  
    30  func randomCommand() string {
    31  	len2 := random(5, 100)
    32  	s := make([]byte, len2)
    33  	for i := 0; i < len2; i++ {
    34  		s[i] = byte(random(32, 127))
    35  	}
    36  	return string(s)
    37  }
    38  
    39  type item struct {
    40  	hash uint64
    41  	i    int
    42  }
    43  
    44  func main() {
    45  	const N = 20 * 1000 * 1000
    46  
    47  	commands := [N]string{}
    48  	hashes := [N]item{}
    49  
    50  	rand.Seed(time.Now().UnixNano())
    51  
    52  	for i := 0; i < N; i++ {
    53  		commands[i] = randomCommand()
    54  		hashes[i] = item{nin.HashCommand(commands[i]), i}
    55  	}
    56  
    57  	sort.Slice(hashes[:], func(i, j int) bool {
    58  		return hashes[i].hash < hashes[j].hash
    59  	})
    60  
    61  	collisionCount := 0
    62  	for i := 1; i < N; i++ {
    63  		if hashes[i-1].hash == hashes[i].hash {
    64  			lhs := commands[hashes[i-1].i]
    65  			rhs := commands[hashes[i].i]
    66  			if lhs != rhs {
    67  				fmt.Printf("collision!\n  string 1: '%s'\n  string 2: '%s'\n", lhs, rhs)
    68  				collisionCount++
    69  			}
    70  		}
    71  	}
    72  	fmt.Printf("\n\n%d collisions after %d runs\n", collisionCount, N)
    73  }