]> Sergey Matveev's repositories - btrtrc.git/blob - smartban_test.go
cmd/btrtrc client
[btrtrc.git] / smartban_test.go
1 package torrent
2
3 import (
4         "crypto/sha1"
5         "net/netip"
6         "testing"
7
8         "github.com/anacrolix/missinggo/v2/iter"
9         "github.com/cespare/xxhash"
10
11         "github.com/anacrolix/torrent/smartban"
12 )
13
14 func benchmarkSmartBanRecordBlock[Sum comparable](b *testing.B, hash func([]byte) Sum) {
15         var cache smartban.Cache[bannableAddr, RequestIndex, Sum]
16         cache.Hash = hash
17         cache.Init()
18         var data [defaultChunkSize]byte
19         var addr netip.Addr
20         b.SetBytes(int64(len(data)))
21         for i := range iter.N(b.N) {
22                 cache.RecordBlock(addr, RequestIndex(i), data[:])
23         }
24 }
25
26 func BenchmarkSmartBanRecordBlock(b *testing.B) {
27         b.Run("xxHash", func(b *testing.B) {
28                 var salt [8]byte
29                 benchmarkSmartBanRecordBlock(b, func(block []byte) uint64 {
30                         h := xxhash.New()
31                         // xxHash is not cryptographic, and so we're salting it so attackers can't know a priori
32                         // where block data collisions are.
33                         h.Write(salt[:])
34                         h.Write(block)
35                         return h.Sum64()
36                 })
37         })
38         b.Run("Sha1", func(b *testing.B) {
39                 benchmarkSmartBanRecordBlock(b, sha1.Sum)
40         })
41 }