Files
allwpilib/benchmark/build.gradle
Gold856 2102a543d1 Change Java JSON to Avaje Jsonb (#8721)
Jackson is a very heavy library; it supports loads of features that we
don't need, and historically has caused issues due to long class loading
times (a little over 2 seconds to load AprilTagFieldLayout). This often
manifests as a help request in the form of "my robot disables when I do
X, but doesn't disable when doing X in subsequent attempts until code
restart." While SC has brought down Jackson loading times significantly,
with AprilTagFieldLayout loads taking only 330 milliseconds, that's
still a rather long delay, and while libraries should handle any JSON
loading ahead of time to prevent delays in auto/teleop, it would still
be good to make the worst case better to reduce user frustration.
Benchmarks indicate using [Avaje
Jsonb](https://github.com/avaje/avaje-jsonb) to load AprilTagFieldLayout
only takes ~70 ms, a fair chunk of which isn't actually in Avaje Jsonb
(~4 ms is spent on using getResourceAsStream to retrieve the JSON file,
~8 ms is spent on just loading the AprilTag class and its dependencies).

Note that all times listed are end-to-end, meaning nothing else was done
except for the operation being benchmarked, and doing arithmetic on them
can be flawed due to some classes being loaded twice, i.e.,
getResourceAsStream and `new AprilTag()` likely load some of the same
JDK classes and so subtracting both from the Avaje Jsonb load time is
likely slightly incorrect because class loading is being double counted.
For our purposes, it's likely accurate enough and is mostly just for
contextualization.

Benchmarks were run on a Raspberry Pi CM5 with 2 GB of RAM. Source code
for the
[results](https://github.com/user-attachments/files/26471452/benchmark.txt)
can be found in the "Fastjson2" commit
(2456d15ca8ebd17635e607cd40bf8816e77869a1).

Avaje Jsonb uses code generation via annotation processors to generate
the classes needed to do JSON serde and uses service providers to find
them, which will require downstream changes in robot projects, as the
different service providers in each library must be merged together for
Avaje Jsonb to function. We will use the Gradle shadow plugin, as its
already used by the installer and therefore adds zero additional
dependencies.
2026-04-10 23:21:00 -07:00

340 lines
13 KiB
Groovy

import org.wpilib.deployutils.deploy.target.RemoteTarget
import org.wpilib.deployutils.deploy.target.location.SshDeployLocation
import org.wpilib.deployutils.deploy.artifact.*
import org.gradle.internal.os.OperatingSystem
plugins {
id 'java'
id 'application'
id 'cpp'
id 'visual-studio'
}
apply plugin: 'org.wpilib.NativeUtils'
apply plugin: 'org.wpilib.DeployUtils'
apply from: "${rootDir}/shared/config.gradle"
application {
if (OperatingSystem.current().isMacOsX()) {
applicationDefaultJvmArgs = ['-XstartOnFirstThread']
}
}
ext {
sharedCvConfigs = [benchmarkCpp: []]
staticCvConfigs = [benchmarkCppStatic: []]
useJava = true
useCpp = true
skipDev = true
}
apply from: "${rootDir}/shared/opencv.gradle"
application {
mainClass = 'wpilib.robot.Main'
}
apply plugin: 'com.gradleup.shadow'
shadowJar {
mergeServiceFiles()
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
repositories {
maven {
url = 'https://frcmaven.wpi.edu/artifactory/ex-mvn'
}
}
dependencies {
implementation project(':apriltag')
implementation project(':cameraserver')
implementation project(':cscore')
implementation project(':epilogue-runtime')
implementation project(':hal')
implementation project(':ntcore')
implementation project(':wpilibj')
implementation project(':commandsv2')
implementation project(':wpimath')
implementation project(':wpinet')
implementation project(':wpiunits')
implementation project(':wpiutil')
annotationProcessor project(':epilogue-processor')
implementation libs.jmh.core
annotationProcessor libs.jmh.annotationprocessor
}
tasks.withType(com.github.spotbugs.snom.SpotBugsTask).configureEach {
onlyIf { false }
}
deploy {
targets {
systemcore(RemoteTarget) {
directory = '/home/systemcore'
maxChannels = 4
locations {
mdns(SshDeployLocation) {
address = "robot.local"
user = 'systemcore'
password = 'systemcore'
ipv6 = false
}
usb(SshDeployLocation) {
address = "172.26.0.1"
user = 'systemcore'
password = 'systemcore'
ipv6 = false
}
}
timeout = 7
def remote = it
artifacts {
all {
postdeploy << { ctx ->
ctx.execute("sync")
ctx.execute("sudo ldconfig /home/systemcore/wpilib/third-party/lib")
}
}
benchmarkCpp(NativeExecutableArtifact) {
libraryDirectory = '/home/systemcore/wpilib/third-party/lib'
def excludes = getLibraryFilter().getExcludes()
excludes.add('**/*.so.debug')
excludes.add('**/*.so.*.debug')
postdeploy << { ctx ->
ctx.execute("echo '/home/systemcore/benchmarkCpp' > /home/systemcore/robotCommand")
ctx.execute("chmod +x /home/systemcore/robotCommand; chown systemcore /home/systemcore/robotCommand")
ctx.execute("setcap cap_sys_nice+eip \"/home/systemcore/benchmarkCpp\"")
ctx.execute('chmod +x benchmarkCpp')
}
}
benchmarkCppStatic(NativeExecutableArtifact) {
libraryDirectory = '/home/systemcore/wpilib/third-party/lib'
postdeploy << { ctx ->
ctx.execute("echo '/home/systemcore/benchmarkCppStatic' > /home/systemcore/robotCommand")
ctx.execute("chmod +x /home/systemcore/robotCommand; chown systemcore /home/systemcore/robotCommand")
ctx.execute("setcap cap_sys_nice+eip \"/home/systemcore/benchmarkCppStatic\"")
ctx.execute('chmod +x benchmarkCppStatic')
}
}
benchmarkCppJava(NativeExecutableArtifact) {
libraryDirectory = '/home/systemcore/wpilib/third-party/lib'
def excludes = getLibraryFilter().getExcludes()
excludes.add('**/*.so.debug')
excludes.add('**/*.so.*.debug')
}
benchmarkJava(JavaArtifact) {
jarTask = shadowJar
postdeploy << { ctx ->
ctx.execute("echo 'java -XX:+UseSerialGC -Djava.library.path=/home/systemcore/wpilib/third-party/lib -Djava.lang.invoke.stringConcat=BC_SB -jar /home/systemcore/benchmark-all.jar' > /home/systemcore/robotCommand")
ctx.execute("chmod +x /home/systemcore/robotCommand; chown systemcore /home/systemcore/robotCommand")
}
}
}
}
}
}
// Prevent the eclipse compiler (used by the VS Code extension for intellisense and debugging)
// from generating bad class files from annotation processors like Epilogue
eclipse {
classpath {
containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
file.whenMerged { cp ->
def entries = cp.entries;
def src = new org.gradle.plugins.ide.eclipse.model.SourceFolder('build/generated/sources/annotationProcessor/java/main/', null)
entries.add(src)
}
}
}
tasks.register('deployJava') {
try {
dependsOn tasks.named('deployjresystemcore')
dependsOn tasks.named('deploybenchmarkJavasystemcore')
dependsOn tasks.named('deploybenchmarkCppJavasystemcore') // Deploying shared C++ is how to get the Java shared libraries.
} catch (ignored) {
}
}
tasks.register('deployShared') {
try {
dependsOn tasks.named('deploybenchmarkCppsystemcore')
} catch (ignored) {
}
}
tasks.register('deployStatic') {
try {
dependsOn tasks.named('deploybenchmarkCppStaticsystemcore')
} catch (ignored) {
}
}
model {
components {
benchmarkCpp(NativeExecutableSpec) {
if (project.hasProperty('ciDebugOnly')) {
targetBuildTypes 'debug'
} else {
targetBuildTypes 'release'
}
sources {
cpp {
source {
srcDirs = [
'src/main/native/cpp',
'src/main/native/thirdparty/benchmark/src'
]
includes = ['**/*.cpp']
}
exportedHeaders {
srcDirs = [
'src/main/native/include',
'src/main/native/thirdparty/benchmark/include',
'src/main/native/thirdparty/benchmark/src'
]
includes = ['**/*.h']
}
}
}
binaries.all { binary ->
if (binary.targetPlatform.name == nativeUtils.wpi.platforms.systemcore) {
if (binary.buildType.name == 'debug') {
deploy.targets.systemcore.artifacts.benchmarkCpp.binary = binary
deploy.targets.systemcore.artifacts.benchmarkCppJava.binary = binary
}
}
lib project: ':apriltag', library: 'apriltag', linkage: 'shared'
lib project: ':cameraserver', library: 'cameraserver', linkage: 'shared'
lib project: ':cscore', library: 'cscore', linkage: 'shared'
lib project: ':cscore', library: 'cscoreJNIShared', linkage: 'shared'
project(':hal').addHalDependency(binary, 'shared')
project(':hal').addHalJniDependency(binary)
project(':ntcore').addNtcoreDependency(binary, 'shared')
project(':ntcore').addNtcoreJniDependency(binary)
lib project: ':wpilibc', library: 'wpilibc', linkage: 'shared'
lib project: ':commandsv2', library: 'commandsv2', linkage: 'shared'
lib project: ':wpimath', library: 'wpimath', linkage: 'shared'
lib project: ':wpimath', library: 'wpimathJNIShared', linkage: 'shared'
lib project: ':wpinet', library: 'wpinet', linkage: 'shared'
lib project: ':wpinet', library: 'wpinetJNIShared', linkage: 'shared'
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
lib project: ':wpiutil', library: 'wpiutilJNIShared', linkage: 'shared'
if (binary.targetPlatform.operatingSystem.isWindows()) {
// Shlwapi.lib is needed for SHGetValueA() inside thirdparty benchmark
binary.linker.args << "Shlwapi.lib"
}
binary.cppCompiler.define 'benchmark_EXPORTS'
}
}
benchmarkCppStatic(NativeExecutableSpec) {
if (project.hasProperty('ciDebugOnly')) {
targetBuildTypes 'debug'
} else {
targetBuildTypes 'release'
}
nativeUtils.excludeBinariesFromStrip(it)
sources {
cpp {
source {
srcDirs = [
'src/main/native/cpp',
'src/main/native/thirdparty/benchmark/src'
]
includes = ['**/*.cpp']
}
exportedHeaders {
srcDirs = [
'src/main/native/include',
'src/main/native/thirdparty/benchmark/include',
'src/main/native/thirdparty/benchmark/src'
]
includes = ['**/*.h']
}
}
}
binaries.all { binary ->
if (binary.targetPlatform.name == nativeUtils.wpi.platforms.systemcore) {
if (binary.buildType.name == 'debug') {
deploy.targets.systemcore.artifacts.benchmarkCppStatic.binary = binary
}
}
lib project: ':apriltag', library: 'apriltag', linkage: 'static'
lib project: ':cameraserver', library: 'cameraserver', linkage: 'static'
lib project: ':cscore', library: 'cscore', linkage: 'static'
project(':hal').addHalDependency(binary, 'static')
project(':ntcore').addNtcoreDependency(binary, 'static')
lib project: ':wpilibc', library: 'wpilibc', linkage: 'static'
lib project: ':commandsv2', library: 'commandsv2', linkage: 'static'
lib project: ':wpimath', library: 'wpimath', linkage: 'static'
lib project: ':wpinet', library: 'wpinet', linkage: 'static'
lib project: ':wpiutil', library: 'wpiutil', linkage: 'static'
if (binary.targetPlatform.operatingSystem.isWindows()) {
// Shlwapi.lib is needed for SHGetValueA() inside thirdparty benchmark
binary.linker.args << "Shlwapi.lib"
}
binary.cppCompiler.define 'benchmark_EXPORTS'
binary.cppCompiler.define 'BENCHMARK_STATIC_DEFINE'
}
}
all {
it.sources.each {
it.exportedHeaders {
srcDirs 'src/main/native/thirdparty/benchmark/include'
}
}
}
}
tasks {
def c = $.components
project.tasks.create('runCpp', Exec) {
group = 'WPILib'
description = "Run the benchmarkCpp executable"
def found = false
def systemArch = getCurrentArch()
c.each {
if (it in NativeExecutableSpec && it.name == "benchmarkCpp") {
it.binaries.each {
if (!found) {
def arch = it.targetPlatform.name
if (arch == systemArch) {
dependsOn it.tasks.install
commandLine it.tasks.install.runScriptFile.get().asFile.toString()
def filePath = it.tasks.install.installDirectory.get().toString() + File.separatorChar + 'lib'
run.dependsOn it.tasks.install
run.systemProperty 'java.library.path', filePath
found = true
}
}
}
}
}
}
installAthena(Task) {
$.binaries.each {
if (it in NativeExecutableBinarySpec && it.targetPlatform.name == nativeUtils.wpi.platforms.systemcore && it.component.name == 'benchmarkCpp') {
dependsOn it.tasks.install
}
}
}
installAthenaStatic(Task) {
$.binaries.each {
if (it in NativeExecutableBinarySpec && it.targetPlatform.name == nativeUtils.wpi.platforms.systemcore && it.component.name == 'benchmarkCppStatic') {
dependsOn it.tasks.install
}
}
}
}
}