1 package request_strategy
6 "github.com/bradfitz/iter"
9 func benchmarkPieceRequestOrder[B Btree](
11 // Initialize the next run, and return a Btree
13 // Set any path hinting for the specified piece
14 hintForPiece func(index int),
19 for range iter.N(b.N) {
20 pro := NewPieceOrder(newBtree(), numPieces)
21 state := PieceRequestOrderState{}
22 doPieces := func(m func(PieceRequestOrderKey)) {
23 for i := range iter.N(numPieces) {
24 key := PieceRequestOrderKey{
31 doPieces(func(key PieceRequestOrderKey) {
35 doPieces(func(key PieceRequestOrderKey) {
36 pro.Update(key, state)
38 pro.tree.Scan(func(item pieceRequestOrderItem) bool {
41 doPieces(func(key PieceRequestOrderKey) {
42 state.Priority = piecePriority(key.Index / 4)
43 pro.Update(key, state)
45 pro.tree.Scan(func(item pieceRequestOrderItem) bool {
46 return item.key.Index < 1000
50 doPieces(func(key PieceRequestOrderKey) {
51 pro.Update(key, state)
53 pro.tree.Scan(func(item pieceRequestOrderItem) bool {
54 return item.key.Index < 1000
57 doPieces(func(key PieceRequestOrderKey) {
58 pro.Update(key, state)
67 func zero[T any](t *T) {
72 func BenchmarkPieceRequestOrder(b *testing.B) {
73 const numPieces = 2000
74 b.Run("TidwallBtree", func(b *testing.B) {
75 b.Run("NoPathHints", func(b *testing.B) {
76 benchmarkPieceRequestOrder(b, NewTidwallBtree, func(int) {}, numPieces)
78 b.Run("SharedPathHint", func(b *testing.B) {
79 var pathHint PieceRequestOrderPathHint
80 var btree *tidwallBtree
81 benchmarkPieceRequestOrder(
82 b, func() *tidwallBtree {
84 btree = NewTidwallBtree()
85 btree.PathHint = &pathHint
87 }, func(int) {}, numPieces,
90 b.Run("PathHintPerPiece", func(b *testing.B) {
91 pathHints := make([]PieceRequestOrderPathHint, numPieces)
92 var btree *tidwallBtree
93 benchmarkPieceRequestOrder(
94 b, func() *tidwallBtree {
95 btree = NewTidwallBtree()
98 btree.PathHint = &pathHints[index]
103 b.Run("AjwernerBtree", func(b *testing.B) {
104 benchmarkPieceRequestOrder(b, NewAjwernerBtree, func(index int) {}, numPieces)