diff --git a/runBenchmarks.sh b/runBenchmarks.sh new file mode 100755 index 0000000..1c36791 --- /dev/null +++ b/runBenchmarks.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +FILE=/tmp/jsonpathlite +FILE2=/tmp/jsonpathlite2 +./gradlew benchmark -DreadmeFormat > "$FILE" +if [ $? -eq 0 ]; then + cat "$FILE" | sed 's/^ *//' | grep '^|' > "$FILE2" + + # print path times + cat "$FILE2" | grep '^| \$' + echo '' + # print compile times + cat "$FILE2" | grep -v '^| \$' +fi diff --git a/src/test/kotlin/com/nfeld/jsonpathlite/BenchmarkTest.kt b/src/test/kotlin/com/nfeld/jsonpathlite/BenchmarkTest.kt index ff26566..e079634 100644 --- a/src/test/kotlin/com/nfeld/jsonpathlite/BenchmarkTest.kt +++ b/src/test/kotlin/com/nfeld/jsonpathlite/BenchmarkTest.kt @@ -8,12 +8,12 @@ import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test -@Disabled class BenchmarkTest : BaseTest() { companion object { private const val DEFAULT_RUNS = 30 private const val DEFAULT_CALLS_PER_RUN = 80000 + private var printReadmeFormat = false @JvmStatic @BeforeAll @@ -22,6 +22,8 @@ class BenchmarkTest : BaseTest() { // disable JsonPath cache for fair benchmarks CacheProvider.setCache(NOOPCache()) + + printReadmeFormat = System.getProperty("readmeFormat")?.toBoolean() ?: false } } @@ -59,7 +61,11 @@ class BenchmarkTest : BaseTest() { private fun runBenchmarksAndPrintResults(path: String, callsPerRun: Int = DEFAULT_CALLS_PER_RUN, runs: Int = DEFAULT_RUNS) { val lite = benchmarkJsonPathLite(path, callsPerRun, runs) val other = benchmarkJsonPath(path, callsPerRun, runs) - println("$path lite: ${lite}, jsonpath: ${other}") + if (printReadmeFormat) { + println("| $path | ${lite} ms | ${other} ms |") + } else { + println("$path lite: ${lite}, jsonpath: ${other}") + } } @Test @@ -74,20 +80,29 @@ class BenchmarkTest : BaseTest() { @Test fun benchmarkPathCompile() { + fun print(name: String, lite: Long, other: Long) { + if (printReadmeFormat) { + println("| $name | ${lite} ms | ${other} ms |") + } else { + println("$name lite: ${lite}, jsonpath: ${other}") + } + } + // short path length var lite = benchmark { JsonPath("$.hello['world']") } var other = benchmark { com.jayway.jsonpath.JsonPath.compile("$.hello['world']") } - println("short path compile time lite: ${lite}, jsonpath: ${other}") + print("short path compile time", lite, other) // medium path length lite = benchmark { JsonPath("$[0].friends[1].other.a.b['c']") } other = benchmark { com.jayway.jsonpath.JsonPath.compile("$[0].friends[1].other.a.b['c']") } - println("medium path compile time lite: ${lite}, jsonpath: ${other}") + print("medium path compile time", lite, other) + // long path length lite = benchmark { JsonPath("$[0].friends[1].other.a.b['c'][5].niko[2].hello.world[6][9][0].id") } other = benchmark { com.jayway.jsonpath.JsonPath.compile("$[0].friends[1].other.a.b['c'][5].niko[2].hello.world[6][9][0].id") } - println("long path compile time lite: ${lite}, jsonpath: ${other}") + print("long path compile time", lite, other) } @Test diff --git a/src/test/kotlin/com/nfeld/jsonpathlite/JsonPathTest.kt b/src/test/kotlin/com/nfeld/jsonpathlite/JsonPathTest.kt index e89bbe9..3fb8cc3 100644 --- a/src/test/kotlin/com/nfeld/jsonpathlite/JsonPathTest.kt +++ b/src/test/kotlin/com/nfeld/jsonpathlite/JsonPathTest.kt @@ -10,8 +10,6 @@ import org.junit.jupiter.api.assertThrows class JsonPathTest : BaseTest() { - // val result = com.jayway.jsonpath.JsonPath.parse(LARGE_JSON).read>("$..name") - // JsonPath::parse related tests @Test fun shouldParseJsonObject() { diff --git a/tests_codecov.gradle b/tests_codecov.gradle index 33734bf..5d49c0b 100644 --- a/tests_codecov.gradle +++ b/tests_codecov.gradle @@ -3,36 +3,70 @@ dependencies { testImplementation 'com.jayway.jsonpath:json-path:2.4.0' } +ext { + shouldBenchmark = false + readmeFormat = false // pass -DreadmeFormat to format benchmark results to update readme +} + compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } -test { - // Make this task never up to date, thus forcing rerun of all tests whenever task is run - outputs.upToDateWhen { false } +task benchmark { + doLast { + shouldBenchmark = true + if (!System.getProperty("readmeFormat", "false").equals("false")) { + readmeFormat = true + } + } +} +benchmark.finalizedBy(test) - // enable junit 5 - useJUnitPlatform() +test { + doFirst { + if (shouldBenchmark) { + if (readmeFormat) { + jvmArgs '-DreadmeFormat=true' + readmeFormat = false + } + filter { + include("com/nfeld/jsonpathlite/BenchmarkTest.class") + } + testLogging { + showStandardStreams = true + } + } else { + filter { + exclude("com/nfeld/jsonpathlite/BenchmarkTest.class") + } - testLogging { - // show test results for following events - events 'PASSED', 'FAILED', 'SKIPPED' + testLogging { + // show test results for following events + events 'PASSED', 'FAILED', 'SKIPPED' - // show printlines - showStandardStreams = true - } + // show printlines + showStandardStreams = true + } - // show test summary at end - afterSuite { desc, result -> - if (!desc.parent) { - println "\nTest result: ${result.resultType}" - println "Test summary: ${result.testCount} tests, " + - "${result.successfulTestCount} succeeded, " + - "${result.failedTestCount} failed, " + - "${result.skippedTestCount} skipped" + // show test summary at end + afterSuite { desc, result -> + if (!desc.parent) { + println "\nTest result: ${result.resultType}" + println "Test summary: ${result.testCount} tests, " + + "${result.successfulTestCount} succeeded, " + + "${result.failedTestCount} failed, " + + "${result.skippedTestCount} skipped" + } + } } } + // Make this task never up to date, thus forcing rerun of all tests whenever task is run + outputs.upToDateWhen { false } + + // enable junit 5 + useJUnitPlatform() + // code coverage jacoco { append = false