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