]> Sergey Matveev's repositories - btrtrc.git/blob - request-strategy/piece-request-order_test.go
Benchmark PieceRequestOrder with varying styles of path hint usage
[btrtrc.git] / request-strategy / piece-request-order_test.go
1 package request_strategy
2
3 import (
4         "testing"
5
6         "github.com/bradfitz/iter"
7 )
8
9 func benchmarkPieceRequestOrder(
10         b *testing.B,
11         hintForPiece func(index int) *PieceRequestOrderPathHint,
12         numPieces int,
13 ) {
14         b.ResetTimer()
15         b.ReportAllocs()
16         for range iter.N(b.N) {
17                 pro := NewPieceOrder()
18                 state := PieceRequestOrderState{}
19                 doPieces := func(m func(PieceRequestOrderKey)) {
20                         for i := range iter.N(numPieces) {
21                                 key := PieceRequestOrderKey{
22                                         Index: i,
23                                 }
24                                 pro.PathHint = hintForPiece(i)
25                                 m(key)
26                         }
27                 }
28                 doPieces(func(key PieceRequestOrderKey) {
29                         pro.Add(key, state)
30                 })
31                 state.Availability++
32                 doPieces(func(key PieceRequestOrderKey) {
33                         pro.Update(key, state)
34                 })
35                 doPieces(func(key PieceRequestOrderKey) {
36                         state.Priority = piecePriority(key.Index / 4)
37                         pro.Update(key, state)
38                 })
39                 // state.Priority = 0
40                 state.Availability++
41                 doPieces(func(key PieceRequestOrderKey) {
42                         pro.Update(key, state)
43                 })
44                 state.Availability--
45                 doPieces(func(key PieceRequestOrderKey) {
46                         pro.Update(key, state)
47                 })
48                 doPieces(pro.Delete)
49                 if pro.Len() != 0 {
50                         b.FailNow()
51                 }
52         }
53 }
54
55 func BenchmarkPieceRequestOrder(b *testing.B) {
56         const numPieces = 2000
57         b.Run("NoPathHints", func(b *testing.B) {
58                 benchmarkPieceRequestOrder(b, func(int) *PieceRequestOrderPathHint {
59                         return nil
60                 }, numPieces)
61         })
62         b.Run("SharedPathHint", func(b *testing.B) {
63                 var pathHint PieceRequestOrderPathHint
64                 benchmarkPieceRequestOrder(b, func(int) *PieceRequestOrderPathHint {
65                         return &pathHint
66                 }, numPieces)
67         })
68         b.Run("PathHintPerPiece", func(b *testing.B) {
69                 pathHints := make([]PieceRequestOrderPathHint, numPieces)
70                 benchmarkPieceRequestOrder(b, func(index int) *PieceRequestOrderPathHint {
71                         return &pathHints[index]
72                 }, numPieces)
73         })
74 }