forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_test.go
223 lines (202 loc) · 7.24 KB
/
metrics_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2019 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 (
"fmt"
"testing"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/datadriven"
"github.com/cockroachdb/pebble/vfs"
"github.com/cockroachdb/redact"
"github.com/stretchr/testify/require"
)
func TestMetricsFormat(t *testing.T) {
var m Metrics
m.BlockCache.Size = 1
m.BlockCache.Count = 2
m.BlockCache.Hits = 3
m.BlockCache.Misses = 4
m.Compact.Count = 5
m.Compact.EstimatedDebt = 6
m.Compact.InProgressBytes = 7
m.Flush.Count = 8
m.Filter.Hits = 9
m.Filter.Misses = 10
m.MemTable.Size = 11
m.MemTable.Count = 12
m.MemTable.ZombieSize = 13
m.MemTable.ZombieCount = 14
m.Table.ZombieSize = 15
m.Table.ZombieCount = 16
m.TableCache.Size = 17
m.TableCache.Count = 18
m.TableCache.Hits = 19
m.TableCache.Misses = 20
m.TableIters = 21
m.WAL.Files = 22
m.WAL.ObsoleteFiles = 23
m.WAL.Size = 24
m.WAL.BytesIn = 25
m.WAL.BytesWritten = 26
for i := range m.Levels {
l := &m.Levels[i]
base := uint64((i + 1) * 100)
l.Sublevels = int32(i + 1)
l.NumFiles = int64(base) + 1
l.Size = int64(base) + 2
l.Score = float64(base) + 3
l.BytesIn = base + 4
l.BytesIngested = base + 4
l.BytesMoved = base + 6
l.BytesRead = base + 7
l.BytesCompacted = base + 8
l.BytesFlushed = base + 9
l.TablesCompacted = base + 10
l.TablesFlushed = base + 11
l.TablesIngested = base + 12
l.TablesMoved = base + 13
}
const expected = `
__level_____count____size___score______in__ingest(sz_cnt)____move(sz_cnt)___write(sz_cnt)____read___r-amp___w-amp
WAL 22 24 B - 25 B - - - - 26 B - - - 1.0
0 101 102 B 103.00 104 B 104 B 112 106 B 113 217 B 221 107 B 1 2.1
1 201 202 B 203.00 204 B 204 B 212 206 B 213 417 B 421 207 B 2 2.0
2 301 302 B 303.00 304 B 304 B 312 306 B 313 617 B 621 307 B 3 2.0
3 401 402 B 403.00 404 B 404 B 412 406 B 413 817 B 821 407 B 4 2.0
4 501 502 B 503.00 504 B 504 B 512 506 B 513 1017 B 1.0 K 507 B 5 2.0
5 601 602 B 603.00 604 B 604 B 612 606 B 613 1.2 K 1.2 K 607 B 6 2.0
6 701 702 B - 704 B 704 B 712 706 B 713 1.4 K 1.4 K 707 B 7 2.0
total 2807 2.7 K - 2.8 K 2.8 K 2.9 K 2.8 K 2.9 K 8.4 K 5.7 K 2.8 K 28 3.0
flush 8
compact 5 6 B 7 B (size == estimated-debt, in = in-progress-bytes)
memtbl 12 11 B
zmemtbl 14 13 B
ztbl 16 15 B
bcache 2 1 B 42.9% (score == hit-rate)
tcache 18 17 B 48.7% (score == hit-rate)
titers 21
filter - - 47.4% (score == utility)
`
if s := "\n" + m.String(); expected != s {
t.Fatalf("expected%s\nbut found%s", expected, s)
}
}
func TestMetrics(t *testing.T) {
d, err := Open("", &Options{
FS: vfs.NewMem(),
L0CompactionThreshold: 8,
})
require.NoError(t, err)
defer func() {
require.NoError(t, d.Close())
}()
iters := make(map[string]*Iterator)
defer func() {
for _, i := range iters {
require.NoError(t, i.Close())
}
}()
datadriven.RunTest(t, "testdata/metrics", func(td *datadriven.TestData) string {
switch td.Cmd {
case "batch":
b := d.NewBatch()
if err := runBatchDefineCmd(td, b); err != nil {
return err.Error()
}
b.Commit(nil)
return ""
case "compact":
if err := runCompactCmd(td, d); err != nil {
return err.Error()
}
d.mu.Lock()
s := d.mu.versions.currentVersion().DebugString(base.DefaultFormatter)
d.mu.Unlock()
return s
case "flush":
if err := d.Flush(); err != nil {
return err.Error()
}
d.mu.Lock()
s := d.mu.versions.currentVersion().DebugString(base.DefaultFormatter)
d.mu.Unlock()
return s
case "iter-close":
if len(td.CmdArgs) != 1 {
return "iter-close <name>"
}
name := td.CmdArgs[0].String()
if iter := iters[name]; iter != nil {
if err := iter.Close(); err != nil {
return err.Error()
}
delete(iters, name)
} else {
return fmt.Sprintf("%s: not found", name)
}
// The deletion of obsolete files happens asynchronously when an iterator
// is closed. Wait for the obsolete tables to be deleted. Note that
// waiting on cleaner.cond isn't precisely correct.
d.mu.Lock()
for d.mu.cleaner.cleaning || len(d.mu.versions.obsoleteTables) > 0 {
d.mu.cleaner.cond.Wait()
}
d.mu.Unlock()
return ""
case "iter-new":
if len(td.CmdArgs) != 1 {
return "iter-new <name>"
}
name := td.CmdArgs[0].String()
if iter := iters[name]; iter != nil {
if err := iter.Close(); err != nil {
return err.Error()
}
}
iter := d.NewIter(nil)
// Some iterators (eg. levelIter) do not instantiate the underlying
// iterator until the first positioning call. Position the iterator
// so that levelIters will have loaded an sstable.
iter.First()
iters[name] = iter
return ""
case "metrics":
// The asynchronous loading of table stats can change metrics, so
// wait for all the tables' stats to be loaded.
d.mu.Lock()
d.waitTableStats()
d.mu.Unlock()
return d.Metrics().String()
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
func TestMetricsRedact(t *testing.T) {
const expected = `
__level_____count____size___score______in__ingest(sz_cnt)____move(sz_cnt)___write(sz_cnt)____read___r-amp___w-amp
WAL 0 0 B - 0 B - - - - 0 B - - - 0.0
0 0 0 B 0.00 0 B 0 B 0 0 B 0 0 B 0 0 B 0 0.0
1 0 0 B 0.00 0 B 0 B 0 0 B 0 0 B 0 0 B 0 0.0
2 0 0 B 0.00 0 B 0 B 0 0 B 0 0 B 0 0 B 0 0.0
3 0 0 B 0.00 0 B 0 B 0 0 B 0 0 B 0 0 B 0 0.0
4 0 0 B 0.00 0 B 0 B 0 0 B 0 0 B 0 0 B 0 0.0
5 0 0 B 0.00 0 B 0 B 0 0 B 0 0 B 0 0 B 0 0.0
6 0 0 B - 0 B 0 B 0 0 B 0 0 B 0 0 B 0 0.0
total 0 0 B - 0 B 0 B 0 0 B 0 0 B 0 0 B 0 0.0
flush 0
compact 0 0 B 0 B (size == estimated-debt, in = in-progress-bytes)
memtbl 0 0 B
zmemtbl 0 0 B
ztbl 0 0 B
bcache 0 0 B 0.0% (score == hit-rate)
tcache 0 0 B 0.0% (score == hit-rate)
titers 0
filter - - 0.0% (score == utility)
`
got := redact.Sprintf("%s", &Metrics{}).Redact()
if s := "\n" + got; expected != s {
t.Fatalf("expected%s\nbut found%s", expected, s)
}
}