github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/objstorage/objstorageprovider/readahead_test.go (about)

     1  // Copyright 2023 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package objstorageprovider
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/cockroachdb/datadriven"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestMaybeReadahead(t *testing.T) {
    18  	rs := makeReadaheadState(256 * 1024)
    19  	datadriven.RunTest(t, "testdata/readahead", func(t *testing.T, d *datadriven.TestData) string {
    20  		cacheHit := false
    21  		switch d.Cmd {
    22  		case "reset":
    23  			rs.size = initialReadaheadSize
    24  			rs.limit = 0
    25  			rs.numReads = 0
    26  			return ""
    27  
    28  		case "cache-read":
    29  			cacheHit = true
    30  			fallthrough
    31  		case "read":
    32  			args := strings.Split(d.Input, ",")
    33  			if len(args) != 2 {
    34  				return "expected 2 args: offset, size"
    35  			}
    36  
    37  			offset, err := strconv.ParseInt(strings.TrimSpace(args[0]), 10, 64)
    38  			require.NoError(t, err)
    39  			size, err := strconv.ParseInt(strings.TrimSpace(args[1]), 10, 64)
    40  			require.NoError(t, err)
    41  			var raSize int64
    42  			if cacheHit {
    43  				rs.recordCacheHit(offset, size)
    44  			} else {
    45  				raSize = rs.maybeReadahead(offset, size)
    46  			}
    47  
    48  			var buf strings.Builder
    49  			fmt.Fprintf(&buf, "readahead:  %d\n", raSize)
    50  			fmt.Fprintf(&buf, "numReads:   %d\n", rs.numReads)
    51  			fmt.Fprintf(&buf, "size:       %d\n", rs.size)
    52  			fmt.Fprintf(&buf, "prevSize:   %d\n", rs.prevSize)
    53  			fmt.Fprintf(&buf, "limit:      %d", rs.limit)
    54  			return buf.String()
    55  		default:
    56  			return fmt.Sprintf("unknown command: %s", d.Cmd)
    57  		}
    58  	})
    59  }