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
8 changes: 8 additions & 0 deletions sway-lsp/benches/bench_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod lsp_benchmarks;
use criterion::criterion_main;

criterion_main! {
lsp_benchmarks::token_map::benches,
lsp_benchmarks::requests::benches,
lsp_benchmarks::compile::benches,
}
15 changes: 15 additions & 0 deletions sway-lsp/benches/lsp_benchmarks/compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use criterion::{black_box, criterion_group, Criterion};

fn benchmarks(c: &mut Criterion) {
c.bench_function("compile_and_traverse", |b| {
b.iter(|| {
let _ = black_box(super::compile_test_project());
})
});
}

criterion_group! {
name = benches;
config = Criterion::default().measurement_time(std::time::Duration::from_secs(10));
targets = benchmarks
}
30 changes: 30 additions & 0 deletions sway-lsp/benches/lsp_benchmarks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pub mod compile;
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")
}
112 changes: 112 additions & 0 deletions sway-lsp/benches/lsp_benchmarks/requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use criterion::{black_box, criterion_group, Criterion};
use lsp_types::{
CodeLens, CompletionResponse, DocumentSymbolResponse, Position, Range,
TextDocumentContentChangeEvent, TextDocumentIdentifier,
};
use sway_lsp::{capabilities, lsp_ext::OnEnterParams, utils::keyword_docs::KeywordDocs};

fn benchmarks(c: &mut Criterion) {
let (uri, session) = black_box(super::compile_test_project());
let config = sway_lsp::config::Config::default();
let keyword_docs = KeywordDocs::new();
let position = Position::new(1717, 24);
let range = Range::new(Position::new(1628, 0), Position::new(1728, 0));

c.bench_function("semantic_tokens", |b| {
b.iter(|| capabilities::semantic_tokens::semantic_tokens_full(session.clone(), &uri))
});

c.bench_function("document_symbol", |b| {
b.iter(|| {
session
.symbol_information(&uri)
.map(DocumentSymbolResponse::Flat)
})
});

c.bench_function("completion", |b| {
let position = Position::new(1698, 28);
b.iter(|| {
session
.completion_items(&uri, position, ".")
.map(CompletionResponse::Array)
})
});

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("inlay_hints", |b| {
b.iter(|| {
capabilities::inlay_hints::inlay_hints(
session.clone(),
&uri,
&range,
&config.inlay_hints,
)
})
});

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,
)
})
});

c.bench_function("code_action", |b| {
let range = Range::new(Position::new(4, 10), Position::new(4, 10));
b.iter(|| capabilities::code_actions::code_actions(session.clone(), &range, &uri, &uri))
});

c.bench_function("code_lens", |b| {
b.iter(|| {
let mut result = vec![];
session.runnables.iter().for_each(|item| {
let runnable = item.value();
result.push(CodeLens {
range: runnable.range(),
command: Some(runnable.command()),
data: None,
});
});
})
});

c.bench_function("on_enter", |b| {
let params = OnEnterParams {
text_document: TextDocumentIdentifier::new(uri.clone()),
content_changes: vec![TextDocumentContentChangeEvent {
range: Some(Range::new(Position::new(3, 30), Position::new(3, 30))),
range_length: Some(0),
text: "\n".to_string(),
}],
};
b.iter(|| capabilities::on_enter::on_enter(&config.on_enter, &session, &uri, &params))
});
}

criterion_group! {
name = benches;
config = Criterion::default().measurement_time(std::time::Duration::from_secs(3));
targets = benchmarks
}
Loading