Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark framework to sway-lsp #4927

Merged
merged 14 commits into from
Aug 11, 2023
126 changes: 126 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions sway-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ urlencoding = "2.1.2"

[dev-dependencies]
assert-json-diff = "2.0"
criterion = "0.5"
dirs = "4.0"
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
sway-lsp-test-utils = { path = "tests/utils" }
tower = { version = "0.4.12", default-features = false, features = ["util"] }

[[bench]]
name = "bench_main"
harness = false
7 changes: 7 additions & 0 deletions sway-lsp/benches/bench_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod lsp_benchmarks;
use criterion::criterion_main;

criterion_main! {
lsp_benchmarks::token_map::benches,
lsp_benchmarks::requests::benches,
}
29 changes: 29 additions & 0 deletions sway-lsp/benches/lsp_benchmarks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pub mod requests;
pub mod token_map;

use lsp_types::Url;
use std::{path::PathBuf, sync::Arc};
use sway_lsp::core::session::{self, Session};

pub fn compile_test_project() -> (Url, Arc<Session>) {
let session = Session::new();
// Load the test project
let uri = Url::from_file_path(benchmark_dir().join("src/main.sw")).unwrap();
session.handle_open_file(&uri);
// Compile the project and write the parse result to the session
let parse_result = session::parse_project(&uri).unwrap();
session.write_parse_result(parse_result);
(uri, Arc::new(session))
}

pub fn sway_workspace_dir() -> PathBuf {
std::env::current_dir()
.unwrap()
.parent()
.unwrap()
.to_path_buf()
}

pub fn benchmark_dir() -> PathBuf {
sway_workspace_dir().join("sway-lsp/tests/fixtures/benchmark")
}
44 changes: 44 additions & 0 deletions sway-lsp/benches/lsp_benchmarks/requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use criterion::{black_box, criterion_group, Criterion};
use lsp_types::Position;
use sway_lsp::{capabilities, utils::keyword_docs::KeywordDocs};

fn benchmarks(c: &mut Criterion) {
let (uri, session) = black_box(super::compile_test_project());
let keyword_docs = KeywordDocs::new();
let position = Position::new(1716, 24);

c.bench_function("hover", |b| {
b.iter(|| {
capabilities::hover::hover_data(session.clone(), &keyword_docs, uri.clone(), position)
})
});

c.bench_function("highlight", |b| {
b.iter(|| capabilities::highlight::get_highlights(session.clone(), uri.clone(), position))
});

c.bench_function("goto_definition", |b| {
b.iter(|| session.token_definition_response(uri.clone(), position))
});

c.bench_function("prepare_rename", |b| {
b.iter(|| capabilities::rename::prepare_rename(session.clone(), uri.clone(), position))
});

c.bench_function("rename", |b| {
JoshuaBatty marked this conversation as resolved.
Show resolved Hide resolved
b.iter(|| {
capabilities::rename::rename(
session.clone(),
"new_token_name".to_string(),
uri.clone(),
position,
)
})
});
}

criterion_group! {
name = benches;
config = Criterion::default().measurement_time(std::time::Duration::from_secs(10));
targets = benchmarks
}
50 changes: 50 additions & 0 deletions sway-lsp/benches/lsp_benchmarks/token_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use criterion::{black_box, criterion_group, Criterion};
use lsp_types::Position;

fn benchmarks(c: &mut Criterion) {
let (uri, session) = black_box(super::compile_test_project());
let engines = session.engines.read();
let position = Position::new(1716, 24);

c.bench_function("tokens_for_file", |b| {
b.iter(|| session.token_map().tokens_for_file(engines.se(), &uri))
});

c.bench_function("idents_at_position", |b| {
b.iter(|| {
session
.token_map()
.idents_at_position(position, session.token_map().iter())
})
});

c.bench_function("tokens_at_position", |b| {
b.iter(|| {
session
.token_map()
.tokens_at_position(engines.se(), &uri, position, None)
})
});

c.bench_function("token_at_position", |b| {
b.iter(|| {
session
.token_map()
.token_at_position(engines.se(), &uri, position)
})
});

c.bench_function("parent_decl_at_position", |b| {
b.iter(|| {
session
.token_map()
.parent_decl_at_position(engines.se(), &uri, position)
})
});
}

criterion_group! {
name = benches;
config = Criterion::default().measurement_time(std::time::Duration::from_secs(10));
targets = benchmarks
}
2 changes: 1 addition & 1 deletion sway-lsp/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pub mod document;
pub mod session;
pub(crate) mod sync;
pub(crate) mod token;
pub(crate) mod token_map;
pub mod token_map;
pub mod token_map_ext;
8 changes: 7 additions & 1 deletion sway-lsp/src/core/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ pub struct Session {
pub diagnostics: Arc<RwLock<Diagnostics>>,
}

impl Default for Session {
JoshuaBatty marked this conversation as resolved.
Show resolved Hide resolved
fn default() -> Self {
Self::new()
}
}

impl Session {
pub fn new() -> Self {
Session {
Expand Down Expand Up @@ -138,7 +144,7 @@ impl Session {

/// Write the result of parsing to the session.
/// This function should only be called after successfully parsing.
pub(crate) fn write_parse_result(&self, res: ParseResult) {
pub fn write_parse_result(&self, res: ParseResult) {
self.token_map.clear();
self.runnables.clear();

Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/src/core/token_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use crate::core::token_map_ext::TokenMapExt;
/// It stores all of the tokens that have been parsed and typechecked by the sway compiler.
///
/// The TokenMap is a wrapper around a [DashMap], which is a concurrent HashMap.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct TokenMap(DashMap<(Ident, Span), Token>);

impl TokenMap {
Expand Down
4 changes: 2 additions & 2 deletions sway-lsp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![recursion_limit = "256"]

mod capabilities;
pub mod capabilities;
pub mod config;
mod core;
pub mod core;
pub mod error;
pub mod server_state;
mod handlers {
Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub(crate) mod attributes;
pub mod debug;
pub(crate) mod document;
pub(crate) mod keyword_docs;
pub mod keyword_docs;
pub mod markdown;
pub(crate) mod markup;
Loading