We provide the code for a small set of benchmarks to compare various approaches to solving common JNI use-cases and then present the results.
The benchmarks at present are:
-
com.evolvedbinary.jnibench.common.call - Benchmarks for Creating Objects with JNI (results).
-
com.evolvedbinary.jnibench.common.array - Benchmarks for Passing Arrays with JNI (results).
-
com.evolvedbinary.jnibench.common.bytearray, and com.evolvedbinary.jnibench.common.getputjni - Benchmarks for JNI Data Transfer.
If you want to run the code yourself, you need to have Java 8, Maven 3, and a C++ compiler that supports the C++ 11 standard. You can then simply run:
$ mvn clean compile package
In the target/
sub-directory, you will then find both a
jni-benchmarks-1.0.0-SNAPSHOT-application
folder and a
jni-benchmarks-1.0.0-SNAPSHOT-application.zip
file, you can use either of
these. They both contain bash scripts in their bin/
sub-folders for Mac,
Linux, Unix and batch scripts for Windows. These scripts will run a single
iteration of the benchmark.
If you want to run multiple iterations and get a CSV file of the results, you
can use benchmark-100.sh
and/or benchmark-100-with-close.sh
, or
array-benchmark-100.sh
.
We have support for running the tests via JMH, see jmh-benchmarks.sh
. You can
also pass --help
to the script to see JMH options.
There are two benchmarks, which are currently available only via JMH:
ByteArrayFromNativeBenchmark and ByteArrayToNativeBenchmark. They can be run
multiple times using jmh-benchmarks-parametrized.sh
with:
./jmh-benchmarks-parametrized.sh -i 10 -b ByteArrayToNativeBenchmark -o results/ -f csv
The above command will run JMH with ByteArrayToNativeBenchmark
benchmarks 10
times and store result in CSV files in results
directory. You can also pass --help
to the script to see additional JMH options that can be used.
Results can then be plotted using the process_byte_array_benchmarks_results.py
script. For example, to produce results for the ByteArrayToNativeBenchmark
benchmarks, you can run:
python3 process_byte_array_benchmarks_results.py -p results/ --param-name "Param: keySize" --chart-title "Performance comparison of passing byte array with {} bytes via JNI"
Command line parameter p
expects a path to the directory with the JMH result CSV files from running the benchmarks with jmh-benchmarks-parametrized.sh
.
The {}
in the chart-title
parameter will be replaced by the value from the param-name
column.
- Java Foreign Interface prototype and performance results in RocksJava (i.e. Panama) - https://rocksdb.org/blog/2024/02/20/foreign-function-interface.html
- RocksJava API Performance Improvements - https://rocksdb.org/blog/2023/11/06/java-jni-benchmarks.html