github.com/ipld/go-ipld-prime@v0.21.0/node/tests/traversalBenchmarks.go (about) 1 package tests 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/ipld/go-ipld-prime/datamodel" 8 "github.com/ipld/go-ipld-prime/node/tests/corpus" 9 "github.com/ipld/go-ipld-prime/traversal" 10 ) 11 12 func BenchmarkSpec_Walk_Map3StrInt(b *testing.B, np datamodel.NodePrototype) { 13 node := mustNodeFromJsonString(np, corpus.Map3StrInt()) 14 sel := mustSelectorFromJsonString(np, `{"a":{">":{".":{}}}}`) 15 b.ResetTimer() 16 17 var visitCountSanityCheck int 18 for i := 0; i < b.N; i++ { 19 visitCountSanityCheck = 0 20 traversal.WalkMatching(node, sel, func(tp traversal.Progress, n datamodel.Node) error { 21 visitCountSanityCheck++ // this sanity check is sufficiently cheap to be worth it 22 return nil // no need to do anything here; just care about exercising the walk internals. 23 }) 24 } 25 if visitCountSanityCheck != 3 { 26 b.Fatalf("visitCountSanityCheck should be 3, got %d", visitCountSanityCheck) 27 } 28 } 29 30 func BenchmarkSpec_Walk_MapNStrMap3StrInt(b *testing.B, np datamodel.NodePrototype) { 31 sel := mustSelectorFromJsonString(np, `{"a":{">":{"a":{">":{".":{}}}}}}`) 32 33 for _, n := range []int{0, 1, 2, 4, 8, 16, 32} { 34 b.Run(fmt.Sprintf("n=%d", n), func(b *testing.B) { 35 node := mustNodeFromJsonString(np, corpus.MapNStrMap3StrInt(n)) 36 b.ResetTimer() 37 38 var visitCountSanityCheck int 39 for i := 0; i < b.N; i++ { 40 visitCountSanityCheck = 0 41 traversal.WalkMatching(node, sel, func(tp traversal.Progress, n datamodel.Node) error { 42 visitCountSanityCheck++ // this sanity check is sufficiently cheap to be worth it 43 return nil // no need to do anything here; just care about exercising the walk internals. 44 }) 45 } 46 if visitCountSanityCheck != 3*n { 47 b.Fatalf("visitCountSanityCheck should be %d, got %d", n*3, visitCountSanityCheck) 48 } 49 }) 50 } 51 }