-
-
Notifications
You must be signed in to change notification settings - Fork 630
/
peer-conn-msg-writer_test.go
68 lines (58 loc) · 1.43 KB
/
peer-conn-msg-writer_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package torrent
import (
"bytes"
"testing"
"github.com/dustin/go-humanize"
pp "github.com/anacrolix/torrent/peer_protocol"
)
func PieceMsg(length int64) pp.Message {
return pp.Message{
Type: pp.Piece,
Index: pp.Integer(0),
Begin: pp.Integer(0),
Piece: make([]byte, length),
}
}
var benchmarkPieceLengths = []int{defaultChunkSize, 1 << 20, 4 << 20, 8 << 20}
func runBenchmarkWriteToBuffer(b *testing.B, length int64) {
writer := &peerConnMsgWriter{
writeBuffer: &bytes.Buffer{},
}
msg := PieceMsg(length)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
//b.StopTimer()
writer.writeBuffer.Reset()
//b.StartTimer()
writer.writeToBuffer(msg)
}
}
func BenchmarkWritePieceMsg(b *testing.B) {
for _, length := range benchmarkPieceLengths {
b.Run(humanize.IBytes(uint64(length)), func(b *testing.B) {
b.Run("ToBuffer", func(b *testing.B) {
b.SetBytes(int64(length))
runBenchmarkWriteToBuffer(b, int64(length))
})
b.Run("MarshalBinary", func(b *testing.B) {
b.SetBytes(int64(length))
runBenchmarkMarshalBinaryWrite(b, int64(length))
})
})
}
}
func runBenchmarkMarshalBinaryWrite(b *testing.B, length int64) {
writer := &peerConnMsgWriter{
writeBuffer: &bytes.Buffer{},
}
msg := PieceMsg(length)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
//b.StopTimer()
writer.writeBuffer.Reset()
//b.StartTimer()
writer.writeBuffer.Write(msg.MustMarshalBinary())
}
}