Last active
March 4, 2021 23:06
-
-
Save ProximaB/0cf43be8736d99c99e5bb798dbe9b1b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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