Skip to content

Commit

Permalink
add GetMany benchs
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jan 16, 2024
1 parent 45f273e commit 28cb989
Showing 1 changed file with 118 additions and 2 deletions.
120 changes: 118 additions & 2 deletions external_jsonlib_test/benchmark_test/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,124 @@ func BenchmarkNodeGet(b *testing.B) {
})
}

func BenchmarkNodeGetMany(b *testing.B) {
b.SetBytes(int64(len(TwitterJson)))
b.Run("gjson", func(b *testing.B) {
for i := 0; i < b.N; i++ {
nodes := gjson.GetMany(TwitterJson, "statuses.0", "statuses.3", "statuses.100")
if !nodes[0].Exists() {
b.Fatal()
}
if !nodes[1].Exists() {
b.Fatal()
}
if nodes[2].Exists() {
b.Fatal()
}
}
})
b.Run("Node", func(b *testing.B) {
for i := 0; i < b.N; i++ {
s := ast.NewSearcher(TwitterJson)
n, _ := s.GetByPath("statuses")
nodes := []*ast.Node{}
x := n.Index(0)
nodes = append(nodes, x)
x = n.Index(3)
nodes = append(nodes, x)
x = n.Index(100)
nodes = append(nodes, x)
if !nodes[0].Exists() {
b.Fatal()
}
if !nodes[1].Exists() {
b.Fatal()
}
if nodes[2].Exists() {
b.Fatal()
}
}
})
b.Run("Value", func(b *testing.B) {
for i := 0; i < b.N; i++ {
s := ast.NewSearcher(TwitterJson)
n, _ := s.GetValueByPath("statuses")
nodes := [3]ast.Value{}
err := n.IndexMany([]int{0,3,100}, nodes[:])
if err != nil {
b.Fatal(err)
}
if !nodes[0].Exists() {
b.Fatal()
}
if !nodes[1].Exists() {
b.Fatal()
}
if nodes[2].Exists() {
b.Fatal()
}
}
})
}

func TestSetNotExist(t *testing.T) {
js, err := sjson.Set(`[]`, "0", 1)
if err != nil {
t.Fatal(err)
}
println(js)
js, err = sjson.Set(js, "10", 1)
if err != nil {
t.Fatal(err)
}
println(js)
}

func BenchmarkNodeSetMany(b *testing.B) {
b.SetBytes(int64(len(TwitterJson)))
b.Run("sjson", func(b *testing.B) {
for i := 0; i < b.N; i++ {
js, err := sjson.Set(TwitterJson, "statuses.0", 1)
if err != nil {
b.Fatal(err)
}
_, err = sjson.Set(js, "statuses.3", 1)
if err != nil {
b.Fatal(err)
}
// _, err = sjson.Set(js, "statuses.100", 1)
// if err != nil {
// b.Fatal(err)
// }
}
})
b.Run("Node", func(b *testing.B) {
for i := 0; i < b.N; i++ {
s := ast.NewSearcher(TwitterJson)
n, _ := s.GetByPath("statuses")
exist, err := n.SetByIndex(0, ast.NewAny(1))
if !exist || err != nil {
b.Fatal()
}
exist, err = n.SetByIndex(3, ast.NewAny(1))
if !exist || err != nil {
b.Fatal()
}
}
})
b.Run("Value", func(b *testing.B) {
for i := 0; i < b.N; i++ {
s := ast.NewSearcher(TwitterJson)
n, _ := s.GetValueByPath("statuses")
nodes := []ast.Value{ast.NewValue(1), ast.NewValue(2)}
exist, err := n.SetManyByIndex([]int{0,3}, nodes)
if !exist || err != nil {
b.Fatal()
}
}
})
}

func BenchmarkSet(b *testing.B) {
b.SetBytes(int64(len(TwitterJson)))
b.Run("sjson", func(b *testing.B) {
Expand Down Expand Up @@ -162,8 +280,6 @@ func BenchmarkSet(b *testing.B) {
})
}



func BenchmarkGetOne_Parallel_Sonic(b *testing.B) {
b.SetBytes(int64(len(TwitterJson)))
b.RunParallel(func(pb *testing.PB) {
Expand Down

0 comments on commit 28cb989

Please sign in to comment.