11package com.nfeld.jsonpathlite
22
3- import com.jayway.jsonpath.spi.cache.CacheProvider
43import com.jayway.jsonpath.spi.cache.NOOPCache
4+ import com.nfeld.jsonpathlite.cache.CacheProvider
55import com.nfeld.jsonpathlite.extension.read
66import org.json.JSONArray
77import org.junit.jupiter.api.BeforeAll
8- import org.junit.jupiter.api.Disabled
8+ import org.junit.jupiter.api.BeforeEach
99import org.junit.jupiter.api.Test
1010
1111class BenchmarkTest : BaseTest () {
@@ -20,11 +20,14 @@ class BenchmarkTest : BaseTest() {
2020 fun setupClass () {
2121 println (" Setting up BenchmarkTest" )
2222
23- // disable JsonPath cache for fair benchmarks
24- CacheProvider .setCache(NOOPCache ())
25-
2623 printReadmeFormat = System .getProperty(" readmeFormat" )?.toBoolean() ? : false
2724 }
25+
26+ @JvmStatic
27+ @BeforeEach
28+ fun resetCache () {
29+ resetCacheProvider()
30+ }
2831 }
2932
3033 private val timestamp: Long
@@ -59,12 +62,20 @@ class BenchmarkTest : BaseTest() {
5962 }
6063
6164 private fun runBenchmarksAndPrintResults (path : String , callsPerRun : Int = DEFAULT_CALLS_PER_RUN , runs : Int = DEFAULT_RUNS ) {
65+ // first benchmarks will be using caches
6266 val lite = benchmarkJsonPathLite(path, callsPerRun, runs)
6367 val other = benchmarkJsonPath(path, callsPerRun, runs)
68+
69+ // now disable caches
70+ CacheProvider .setCache(null )
71+ com.jayway.jsonpath.spi.cache.CacheProvider .setCache(NOOPCache ())
72+ val liteNoCache = benchmarkJsonPathLite(path, callsPerRun, runs)
73+ val otherNoCache = benchmarkJsonPath(path, callsPerRun, runs)
74+
6475 if (printReadmeFormat) {
65- println (" | $path | ${lite} ms | ${other} ms |" )
76+ println (" | $path | ${lite} ms | ${other} ms | $liteNoCache ms | $otherNoCache ms | " )
6677 } else {
67- println (" $path lite: ${lite} , jsonpath: ${other} " )
78+ println (" $path lite: ${lite} , jsonpath: ${other} Without caches: lite: ${liteNoCache} , jsonpath: ${otherNoCache} " )
6879 }
6980 }
7081
@@ -82,15 +93,23 @@ class BenchmarkTest : BaseTest() {
8293 fun benchmarkPathCompile () {
8394
8495 fun compile (path : String ) {
96+ // first with caches
8597 val lite = benchmark { JsonPath (path) }
8698 val other = benchmark { com.jayway.jsonpath.JsonPath .compile(path) }
99+
100+ // now disable caches
101+ CacheProvider .setCache(null )
102+ com.jayway.jsonpath.spi.cache.CacheProvider .setCache(NOOPCache ())
103+ val liteNoCache = benchmark { JsonPath (path) }
104+ val otherNoCache = benchmark { com.jayway.jsonpath.JsonPath .compile(path) }
105+
87106 val numTokens = PathCompiler .compile(path).size
88107 val name = " ${path.length} chars, $numTokens tokens"
89108
90109 if (printReadmeFormat) {
91- println (" | $name | ${lite} ms | ${other} ms |" )
110+ println (" | $name | ${lite} ms | ${other} ms | $liteNoCache ms | $otherNoCache ms | " )
92111 } else {
93- println (" $name lite: ${lite} , jsonpath: ${other} " )
112+ println (" $name lite: ${lite} , jsonpath: ${other} Without caches: lite: ${liteNoCache} , jsonpath: ${otherNoCache} " )
94113 }
95114 }
96115
@@ -142,11 +161,11 @@ class BenchmarkTest : BaseTest() {
142161
143162 @Test
144163 fun benchmarkMultiArrayAccess () {
145- runBenchmarksAndPrintResults(" $[0]['tags'][0,3, 5]" )
164+ runBenchmarksAndPrintResults(" $[0]['tags'][0,3,5]" )
146165 }
147166
148167 @Test
149168 fun benchmarkMultiObjectAccess () {
150- runBenchmarksAndPrintResults(" $[0]['latitude','longitude', 'isActive']" )
169+ runBenchmarksAndPrintResults(" $[0]['latitude','longitude','isActive']" )
151170 }
152171}
0 commit comments