Skip to content

Commit

Permalink
Add performance tests for bindings API
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Sep 20, 2024
1 parent 15c437c commit a27111e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion crates/solidity/testing/perf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ publish = false
[dev-dependencies]
iai-callgrind = { workspace = true }
semver = { workspace = true }
slang_solidity = { workspace = true }
metaslang_bindings = { workspace = true }
slang_solidity = { workspace = true, features = [
"__experimental_bindings_api",
] }

[[bench]]
name = "iai"
Expand Down
55 changes: 55 additions & 0 deletions crates/solidity/testing/perf/benches/iai/dataset.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::BTreeSet;
use std::sync::Arc;

use metaslang_bindings::PathResolver;
use semver::Version;
use slang_solidity::bindings::{self, Bindings};
use slang_solidity::cst::Node;
use slang_solidity::kinds::{EdgeLabel, NonterminalKind};
use slang_solidity::language::Language;
Expand Down Expand Up @@ -243,3 +246,55 @@ pub fn run_query(trees: &Vec<Node>) {
"Function names don't match: {results:#?}"
);
}

struct NoOpResolver;

impl PathResolver for NoOpResolver {
fn resolve_path(&self, _context_path: &str, path_to_resolve: &str) -> Option<String> {
Some(path_to_resolve.to_string())
}
}

pub fn run_create_bindings() {
let _ = bindings::create_with_resolver(SOLC_VERSION, Arc::new(NoOpResolver {}));
}

pub fn run_bindings(trees: &Vec<Node>) -> Vec<Bindings> {
let mut result = vec![];
let mut definition_count = 0_usize;

for tree in trees {
let mut bindings = bindings::create_with_resolver(SOLC_VERSION, Arc::new(NoOpResolver {}));
bindings.add_file("input.sol", tree.cursor_with_offset(TextIndex::ZERO));
definition_count += bindings.all_definitions().count();
result.push(bindings);
}

// 723 definitions
println!("A total of {definition_count} definitions were found");

result
}

pub fn prepare_bindings() -> Vec<Bindings> {
let trees = run_parser();
run_bindings(&trees)
}

pub fn run_resolve_references(bindings_vec: &Vec<Bindings>) {
let mut reference_count = 0_usize;
let mut resolved_references = 0_usize;

for bindings in bindings_vec {
for reference in bindings.all_references() {
reference_count += 1;
let resolution = reference.jump_to_definition();
if resolution.is_ok() {
resolved_references += 1;
}
}
}

// 1491 references, 1170 resolved
println!("Out of a total of {reference_count} references found, {resolved_references} were unambiguously resolved");
}
18 changes: 17 additions & 1 deletion crates/solidity/testing/perf/benches/iai/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use iai_callgrind::{
library_benchmark, library_benchmark_group, main, Direction, FlamegraphConfig,
LibraryBenchmarkConfig, Tool, ValgrindTool,
};
use slang_solidity::bindings::Bindings;
use slang_solidity::cst::Node;

#[library_benchmark]
Expand All @@ -32,10 +33,25 @@ fn query(trees: Vec<Node>) {
black_box(dataset::run_query(&trees));
}

#[library_benchmark]
fn create_bindings() {
black_box(dataset::run_create_bindings());
}

#[library_benchmark(setup = dataset::run_parser)]
fn bindings(trees: Vec<Node>) {
black_box(dataset::run_bindings(&trees));
}

#[library_benchmark(setup = dataset::prepare_bindings)]
fn resolve_references(bindings: Vec<Bindings>) {
black_box(dataset::run_resolve_references(&bindings));
}

library_benchmark_group!(
name = benchmarks;

benchmarks = parser, cursor, query
benchmarks = parser, cursor, query, create_bindings, bindings, resolve_references
);

main!(
Expand Down

0 comments on commit a27111e

Please sign in to comment.