github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/compaction_picker_test.go (about)

     1  // Copyright 2018 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 pebble
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"strconv"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/petermattis/pebble/internal/datadriven"
    15  )
    16  
    17  func loadVersion(d *datadriven.TestData) (*version, *Options, string) {
    18  	opts := &Options{}
    19  	opts.EnsureDefaults()
    20  
    21  	if len(d.CmdArgs) != 1 {
    22  		return nil, nil, fmt.Sprintf("%s expects 1 argument", d.Cmd)
    23  	}
    24  	var err error
    25  	opts.LBaseMaxBytes, err = strconv.ParseInt(d.CmdArgs[0].Key, 10, 64)
    26  	if err != nil {
    27  		return nil, nil, err.Error()
    28  	}
    29  
    30  	vers := &version{}
    31  	if len(d.Input) > 0 {
    32  		for _, data := range strings.Split(d.Input, "\n") {
    33  			parts := strings.Split(data, ":")
    34  			if len(parts) != 2 {
    35  				return nil, nil, fmt.Sprintf("malformed test:\n%s", d.Input)
    36  			}
    37  			level, err := strconv.Atoi(parts[0])
    38  			if err != nil {
    39  				return nil, nil, err.Error()
    40  			}
    41  			if vers.Files[level] != nil {
    42  				return nil, nil, fmt.Sprintf("level %d already filled", level)
    43  			}
    44  			size, err := strconv.ParseUint(strings.TrimSpace(parts[1]), 10, 64)
    45  			if err != nil {
    46  				return nil, nil, err.Error()
    47  			}
    48  			if level == 0 {
    49  				for i := uint64(0); i < size; i++ {
    50  					vers.Files[level] = append(vers.Files[level], fileMetadata{
    51  						Size: 1,
    52  					})
    53  				}
    54  			} else {
    55  				vers.Files[level] = append(vers.Files[level], fileMetadata{
    56  					Size: size,
    57  				})
    58  			}
    59  		}
    60  	}
    61  
    62  	return vers, opts, ""
    63  }
    64  
    65  func TestCompactionPickerLevelMaxBytes(t *testing.T) {
    66  	datadriven.RunTest(t, "testdata/compaction_picker_level_max_bytes",
    67  		func(d *datadriven.TestData) string {
    68  			switch d.Cmd {
    69  			case "init":
    70  				vers, opts, errMsg := loadVersion(d)
    71  				if errMsg != "" {
    72  					return errMsg
    73  				}
    74  
    75  				p := newCompactionPicker(vers, opts)
    76  				var buf bytes.Buffer
    77  				for level := p.baseLevel; level < numLevels; level++ {
    78  					fmt.Fprintf(&buf, "%d: %d\n", level, p.levelMaxBytes[level])
    79  				}
    80  				return buf.String()
    81  
    82  			default:
    83  				return fmt.Sprintf("unknown command: %s", d.Cmd)
    84  			}
    85  		})
    86  }
    87  
    88  func TestCompactionPickerTargetLevel(t *testing.T) {
    89  	datadriven.RunTest(t, "testdata/compaction_picker_target_level",
    90  		func(d *datadriven.TestData) string {
    91  			switch d.Cmd {
    92  			case "pick":
    93  				vers, opts, errMsg := loadVersion(d)
    94  				if errMsg != "" {
    95  					return errMsg
    96  				}
    97  
    98  				p := newCompactionPicker(vers, opts)
    99  				return fmt.Sprintf("%d: %.1f\n", p.level, p.score)
   100  
   101  			default:
   102  				return fmt.Sprintf("unknown command: %s", d.Cmd)
   103  			}
   104  		})
   105  }
   106  
   107  func TestCompactionPickerEstimatedCompactionDebt(t *testing.T) {
   108  	datadriven.RunTest(t, "testdata/compaction_picker_estimated_debt",
   109  		func(d *datadriven.TestData) string {
   110  			switch d.Cmd {
   111  			case "init":
   112  				vers, opts, errMsg := loadVersion(d)
   113  				if errMsg != "" {
   114  					return errMsg
   115  				}
   116  				opts.MemTableSize = 1000
   117  
   118  				p := newCompactionPicker(vers, opts)
   119  				return fmt.Sprintf("%d\n", p.estimatedCompactionDebt(0))
   120  
   121  			default:
   122  				return fmt.Sprintf("unknown command: %s", d.Cmd)
   123  			}
   124  		})
   125  }