Files
seaweedfs/weed/filer/reader_at_benchmark_test.go
T
25e0c6080a filer: resume chunk reads where the last one ended (#13393)
* filer: resume chunk reads where the last one ended

ChunkReadAt walked the chunk views from the front on every ReadAt, so
reading a file of n chunks front to back took time proportional to n
squared. S3 GETs (256 KiB reads) and WebDAV downloads (32 KiB reads)
both read whole files through it: a 32 KiB read near the end of a file
of 100,000 chunks took 164 µs instead of 8 µs.

Keep where the last read ended and start the next walk there when it is
at or before the new offset. The mount merges new chunks into a live
reader's views, so the interval list now counts its edits and a kept
position is only used while the count is unchanged.

* filer: benchmark ChunkReadAt reads

Whole-file sequential reads at 1 to 100,000 chunks, one-chunk files read
with a new reader, and random reads, all served by mockChunkCache so they
measure the reader itself.

* filer: trim the resume-point comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Denis Maier <denis@dAir.lab>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 11:22:53 -08:00

105 lines
3.1 KiB
Go

package filer
import (
"context"
"fmt"
"io"
"math"
"math/rand"
"strconv"
"testing"
)
// benchReader returns a reader over a file of n chunks of chunkSize bytes,
// served by mockChunkCache, so the benchmarks measure ChunkReadAt itself.
func benchReader(n int, chunkSize int64) func() *ChunkReadAt {
visibles := NewIntervalList[*VisibleInterval]()
for i := 0; i < n; i++ {
start := int64(i) * chunkSize
addVisibleInterval(visibles, &VisibleInterval{start: start, stop: start + chunkSize, fileId: strconv.Itoa(i + 1), chunkSize: uint64(chunkSize)})
}
views := ViewFromVisibleIntervals(visibles, 0, math.MaxInt64)
readerCache := NewReaderCache(3, &mockChunkCache{}, nil, nil)
return func() *ChunkReadAt {
return &ChunkReadAt{
chunkViews: views,
fileSize: int64(n) * chunkSize,
readerCache: readerCache,
readerPattern: NewReaderPattern(),
ctx: context.Background(),
}
}
}
func benchReadAt(b *testing.B, r *ChunkReadAt, buf []byte, offset int64) {
if n, err := r.ReadAt(buf, offset); n != len(buf) || (err != nil && err != io.EOF) {
b.Fatalf("read at %d: %d bytes, %v", offset, n, err)
}
}
// BenchmarkReaderAtSequential reads a whole file front to back in 32 KiB
// reads, as S3 and WebDAV GETs do, with a new reader per file.
func BenchmarkReaderAtSequential(b *testing.B) {
const chunkSize, readSize = 64 * 1024, 32 * 1024
for _, chunks := range []int{1, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("chunks=%d", chunks), func(b *testing.B) {
newReader := benchReader(chunks, chunkSize)
fileSize := int64(chunks) * chunkSize
buf := make([]byte, readSize)
b.SetBytes(fileSize)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := newReader()
for off := int64(0); off < fileSize; off += readSize {
benchReadAt(b, r, buf, off)
}
r.ReleaseStream()
}
})
}
}
// BenchmarkReaderAtSmallFile reads a one-chunk file with a new reader, the
// way a GET of a small object does.
func BenchmarkReaderAtSmallFile(b *testing.B) {
for _, size := range []int64{4 * 1024, 64 * 1024, 1024 * 1024} {
b.Run(fmt.Sprintf("size=%dKiB", size/1024), func(b *testing.B) {
newReader := benchReader(1, size)
readSize := min(size, 32*1024)
buf := make([]byte, readSize)
b.SetBytes(size)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := newReader()
for off := int64(0); off < size; off += readSize {
benchReadAt(b, r, buf, off)
}
r.ReleaseStream()
}
})
}
}
// BenchmarkReaderAtRandom makes 32 KiB reads at random offsets on one reader,
// where a kept position helps only when the next read is further on.
func BenchmarkReaderAtRandom(b *testing.B) {
const chunkSize, readSize = 64 * 1024, 32 * 1024
for _, chunks := range []int{100, 10000} {
b.Run(fmt.Sprintf("chunks=%d", chunks), func(b *testing.B) {
r := benchReader(chunks, chunkSize)()
defer r.ReleaseStream()
reads := int64(chunks) * chunkSize / readSize
rnd := rand.New(rand.NewSource(1))
buf := make([]byte, readSize)
b.SetBytes(readSize)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchReadAt(b, r, buf, rnd.Int63n(reads)*readSize)
}
})
}
}