MCPcopy
hub / github.com/etcd-io/bbolt / TestBucket_Stats

Function TestBucket_Stats

bucket_test.go:1232–1337  ·  view source on GitHub ↗

Ensure a bucket can calculate stats.

(t *testing.T)

Source from the content-addressed store, hash-verified

1230
1231// Ensure a bucket can calculate stats.
1232func TestBucket_Stats(t *testing.T) {
1233 if testing.Short() {
1234 t.Skip("skipping test in short mode")
1235 }
1236
1237 db := btesting.MustCreateDB(t)
1238
1239 // Add bucket with fewer keys but one big value.
1240 bigKey := []byte("really-big-value")
1241 for i := 0; i < 500; i++ {
1242 if err := db.Update(func(tx *bolt.Tx) error {
1243 b, err := tx.CreateBucketIfNotExists([]byte("woojits"))
1244 if err != nil {
1245 t.Fatal(err)
1246 }
1247
1248 if err := b.Put([]byte(fmt.Sprintf("%03d", i)), []byte(strconv.Itoa(i))); err != nil {
1249 t.Fatal(err)
1250 }
1251 return nil
1252 }); err != nil {
1253 t.Fatal(err)
1254 }
1255 }
1256 longKeyLength := 10*db.Info().PageSize + 17
1257 if err := db.Update(func(tx *bolt.Tx) error {
1258 if err := tx.Bucket([]byte("woojits")).Put(bigKey, []byte(strings.Repeat("*", longKeyLength))); err != nil {
1259 t.Fatal(err)
1260 }
1261 return nil
1262 }); err != nil {
1263 t.Fatal(err)
1264 }
1265
1266 db.MustCheck()
1267
1268 pageSize2stats := map[int]bolt.BucketStats{
1269 4096: {
1270 BranchPageN: 1,
1271 BranchOverflowN: 0,
1272 LeafPageN: 7,
1273 LeafOverflowN: 10,
1274 KeyN: 501,
1275 Depth: 2,
1276 BranchAlloc: 4096,
1277 BranchInuse: 149,
1278 LeafAlloc: 69632,
1279 LeafInuse: 0 +
1280 7*16 + // leaf page header (x LeafPageN)
1281 501*16 + // leaf elements
1282 500*3 + len(bigKey) + // leaf keys
1283 1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ...
1284 BucketN: 1,
1285 InlineBucketN: 0,
1286 InlineBucketInuse: 0},
1287 16384: {
1288 BranchPageN: 1,
1289 BranchOverflowN: 0,

Callers

nothing calls this directly

Calls 10

MustCreateDBFunction · 0.92
UpdateMethod · 0.80
MustCheckMethod · 0.80
ViewMethod · 0.80
FatalMethod · 0.65
InfoMethod · 0.65
PutMethod · 0.45
BucketMethod · 0.45
StatsMethod · 0.45

Tested by

no test coverage detected