github.com/lkarlslund/stringdedup@v0.6.2/sd_benchmark_test.go (about)

     1  package stringdedup
     2  
     3  import (
     4  	"math/rand"
     5  	"runtime"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/OneOfOne/xxhash"
    10  )
    11  
    12  // const letterBytes = "abcdef"
    13  
    14  const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    15  const (
    16  	letterIdxBits = 6                    // 6 bits to represent a letter index
    17  	letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
    18  	letterIdxMax  = 63 / letterIdxBits   // # of letter indices fitting in 63 bits
    19  )
    20  
    21  var src = rand.NewSource(time.Now().UnixNano())
    22  
    23  func RandomBytes(b []byte) {
    24  	// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
    25  	for i, cache, remain := len(b)-1, src.Int63(), letterIdxMax; i >= 0; {
    26  		if remain == 0 {
    27  			cache, remain = src.Int63(), letterIdxMax
    28  		}
    29  		if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
    30  			b[i] = letterBytes[idx]
    31  			i--
    32  		}
    33  		cache >>= letterIdxBits
    34  		remain--
    35  	}
    36  }
    37  
    38  func generatestrings(totalstrings, stringlength int) []string {
    39  	if totalstrings < 1 {
    40  		totalstrings = 1
    41  	}
    42  	generated := make([]string, totalstrings)
    43  	b := make([]byte, stringlength)
    44  	for i := 0; i < len(generated); i++ {
    45  		RandomBytes(b)
    46  		generated[i] = string(b)
    47  	}
    48  	return generated
    49  }
    50  
    51  var bs = make([]byte, 12)
    52  
    53  func BenchmarkGoRandom(b *testing.B) {
    54  	var s = make([]string, b.N)
    55  	for n := 0; n < b.N; n++ {
    56  		RandomBytes(bs)
    57  		s[n] = string(bs)
    58  	}
    59  }
    60  
    61  func BenchmarkNSDRandom(b *testing.B) {
    62  	sd := New(func(in []byte) uint32 {
    63  		return xxhash.Checksum32(in)
    64  	})
    65  	var s = make([]string, b.N)
    66  	for n := 0; n < b.N; n++ {
    67  		RandomBytes(bs)
    68  		s[n] = sd.BS(bs)
    69  	}
    70  	runtime.KeepAlive(s)
    71  }
    72  
    73  func BenchmarkNSDRandomNoValidate(b *testing.B) {
    74  	sd := New(func(in []byte) uint32 {
    75  		return xxhash.Checksum32(in)
    76  	})
    77  	sd.DontValidateResults = true
    78  	var s = make([]string, b.N)
    79  	for n := 0; n < b.N; n++ {
    80  		RandomBytes(bs)
    81  		s[n] = sd.BS(bs)
    82  	}
    83  	runtime.KeepAlive(s)
    84  }
    85  
    86  func BenchmarkNSD64Random(b *testing.B) {
    87  	sd := New(func(in []byte) uint64 {
    88  		return xxhash.Checksum64(in)
    89  	})
    90  	var s = make([]string, b.N)
    91  	for n := 0; n < b.N; n++ {
    92  		RandomBytes(bs)
    93  		s[n] = sd.BS(bs)
    94  	}
    95  	runtime.KeepAlive(s)
    96  }
    97  
    98  func BenchmarkNSD64RandomNoValidate(b *testing.B) {
    99  	sd := New(func(in []byte) uint64 {
   100  		return xxhash.Checksum64(in)
   101  	})
   102  	sd.DontValidateResults = true
   103  	var s = make([]string, b.N)
   104  	for n := 0; n < b.N; n++ {
   105  		RandomBytes(bs)
   106  		s[n] = sd.BS(bs)
   107  	}
   108  	runtime.KeepAlive(s)
   109  }
   110  
   111  var somestring = "SomeStaticString"
   112  
   113  func BenchmarkGoPrecalculated(b *testing.B) {
   114  	b.StopTimer()
   115  	generated := generatestrings(b.N/10, 5)
   116  	b.StartTimer()
   117  	var s = make([]string, b.N)
   118  	for n := 0; n < b.N; n++ {
   119  		s[n] = generated[n%len(generated)]
   120  	}
   121  	runtime.KeepAlive(s)
   122  }
   123  
   124  func BenchmarkNSDPrecalculated(b *testing.B) {
   125  	b.StopTimer()
   126  	generated := generatestrings(b.N/10, 5)
   127  	b.StartTimer()
   128  	sd := New(func(in []byte) uint32 {
   129  		return xxhash.Checksum32(in)
   130  	})
   131  	var s = make([]string, b.N)
   132  	for n := 0; n < b.N; n++ {
   133  		s[n] = sd.S(generated[n%len(generated)])
   134  	}
   135  	runtime.KeepAlive(s)
   136  }
   137  
   138  func BenchmarkNSDPrecalculatedNoValidate(b *testing.B) {
   139  	b.StopTimer()
   140  	generated := generatestrings(b.N/10, 5)
   141  	b.StartTimer()
   142  	sd := New(func(in []byte) uint32 {
   143  		return xxhash.Checksum32(in)
   144  	})
   145  	sd.DontValidateResults = true
   146  	var s = make([]string, b.N)
   147  	for n := 0; n < b.N; n++ {
   148  		s[n] = sd.S(generated[n%len(generated)])
   149  	}
   150  	runtime.KeepAlive(s)
   151  }
   152  
   153  func BenchmarkNSD64Precalculated(b *testing.B) {
   154  	b.StopTimer()
   155  	generated := generatestrings(b.N/10, 5)
   156  	b.StartTimer()
   157  	sd := New(func(in []byte) uint64 {
   158  		return xxhash.Checksum64(in)
   159  	})
   160  	var s = make([]string, b.N)
   161  	for n := 0; n < b.N; n++ {
   162  		s[n] = sd.S(generated[n%len(generated)])
   163  	}
   164  	runtime.KeepAlive(s)
   165  }
   166  
   167  func BenchmarkNSD64PrecalculatedNoValidate(b *testing.B) {
   168  	b.StopTimer()
   169  	generated := generatestrings(b.N/10, 5)
   170  	b.StartTimer()
   171  	sd := New(func(in []byte) uint64 {
   172  		return xxhash.Checksum64(in)
   173  	})
   174  	sd.DontValidateResults = true
   175  	var s = make([]string, b.N)
   176  	for n := 0; n < b.N; n++ {
   177  		s[n] = sd.S(generated[n%len(generated)])
   178  	}
   179  	runtime.KeepAlive(s)
   180  }