Skip to content

Commit 34446a1

Browse files
committed
benchmark task and format results for markdown table
1 parent 8418acb commit 34446a1

File tree

4 files changed

+87
-26
lines changed

4 files changed

+87
-26
lines changed

runBenchmarks.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
FILE=/tmp/jsonpathlite
4+
FILE2=/tmp/jsonpathlite2
5+
./gradlew benchmark -DreadmeFormat > "$FILE"
6+
if [ $? -eq 0 ]; then
7+
cat "$FILE" | sed 's/^ *//' | grep '^|' > "$FILE2"
8+
9+
# print path times
10+
cat "$FILE2" | grep '^| \$'
11+
echo ''
12+
# print compile times
13+
cat "$FILE2" | grep -v '^| \$'
14+
fi

src/test/kotlin/com/nfeld/jsonpathlite/BenchmarkTest.kt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import org.junit.jupiter.api.BeforeAll
88
import org.junit.jupiter.api.Disabled
99
import org.junit.jupiter.api.Test
1010

11-
@Disabled
1211
class BenchmarkTest : BaseTest() {
1312

1413
companion object {
1514
private const val DEFAULT_RUNS = 30
1615
private const val DEFAULT_CALLS_PER_RUN = 80000
16+
private var printReadmeFormat = false
1717

1818
@JvmStatic
1919
@BeforeAll
@@ -22,6 +22,8 @@ class BenchmarkTest : BaseTest() {
2222

2323
// disable JsonPath cache for fair benchmarks
2424
CacheProvider.setCache(NOOPCache())
25+
26+
printReadmeFormat = System.getProperty("readmeFormat")?.toBoolean() ?: false
2527
}
2628
}
2729

@@ -59,7 +61,11 @@ class BenchmarkTest : BaseTest() {
5961
private fun runBenchmarksAndPrintResults(path: String, callsPerRun: Int = DEFAULT_CALLS_PER_RUN, runs: Int = DEFAULT_RUNS) {
6062
val lite = benchmarkJsonPathLite(path, callsPerRun, runs)
6163
val other = benchmarkJsonPath(path, callsPerRun, runs)
62-
println("$path lite: ${lite}, jsonpath: ${other}")
64+
if (printReadmeFormat) {
65+
println("| $path | ${lite} ms | ${other} ms |")
66+
} else {
67+
println("$path lite: ${lite}, jsonpath: ${other}")
68+
}
6369
}
6470

6571
@Test
@@ -74,20 +80,29 @@ class BenchmarkTest : BaseTest() {
7480

7581
@Test
7682
fun benchmarkPathCompile() {
83+
fun print(name: String, lite: Long, other: Long) {
84+
if (printReadmeFormat) {
85+
println("| $name | ${lite} ms | ${other} ms |")
86+
} else {
87+
println("$name lite: ${lite}, jsonpath: ${other}")
88+
}
89+
}
90+
7791
// short path length
7892
var lite = benchmark { JsonPath("$.hello['world']") }
7993
var other = benchmark { com.jayway.jsonpath.JsonPath.compile("$.hello['world']") }
80-
println("short path compile time lite: ${lite}, jsonpath: ${other}")
94+
print("short path compile time", lite, other)
8195

8296
// medium path length
8397
lite = benchmark { JsonPath("$[0].friends[1].other.a.b['c']") }
8498
other = benchmark { com.jayway.jsonpath.JsonPath.compile("$[0].friends[1].other.a.b['c']") }
85-
println("medium path compile time lite: ${lite}, jsonpath: ${other}")
99+
print("medium path compile time", lite, other)
100+
86101

87102
// long path length
88103
lite = benchmark { JsonPath("$[0].friends[1].other.a.b['c'][5].niko[2].hello.world[6][9][0].id") }
89104
other = benchmark { com.jayway.jsonpath.JsonPath.compile("$[0].friends[1].other.a.b['c'][5].niko[2].hello.world[6][9][0].id") }
90-
println("long path compile time lite: ${lite}, jsonpath: ${other}")
105+
print("long path compile time", lite, other)
91106
}
92107

93108
@Test

src/test/kotlin/com/nfeld/jsonpathlite/JsonPathTest.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import org.junit.jupiter.api.assertThrows
1010

1111
class JsonPathTest : BaseTest() {
1212

13-
// val result = com.jayway.jsonpath.JsonPath.parse(LARGE_JSON).read<List<String>>("$..name")
14-
1513
// JsonPath::parse related tests
1614
@Test
1715
fun shouldParseJsonObject() {

tests_codecov.gradle

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,70 @@ dependencies {
33
testImplementation 'com.jayway.jsonpath:json-path:2.4.0'
44
}
55

6+
ext {
7+
shouldBenchmark = false
8+
readmeFormat = false // pass -DreadmeFormat to format benchmark results to update readme
9+
}
10+
611
compileTestKotlin {
712
kotlinOptions.jvmTarget = "1.8"
813
}
914

10-
test {
11-
// Make this task never up to date, thus forcing rerun of all tests whenever task is run
12-
outputs.upToDateWhen { false }
15+
task benchmark {
16+
doLast {
17+
shouldBenchmark = true
18+
if (!System.getProperty("readmeFormat", "false").equals("false")) {
19+
readmeFormat = true
20+
}
21+
}
22+
}
23+
benchmark.finalizedBy(test)
1324

14-
// enable junit 5
15-
useJUnitPlatform()
25+
test {
26+
doFirst {
27+
if (shouldBenchmark) {
28+
if (readmeFormat) {
29+
jvmArgs '-DreadmeFormat=true'
30+
readmeFormat = false
31+
}
32+
filter {
33+
include("com/nfeld/jsonpathlite/BenchmarkTest.class")
34+
}
35+
testLogging {
36+
showStandardStreams = true
37+
}
38+
} else {
39+
filter {
40+
exclude("com/nfeld/jsonpathlite/BenchmarkTest.class")
41+
}
1642

17-
testLogging {
18-
// show test results for following events
19-
events 'PASSED', 'FAILED', 'SKIPPED'
43+
testLogging {
44+
// show test results for following events
45+
events 'PASSED', 'FAILED', 'SKIPPED'
2046

21-
// show printlines
22-
showStandardStreams = true
23-
}
47+
// show printlines
48+
showStandardStreams = true
49+
}
2450

25-
// show test summary at end
26-
afterSuite { desc, result ->
27-
if (!desc.parent) {
28-
println "\nTest result: ${result.resultType}"
29-
println "Test summary: ${result.testCount} tests, " +
30-
"${result.successfulTestCount} succeeded, " +
31-
"${result.failedTestCount} failed, " +
32-
"${result.skippedTestCount} skipped"
51+
// show test summary at end
52+
afterSuite { desc, result ->
53+
if (!desc.parent) {
54+
println "\nTest result: ${result.resultType}"
55+
println "Test summary: ${result.testCount} tests, " +
56+
"${result.successfulTestCount} succeeded, " +
57+
"${result.failedTestCount} failed, " +
58+
"${result.skippedTestCount} skipped"
59+
}
60+
}
3361
}
3462
}
3563

64+
// Make this task never up to date, thus forcing rerun of all tests whenever task is run
65+
outputs.upToDateWhen { false }
66+
67+
// enable junit 5
68+
useJUnitPlatform()
69+
3670
// code coverage
3771
jacoco {
3872
append = false

0 commit comments

Comments
 (0)