Skip to content

Commit

Permalink
Merge pull request #232 from theseus-rs/add-vm-benchmarks
Browse files Browse the repository at this point in the history
test: add vm benchmarks
  • Loading branch information
brianheineman authored Jan 22, 2025
2 parents 2fd8ee5 + fbee0c5 commit 3ac160b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 488 deletions.
262 changes: 13 additions & 249 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ async-recursion = "1.1.1"
bitflags = "2.8.0"
byteorder = "1.5.0"
byte-unit = "5.1.6"
clap = "4.5.26"
convert_case = "0.7.1"
clap = "4.5.27"
criterion = { version = "0.5.1", default-features = false }
dashmap = "6.1.0"
dirs = "6.0.0"
flate2 = "1.0.35"
getrandom = "0.2.15"
handlebars = "6.2.0"
home = "0.5.11"
indoc = "2.0.5"
indexmap = "2.7.1"
Expand Down
2 changes: 0 additions & 2 deletions ristretto_classloader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ home = { workspace = true }
tokio = { workspace = true, features = ["fs"] }

[dev-dependencies]
convert_case = { workspace = true }
criterion = { workspace = true }
handlebars = { workspace = true }
indoc = { workspace = true }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
Expand Down
41 changes: 11 additions & 30 deletions ristretto_classloader/benches/class_loader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::{criterion_group, criterion_main, Criterion};
use ristretto_classloader::{runtime, Result};
use ristretto_classloader::{runtime, ClassLoader, Result};
use std::sync::Arc;
use tokio::runtime::Runtime;

Expand All @@ -9,35 +9,16 @@ fn benchmarks(criterion: &mut Criterion) {

fn bench_lifecycle(criterion: &mut Criterion) -> Result<()> {
let runtime = Runtime::new()?;
let (_java_home, _version, class_loader) =
runtime.block_on(async { runtime::version_class_loader("21.0.6.7.1").await })?;
let class_loader = runtime.block_on(async { default_class_loader().await })?;
let class_loader = Arc::new(class_loader);

criterion.bench_function("runtime_v8", |bencher| {
criterion.bench_function("default_class_loader", |bencher| {
bencher.iter(|| {
runtime.block_on(async {
boot_class_loader("8.442.06.1").await.ok();
});
});
});
criterion.bench_function("runtime_v11", |bencher| {
bencher.iter(|| {
runtime.block_on(async {
boot_class_loader("11.0.26.4.1").await.ok();
});
});
});
criterion.bench_function("runtime_v17", |bencher| {
bencher.iter(|| {
runtime.block_on(async {
boot_class_loader("17.0.13.11.1").await.ok();
});
});
});
criterion.bench_function("runtime_v21", |bencher| {
bencher.iter(|| {
runtime.block_on(async {
boot_class_loader("21.0.6.7.1").await.ok();
let class_loader = default_class_loader()
.await
.expect("Failed to create class loader");
let _class = class_loader.load("java.lang.Object").await.ok();
});
});
});
Expand All @@ -59,10 +40,10 @@ fn bench_lifecycle(criterion: &mut Criterion) -> Result<()> {
Ok(())
}

async fn boot_class_loader(version: &str) -> Result<()> {
let (_java_home, _java_version, class_loader) = runtime::version_class_loader(version).await?;
let _class = class_loader.load("java.lang.Object").await?;
Ok(())
async fn default_class_loader() -> Result<Arc<ClassLoader>> {
let (_java_home, _version, class_loader) = runtime::default_class_loader().await?;
let class_loader = Arc::new(class_loader);
Ok(class_loader)
}

criterion_group!(
Expand Down
181 changes: 0 additions & 181 deletions ristretto_classloader/tests/native.rs

This file was deleted.

23 changes: 0 additions & 23 deletions ristretto_classloader/tests/template.hbs

This file was deleted.

4 changes: 4 additions & 0 deletions ristretto_vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ rustls-tls = [
url = [
"ristretto_classloader/url",
]

[[bench]]
harness = false
name = "vm"
69 changes: 69 additions & 0 deletions ristretto_vm/benches/vm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use criterion::{criterion_group, criterion_main, Criterion};
use ristretto_classfile::Error;
use ristretto_classloader::ClassPath;
use ristretto_vm::{ConfigurationBuilder, Result, VM};
use std::path::PathBuf;
use tokio::runtime::Runtime;

const CARGO_MANIFEST: &str = env!("CARGO_MANIFEST_DIR");

fn benchmarks(criterion: &mut Criterion) {
bench_lifecycle(criterion).ok();
}

fn bench_lifecycle(criterion: &mut Criterion) -> Result<()> {
let runtime = Runtime::new().map_err(|error| Error::IoError(error.to_string()))?;

criterion.bench_function("vm_init", |bencher| {
bencher.iter(|| {
runtime.block_on(async {
vm_init().await.ok();
});
});
});
criterion.bench_function("hello_world", |bencher| {
bencher.iter(|| {
runtime.block_on(async {
hello_world().await.ok();
});
});
});

Ok(())
}

async fn vm_init() -> Result<()> {
let cargo_manifest = PathBuf::from(CARGO_MANIFEST);
let classes_jar_path = cargo_manifest
.join("..")
.join("classes")
.join("classes.jar");
let class_path = ClassPath::from(classes_jar_path.to_string_lossy());
let configuration = ConfigurationBuilder::new().class_path(class_path).build()?;
let _ = VM::new(configuration).await?;
Ok(())
}

async fn hello_world() -> Result<()> {
let cargo_manifest = PathBuf::from(CARGO_MANIFEST);
let classes_jar_path = cargo_manifest
.join("..")
.join("classes")
.join("classes.jar");
let class_path = ClassPath::from(classes_jar_path.to_string_lossy());
let configuration = ConfigurationBuilder::new()
.class_path(class_path)
.main_class("HelloWorld")
.build()?;
let vm = VM::new(configuration).await?;
let parameters: Vec<&str> = Vec::new();
let _result = vm.invoke_main(parameters).await?;
Ok(())
}

criterion_group!(
name = benches;
config = Criterion::default();
targets = benchmarks
);
criterion_main!(benches);

0 comments on commit 3ac160b

Please sign in to comment.