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