2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-02-25 00:14:17 -05:00
|
|
|
|
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2021-05-20 05:56:48 -04:00
|
|
|
use deno_core::url::Url;
|
2021-02-25 00:14:17 -05:00
|
|
|
use std::collections::HashMap;
|
2021-03-25 14:17:37 -04:00
|
|
|
use std::path::Path;
|
2021-02-25 00:14:17 -05:00
|
|
|
use std::time::Duration;
|
2023-03-08 18:15:20 -05:00
|
|
|
use test_util::lsp::LspClientBuilder;
|
2022-04-03 00:17:30 -04:00
|
|
|
use tower_lsp::lsp_types as lsp;
|
2021-02-25 00:14:17 -05:00
|
|
|
|
2022-01-13 10:25:20 -05:00
|
|
|
static FIXTURE_CODE_LENS_TS: &str = include_str!("testdata/code_lens.ts");
|
|
|
|
static FIXTURE_DB_TS: &str = include_str!("testdata/db.ts");
|
|
|
|
static FIXTURE_DB_MESSAGES: &[u8] = include_bytes!("testdata/db_messages.json");
|
2021-02-25 00:14:17 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
enum FixtureType {
|
|
|
|
#[serde(rename = "action")]
|
|
|
|
Action,
|
|
|
|
#[serde(rename = "change")]
|
|
|
|
Change,
|
|
|
|
#[serde(rename = "completion")]
|
|
|
|
Completion,
|
|
|
|
#[serde(rename = "highlight")]
|
|
|
|
Highlight,
|
|
|
|
#[serde(rename = "hover")]
|
|
|
|
Hover,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct FixtureMessage {
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
fixture_type: FixtureType,
|
|
|
|
params: Value,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A benchmark that opens a 8000+ line TypeScript document, adds a function to
|
|
|
|
/// the end of the document and does a level of hovering and gets quick fix
|
|
|
|
/// code actions.
|
2023-03-09 15:09:03 -05:00
|
|
|
fn bench_big_file_edits(deno_exe: &Path) -> Duration {
|
2023-05-26 02:10:18 -04:00
|
|
|
let mut client = LspClientBuilder::new()
|
|
|
|
.use_diagnostic_sync(false)
|
|
|
|
.deno_exe(deno_exe)
|
|
|
|
.build();
|
2023-03-08 18:15:20 -05:00
|
|
|
client.initialize_default();
|
2023-10-24 23:13:16 -04:00
|
|
|
client.change_configuration(json!({ "deno": { "enable": true } }));
|
2021-02-25 00:14:17 -05:00
|
|
|
|
|
|
|
client.write_notification(
|
|
|
|
"textDocument/didOpen",
|
|
|
|
json!({
|
|
|
|
"textDocument": {
|
2022-01-13 10:25:20 -05:00
|
|
|
"uri": "file:///testdata/db.ts",
|
2021-02-25 00:14:17 -05:00
|
|
|
"languageId": "typescript",
|
|
|
|
"version": 1,
|
|
|
|
"text": FIXTURE_DB_TS
|
|
|
|
}
|
|
|
|
}),
|
2023-03-09 15:09:03 -05:00
|
|
|
);
|
2021-02-25 00:14:17 -05:00
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-05-07 07:05:32 -04:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-05-07 07:05:32 -04:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-02-25 00:14:17 -05:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
|
|
|
|
|
|
|
let messages: Vec<FixtureMessage> =
|
2023-03-09 15:09:03 -05:00
|
|
|
serde_json::from_slice(FIXTURE_DB_MESSAGES).unwrap();
|
2021-02-25 00:14:17 -05:00
|
|
|
|
|
|
|
for msg in messages {
|
|
|
|
match msg.fixture_type {
|
|
|
|
FixtureType::Action => {
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_request("textDocument/codeAction", msg.params);
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|
|
|
|
FixtureType::Change => {
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_notification("textDocument/didChange", msg.params);
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|
|
|
|
FixtureType::Completion => {
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_request("textDocument/completion", msg.params);
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|
|
|
|
FixtureType::Highlight => {
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_request("textDocument/documentHighlight", msg.params);
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|
|
|
|
FixtureType::Hover => {
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_request("textDocument/hover", msg.params);
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_request("shutdown", json!(null));
|
|
|
|
client.write_notification("exit", json!(null));
|
2021-02-25 00:14:17 -05:00
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
client.duration()
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
fn bench_code_lens(deno_exe: &Path) -> Duration {
|
2023-05-26 02:10:18 -04:00
|
|
|
let mut client = LspClientBuilder::new()
|
|
|
|
.use_diagnostic_sync(false)
|
|
|
|
.deno_exe(deno_exe)
|
|
|
|
.build();
|
2023-03-08 18:15:20 -05:00
|
|
|
client.initialize_default();
|
2023-10-24 23:13:16 -04:00
|
|
|
client.change_configuration(json!({ "deno": {
|
|
|
|
"enable": true,
|
|
|
|
"codeLens": {
|
|
|
|
"implementations": true,
|
|
|
|
"references": true,
|
|
|
|
"test": true,
|
|
|
|
},
|
|
|
|
} }));
|
2021-06-04 17:31:44 -04:00
|
|
|
|
|
|
|
client.write_notification(
|
|
|
|
"textDocument/didOpen",
|
|
|
|
json!({
|
|
|
|
"textDocument": {
|
2022-01-13 10:25:20 -05:00
|
|
|
"uri": "file:///testdata/code_lens.ts",
|
2021-06-04 17:31:44 -04:00
|
|
|
"languageId": "typescript",
|
|
|
|
"version": 1,
|
|
|
|
"text": FIXTURE_CODE_LENS_TS
|
|
|
|
}
|
|
|
|
}),
|
2023-03-09 15:09:03 -05:00
|
|
|
);
|
2021-06-04 17:31:44 -04:00
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-06-04 17:31:44 -04:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-06-04 17:31:44 -04:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-06-04 17:31:44 -04:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
let res = client.write_request_with_res_as::<Vec<lsp::CodeLens>>(
|
|
|
|
"textDocument/codeLens",
|
|
|
|
json!({
|
|
|
|
"textDocument": {
|
|
|
|
"uri": "file:///testdata/code_lens.ts"
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
2021-06-04 17:31:44 -04:00
|
|
|
assert!(!res.is_empty());
|
|
|
|
|
|
|
|
for code_lens in res {
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_request("codeLens/resolve", code_lens);
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
client.duration()
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
fn bench_find_replace(deno_exe: &Path) -> Duration {
|
2023-05-26 02:10:18 -04:00
|
|
|
let mut client = LspClientBuilder::new()
|
|
|
|
.use_diagnostic_sync(false)
|
|
|
|
.deno_exe(deno_exe)
|
|
|
|
.build();
|
2023-03-08 18:15:20 -05:00
|
|
|
client.initialize_default();
|
2023-10-24 23:13:16 -04:00
|
|
|
client.change_configuration(json!({ "deno": { "enable": true } }));
|
2021-05-20 05:56:48 -04:00
|
|
|
|
|
|
|
for i in 0..10 {
|
|
|
|
client.write_notification(
|
|
|
|
"textDocument/didOpen",
|
|
|
|
json!({
|
|
|
|
"textDocument": {
|
2023-01-27 10:43:16 -05:00
|
|
|
"uri": format!("file:///a/file_{i}.ts"),
|
2021-05-20 05:56:48 -04:00
|
|
|
"languageId": "typescript",
|
|
|
|
"version": 1,
|
|
|
|
"text": "console.log(\"000\");\n"
|
|
|
|
}
|
|
|
|
}),
|
2023-03-09 15:09:03 -05:00
|
|
|
);
|
2021-05-20 05:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _ in 0..3 {
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-05-20 05:56:48 -04:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..10 {
|
2023-01-27 10:43:16 -05:00
|
|
|
let file_name = format!("file:///a/file_{i}.ts");
|
2021-05-20 05:56:48 -04:00
|
|
|
client.write_notification(
|
|
|
|
"textDocument/didChange",
|
|
|
|
lsp::DidChangeTextDocumentParams {
|
|
|
|
text_document: lsp::VersionedTextDocumentIdentifier {
|
|
|
|
uri: Url::parse(&file_name).unwrap(),
|
|
|
|
version: 2,
|
|
|
|
},
|
|
|
|
content_changes: vec![lsp::TextDocumentContentChangeEvent {
|
|
|
|
range: Some(lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 13,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 16,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
range_length: None,
|
|
|
|
text: "111".to_string(),
|
|
|
|
}],
|
|
|
|
},
|
2023-03-09 15:09:03 -05:00
|
|
|
);
|
2021-05-20 05:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..10 {
|
2023-01-27 10:43:16 -05:00
|
|
|
let file_name = format!("file:///a/file_{i}.ts");
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_request(
|
2021-05-20 05:56:48 -04:00
|
|
|
"textDocument/formatting",
|
|
|
|
lsp::DocumentFormattingParams {
|
|
|
|
text_document: lsp::TextDocumentIdentifier {
|
|
|
|
uri: Url::parse(&file_name).unwrap(),
|
|
|
|
},
|
|
|
|
options: lsp::FormattingOptions {
|
|
|
|
tab_size: 2,
|
|
|
|
insert_spaces: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
work_done_progress_params: Default::default(),
|
|
|
|
},
|
2023-03-09 15:09:03 -05:00
|
|
|
);
|
2021-05-20 05:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _ in 0..3 {
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-05-20 05:56:48 -04:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_request("shutdown", json!(null));
|
|
|
|
client.write_notification("exit", json!(null));
|
2021-05-20 05:56:48 -04:00
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
client.duration()
|
2021-05-20 05:56:48 -04:00
|
|
|
}
|
|
|
|
|
2021-02-25 00:14:17 -05:00
|
|
|
/// A test that starts up the LSP, opens a single line document, and exits.
|
2023-03-09 15:09:03 -05:00
|
|
|
fn bench_startup_shutdown(deno_exe: &Path) -> Duration {
|
2023-05-26 02:10:18 -04:00
|
|
|
let mut client = LspClientBuilder::new()
|
|
|
|
.use_diagnostic_sync(false)
|
|
|
|
.deno_exe(deno_exe)
|
|
|
|
.build();
|
2023-03-08 18:15:20 -05:00
|
|
|
client.initialize_default();
|
2023-10-24 23:13:16 -04:00
|
|
|
client.change_configuration(json!({ "deno": { "enable": true } }));
|
2021-02-25 00:14:17 -05:00
|
|
|
|
|
|
|
client.write_notification(
|
|
|
|
"textDocument/didOpen",
|
|
|
|
json!({
|
|
|
|
"textDocument": {
|
|
|
|
"uri": "file:///a/file.ts",
|
|
|
|
"languageId": "typescript",
|
|
|
|
"version": 1,
|
|
|
|
"text": "console.log(Deno.args);\n"
|
|
|
|
}
|
|
|
|
}),
|
2023-03-09 15:09:03 -05:00
|
|
|
);
|
2021-02-25 00:14:17 -05:00
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-05-07 07:05:32 -04:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-05-07 07:05:32 -04:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
2023-03-09 15:09:03 -05:00
|
|
|
let (method, _): (String, Option<Value>) = client.read_notification();
|
2021-02-25 00:14:17 -05:00
|
|
|
assert_eq!(method, "textDocument/publishDiagnostics");
|
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_request("shutdown", json!(null));
|
2021-02-25 00:14:17 -05:00
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
client.write_notification("exit", json!(null));
|
2021-02-25 00:14:17 -05:00
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
client.duration()
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate benchmarks for the LSP server.
|
2023-03-09 15:09:03 -05:00
|
|
|
pub fn benchmarks(deno_exe: &Path) -> HashMap<String, i64> {
|
2021-02-25 00:14:17 -05:00
|
|
|
println!("-> Start benchmarking lsp");
|
|
|
|
let mut exec_times = HashMap::new();
|
|
|
|
|
|
|
|
println!(" - Simple Startup/Shutdown ");
|
|
|
|
let mut times = Vec::new();
|
|
|
|
for _ in 0..10 {
|
2023-03-09 15:09:03 -05:00
|
|
|
times.push(bench_startup_shutdown(deno_exe));
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|
|
|
|
let mean =
|
2022-06-28 08:21:05 -04:00
|
|
|
(times.iter().sum::<Duration>() / times.len() as u32).as_millis() as i64;
|
2021-02-25 00:14:17 -05:00
|
|
|
println!(" ({} runs, mean: {}ms)", times.len(), mean);
|
|
|
|
exec_times.insert("startup_shutdown".to_string(), mean);
|
|
|
|
|
|
|
|
println!(" - Big Document/Several Edits ");
|
|
|
|
let mut times = Vec::new();
|
|
|
|
for _ in 0..5 {
|
2023-03-09 15:09:03 -05:00
|
|
|
times.push(bench_big_file_edits(deno_exe));
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|
|
|
|
let mean =
|
2022-06-28 08:21:05 -04:00
|
|
|
(times.iter().sum::<Duration>() / times.len() as u32).as_millis() as i64;
|
2021-02-25 00:14:17 -05:00
|
|
|
println!(" ({} runs, mean: {}ms)", times.len(), mean);
|
|
|
|
exec_times.insert("big_file_edits".to_string(), mean);
|
|
|
|
|
2021-05-20 05:56:48 -04:00
|
|
|
println!(" - Find/Replace");
|
|
|
|
let mut times = Vec::new();
|
|
|
|
for _ in 0..10 {
|
2023-03-09 15:09:03 -05:00
|
|
|
times.push(bench_find_replace(deno_exe));
|
2021-05-20 05:56:48 -04:00
|
|
|
}
|
|
|
|
let mean =
|
2022-06-28 08:21:05 -04:00
|
|
|
(times.iter().sum::<Duration>() / times.len() as u32).as_millis() as i64;
|
2021-05-20 05:56:48 -04:00
|
|
|
println!(" ({} runs, mean: {}ms)", times.len(), mean);
|
|
|
|
exec_times.insert("find_replace".to_string(), mean);
|
|
|
|
|
2021-06-04 17:31:44 -04:00
|
|
|
println!(" - Code Lens");
|
|
|
|
let mut times = Vec::new();
|
|
|
|
for _ in 0..10 {
|
2023-03-09 15:09:03 -05:00
|
|
|
times.push(bench_code_lens(deno_exe));
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
let mean =
|
2022-06-28 08:21:05 -04:00
|
|
|
(times.iter().sum::<Duration>() / times.len() as u32).as_millis() as i64;
|
2021-06-04 17:31:44 -04:00
|
|
|
println!(" ({} runs, mean: {}ms)", times.len(), mean);
|
|
|
|
exec_times.insert("code_lens".to_string(), mean);
|
|
|
|
|
2021-02-25 00:14:17 -05:00
|
|
|
println!("<- End benchmarking lsp");
|
|
|
|
|
2023-03-09 15:09:03 -05:00
|
|
|
exec_times
|
2021-02-25 00:14:17 -05:00
|
|
|
}
|