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

Function TestBucket_Stats_RandomFill

bucket_test.go:1340–1404  ·  view source on GitHub ↗

Ensure a bucket with random insertion utilizes fill percentage correctly.

(t *testing.T)

Source from the content-addressed store, hash-verified

1338
1339// Ensure a bucket with random insertion utilizes fill percentage correctly.
1340func TestBucket_Stats_RandomFill(t *testing.T) {
1341 if testing.Short() {
1342 t.Skip("skipping test in short mode.")
1343 } else if os.Getpagesize() != 4096 {
1344 t.Skip("invalid page size for test")
1345 }
1346
1347 db := btesting.MustCreateDB(t)
1348
1349 // Add a set of values in random order. It will be the same random
1350 // order so we can maintain consistency between test runs.
1351 var count int
1352 rand := rand.New(rand.NewSource(42))
1353 for _, i := range rand.Perm(1000) {
1354 if err := db.Update(func(tx *bolt.Tx) error {
1355 b, err := tx.CreateBucketIfNotExists([]byte("woojits"))
1356 if err != nil {
1357 t.Fatal(err)
1358 }
1359 b.FillPercent = 0.9
1360 for _, j := range rand.Perm(100) {
1361 index := (j * 10000) + i
1362 if err := b.Put([]byte(fmt.Sprintf("%d000000000000000", index)), []byte("0000000000")); err != nil {
1363 t.Fatal(err)
1364 }
1365 count++
1366 }
1367 return nil
1368 }); err != nil {
1369 t.Fatal(err)
1370 }
1371 }
1372
1373 db.MustCheck()
1374
1375 if err := db.View(func(tx *bolt.Tx) error {
1376 stats := tx.Bucket([]byte("woojits")).Stats()
1377 if stats.KeyN != 100000 {
1378 t.Fatalf("unexpected KeyN: %d", stats.KeyN)
1379 }
1380
1381 if stats.BranchPageN != 98 {
1382 t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN)
1383 } else if stats.BranchOverflowN != 0 {
1384 t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN)
1385 } else if stats.BranchInuse != 130984 {
1386 t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse)
1387 } else if stats.BranchAlloc != 401408 {
1388 t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc)
1389 }
1390
1391 if stats.LeafPageN != 3412 {
1392 t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN)
1393 } else if stats.LeafOverflowN != 0 {
1394 t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN)
1395 } else if stats.LeafInuse != 4742482 {
1396 t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse)
1397 } else if stats.LeafAlloc != 13975552 {

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected