2
0
mirror of synced 2025-02-24 14:48:27 +00:00

Small tweak to readahead, and fix the tests

This commit is contained in:
Matt Joiner 2015-06-16 17:14:15 +10:00
parent ced5733c88
commit b2b290a589
2 changed files with 12 additions and 8 deletions

View File

@ -276,9 +276,13 @@ again:
// Calculates the number of pieces to set to Readahead priority, after the // Calculates the number of pieces to set to Readahead priority, after the
// Now, and Next pieces. // Now, and Next pieces.
func readaheadPieces(readahead, pieceLength int64) (ret int) { func readaheadPieces(readahead, pieceLength int64) (ret int) {
// Expand the readahead to fit any partial pieces. Subtract 1 for the
// "next" piece that is assigned.
ret = int((readahead+pieceLength-1)/pieceLength - 1) ret = int((readahead+pieceLength-1)/pieceLength - 1)
// Lengthen the "readahead tail" to smooth blockiness that occurs when the
// piece length is much larger than the readahead.
if ret < 2 { if ret < 2 {
ret = 2 ret++
} }
return return
} }

View File

@ -15,6 +15,7 @@ import (
_ "github.com/anacrolix/envpprof" _ "github.com/anacrolix/envpprof"
"github.com/anacrolix/utp" "github.com/anacrolix/utp"
"github.com/bradfitz/iter" "github.com/bradfitz/iter"
"github.com/stretchr/testify/assert"
"gopkg.in/check.v1" "gopkg.in/check.v1"
"github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/bencode"
@ -295,15 +296,14 @@ func TestReadaheadPieces(t *testing.T) {
readaheadPieces int readaheadPieces int
}{ }{
{5 * 1024 * 1024, 256 * 1024, 19}, {5 * 1024 * 1024, 256 * 1024, 19},
{5 * 1024 * 1024, 5 * 1024 * 1024, 0}, {5 * 1024 * 1024, 5 * 1024 * 1024, 1},
{5*1024*1024 - 1, 5 * 1024 * 1024, 0}, {5*1024*1024 - 1, 5 * 1024 * 1024, 1},
{5 * 1024 * 1024, 5*1024*1024 - 1, 1}, {5 * 1024 * 1024, 5*1024*1024 - 1, 2},
{0, 5 * 1024 * 1024, -1}, {0, 5 * 1024 * 1024, 0},
{5 * 1024 * 1024, 1048576, 4}, {5 * 1024 * 1024, 1048576, 4},
} { } {
if readaheadPieces(case_.readaheadBytes, case_.pieceLength) != case_.readaheadPieces { pieces := readaheadPieces(case_.readaheadBytes, case_.pieceLength)
t.Fatalf("case failed: %v", case_) assert.Equal(t, case_.readaheadPieces, pieces, "%v", case_)
}
} }
} }