Skip to content

Commit

Permalink
benchmark task and format results for markdown table
Browse files Browse the repository at this point in the history
  • Loading branch information
codeniko committed Mar 12, 2019
1 parent 8418acb commit 34446a1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 26 deletions.
14 changes: 14 additions & 0 deletions runBenchmarks.sh
Original file line number Diff line number Diff line change
@@ -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
25 changes: 20 additions & 5 deletions src/test/kotlin/com/nfeld/jsonpathlite/BenchmarkTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +22,8 @@ class BenchmarkTest : BaseTest() {

// disable JsonPath cache for fair benchmarks
CacheProvider.setCache(NOOPCache())

printReadmeFormat = System.getProperty("readmeFormat")?.toBoolean() ?: false
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/test/kotlin/com/nfeld/jsonpathlite/JsonPathTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import org.junit.jupiter.api.assertThrows

class JsonPathTest : BaseTest() {

// val result = com.jayway.jsonpath.JsonPath.parse(LARGE_JSON).read<List<String>>("$..name")

// JsonPath::parse related tests
@Test
fun shouldParseJsonObject() {
Expand Down
72 changes: 53 additions & 19 deletions tests_codecov.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34446a1

Please # to comment.