this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

mst: add BenchmarkDiffTrees

To establish a baseline for future DiffTrees optimizations.

authored by

Brad Fitzpatrick and committed by
bnewbold
e055d34f 0430d074

+56 -3
+56 -3
mst/mst_test.go
··· 361 361 return out 362 362 } 363 363 364 - func cidMapToMst(t *testing.T, bs blockstore.Blockstore, m map[string]cid.Cid) *MerkleSearchTree { 364 + func cidMapToMst(t testing.TB, bs blockstore.Blockstore, m map[string]cid.Cid) *MerkleSearchTree { 365 365 cst := util.CborStore(bs) 366 366 mt := createMST(cst, cid.Undef, []nodeEntry{}, -1) 367 367 ··· 377 377 return mt 378 378 } 379 379 380 - func mustCidTree(t *testing.T, tree *MerkleSearchTree) cid.Cid { 380 + func mustCidTree(t testing.TB, tree *MerkleSearchTree) cid.Cid { 381 381 c, err := tree.GetPointer(context.TODO()) 382 382 if err != nil { 383 383 t.Fatal(err) ··· 389 389 return blockstore.NewBlockstore(datastore.NewMapDatastore()) 390 390 } 391 391 392 - func testMapDiffs(t *testing.T, a, b map[string]string) { 392 + func testMapDiffs(t testing.TB, a, b map[string]string) { 393 393 amc := mapToCidMap(a) 394 394 bmc := mapToCidMap(b) 395 395 ··· 546 546 _ = leadingZerosOnHash("some.key.prefix/key.bar123456789012334556") 547 547 } 548 548 } 549 + 550 + func BenchmarkDiffTrees(b *testing.B) { 551 + b.ReportAllocs() 552 + const size = 10000 553 + ma := map[string]string{} 554 + for i := 0; i < size; i++ { 555 + ma[fmt.Sprintf("num/%02d", i)] = fmt.Sprint(i) 556 + } 557 + // And then mess with half of the items of the first half of it. 558 + mb := maps.Clone(ma) 559 + for i := 0; i < size/2; i++ { 560 + switch i % 4 { 561 + case 0, 1: 562 + case 2: 563 + delete(mb, fmt.Sprintf("num/%02d", i)) 564 + case 3: 565 + ma[fmt.Sprintf("num/%02d", i)] = fmt.Sprint(i + 1) 566 + } 567 + } 568 + 569 + amc := mapToCidMap(ma) 570 + bmc := mapToCidMap(mb) 571 + 572 + want := diffMaps(amc, bmc) 573 + 574 + bs := memBs() 575 + 576 + msta := cidMapToMst(b, bs, amc) 577 + mstb := cidMapToMst(b, bs, bmc) 578 + 579 + cida := mustCidTree(b, msta) 580 + cidb := mustCidTree(b, mstb) 581 + 582 + b.ResetTimer() 583 + 584 + var diffs []*DiffOp 585 + var err error 586 + for i := 0; i < b.N; i++ { 587 + diffs, err = DiffTrees(context.TODO(), bs, cida, cidb) 588 + if err != nil { 589 + b.Fatal(err) 590 + } 591 + } 592 + 593 + if !sort.SliceIsSorted(diffs, func(i, j int) bool { 594 + return diffs[i].Rpath < diffs[j].Rpath 595 + }) { 596 + b.Log("diff algo did not produce properly sorted diff") 597 + } 598 + if !compareDiffs(diffs, want) { 599 + b.Fatal("diffs not equal") 600 + } 601 + }