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