Skip to content

Instantly share code, notes, and snippets.

@ProximaB
Last active March 4, 2021 23:06
Show Gist options
  • Save ProximaB/0cf43be8736d99c99e5bb798dbe9b1b3 to your computer and use it in GitHub Desktop.
Save ProximaB/0cf43be8736d99c99e5bb798dbe9b1b3 to your computer and use it in GitHub Desktop.
package benchmark
import (
"github.com/JumboInteractiveLimited/jsonpath"
"github.com/loov/hrtime/hrtesting"
"github.com/tidwall/gjson"
"io/ioutil"
"strings"
"testing"
)
const testJson string = `{
"Items":
[
{
"title": "A Midsummer Night's Dream",
"tags":[
"comedy",
"shakespeare",
"play"
]
},{
"title": "A Tale of Two Cities",
"tags":[
"french",
"revolution",
"london"
]
}
]
}`
func BenchmarkJumboInteractiveLimited(b *testing.B) {
json := strings.NewReader(testJson)
jsonBytes, err := ioutil.ReadAll(json)
if err != nil {
panic(err)
}
bench := hrtesting.NewBenchmark(b)
defer bench.Report()
for bench.Next() {
paths, err := jsonpath.ParsePaths(`$.Items[*]?(@.title == "A Tale of Two Cities").tags+`)
if err != nil {
panic(err)
}
eval, err := jsonpath.EvalPathsInBytes(jsonBytes, paths)
if err != nil {
panic(err)
}
for {
if _, ok := eval.Next(); ok {
//fmt.Println(result.Pretty(true))
} else {
break
}
}
if eval.Error != nil {
panic(eval.Error)
}
}
}
func BenchmarkGjson(b *testing.B) {
json := strings.NewReader(testJson)
jsonBytes, err := ioutil.ReadAll(json)
if err != nil {
panic(err)
}
bench := hrtesting.NewBenchmark(b)
defer bench.Report()
for bench.Next() {
gjson.GetBytes(jsonBytes, `Items.#(title="A Tale of Two Cities").tags|@pretty`)
//fmt.Println(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment