diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-03-09 15:09:03 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-09 15:09:03 -0500 |
commit | 47012bd931e785073e943b82dd397386b6ee7ca5 (patch) | |
tree | 82e2695413c72bd124f55a8bc5423c2b79d63f34 /cli/bench/lsp.rs | |
parent | 2f81e555d88e45a7ec9b5a5bc511fd3ea4d9c75f (diff) |
refactor(tests/lsp): consolidate more into test LspClient and reduce verbosity (#18100)
Diffstat (limited to 'cli/bench/lsp.rs')
-rw-r--r-- | cli/bench/lsp.rs | 153 |
1 files changed, 61 insertions, 92 deletions
diff --git a/cli/bench/lsp.rs b/cli/bench/lsp.rs index b0682e2e9..15f3ee762 100644 --- a/cli/bench/lsp.rs +++ b/cli/bench/lsp.rs @@ -1,6 +1,5 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use deno_core::error::AnyError; use deno_core::serde::Deserialize; use deno_core::serde_json; use deno_core::serde_json::json; @@ -10,7 +9,6 @@ use std::collections::HashMap; use std::path::Path; use std::time::Duration; use test_util::lsp::LspClientBuilder; -use test_util::lsp::LspResponseError; use tower_lsp::lsp_types as lsp; static FIXTURE_CODE_LENS_TS: &str = include_str!("testdata/code_lens.ts"); @@ -41,7 +39,7 @@ struct FixtureMessage { /// 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. -fn bench_big_file_edits(deno_exe: &Path) -> Result<Duration, AnyError> { +fn bench_big_file_edits(deno_exe: &Path) -> Duration { let mut client = LspClientBuilder::new().deno_exe(deno_exe).build(); client.initialize_default(); @@ -55,9 +53,9 @@ fn bench_big_file_edits(deno_exe: &Path) -> Result<Duration, AnyError> { "text": FIXTURE_DB_TS } }), - )?; + ); - let (id, method, _): (u64, String, Option<Value>) = client.read_request()?; + let (id, method, _): (u64, String, Option<Value>) = client.read_request(); assert_eq!(method, "workspace/configuration"); client.write_response( @@ -65,58 +63,45 @@ fn bench_big_file_edits(deno_exe: &Path) -> Result<Duration, AnyError> { json!({ "enable": true }), - )?; + ); - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); let messages: Vec<FixtureMessage> = - serde_json::from_slice(FIXTURE_DB_MESSAGES)?; + serde_json::from_slice(FIXTURE_DB_MESSAGES).unwrap(); for msg in messages { match msg.fixture_type { FixtureType::Action => { - client.write_request::<_, _, Value>( - "textDocument/codeAction", - msg.params, - )?; + client.write_request("textDocument/codeAction", msg.params); } FixtureType::Change => { - client.write_notification("textDocument/didChange", msg.params)?; + client.write_notification("textDocument/didChange", msg.params); } FixtureType::Completion => { - client.write_request::<_, _, Value>( - "textDocument/completion", - msg.params, - )?; + client.write_request("textDocument/completion", msg.params); } FixtureType::Highlight => { - client.write_request::<_, _, Value>( - "textDocument/documentHighlight", - msg.params, - )?; + client.write_request("textDocument/documentHighlight", msg.params); } FixtureType::Hover => { - client - .write_request::<_, _, Value>("textDocument/hover", msg.params)?; + client.write_request("textDocument/hover", msg.params); } } } - let (_, response_error): (Option<Value>, Option<LspResponseError>) = - client.write_request("shutdown", json!(null))?; - assert!(response_error.is_none()); + client.write_request("shutdown", json!(null)); + client.write_notification("exit", json!(null)); - client.write_notification("exit", json!(null))?; - - Ok(client.duration()) + client.duration() } -fn bench_code_lens(deno_exe: &Path) -> Result<Duration, AnyError> { +fn bench_code_lens(deno_exe: &Path) -> Duration { let mut client = LspClientBuilder::new().deno_exe(deno_exe).build(); client.initialize_default(); @@ -130,9 +115,9 @@ fn bench_code_lens(deno_exe: &Path) -> Result<Duration, AnyError> { "text": FIXTURE_CODE_LENS_TS } }), - )?; + ); - let (id, method, _): (u64, String, Option<Value>) = client.read_request()?; + let (id, method, _): (u64, String, Option<Value>) = client.read_request(); assert_eq!(method, "workspace/configuration"); client.write_response( @@ -140,42 +125,33 @@ fn bench_code_lens(deno_exe: &Path) -> Result<Duration, AnyError> { json!({ "enable": true }), - )?; + ); - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); - let (maybe_res, maybe_err) = client - .write_request::<_, _, Vec<lsp::CodeLens>>( - "textDocument/codeLens", - json!({ - "textDocument": { - "uri": "file:///testdata/code_lens.ts" - } - }), - ) - .unwrap(); - assert!(maybe_err.is_none()); - assert!(maybe_res.is_some()); - let res = maybe_res.unwrap(); + let res = client.write_request_with_res_as::<Vec<lsp::CodeLens>>( + "textDocument/codeLens", + json!({ + "textDocument": { + "uri": "file:///testdata/code_lens.ts" + } + }), + ); assert!(!res.is_empty()); for code_lens in res { - let (maybe_res, maybe_err) = client - .write_request::<_, _, lsp::CodeLens>("codeLens/resolve", code_lens) - .unwrap(); - assert!(maybe_err.is_none()); - assert!(maybe_res.is_some()); + client.write_request("codeLens/resolve", code_lens); } - Ok(client.duration()) + client.duration() } -fn bench_find_replace(deno_exe: &Path) -> Result<Duration, AnyError> { +fn bench_find_replace(deno_exe: &Path) -> Duration { let mut client = LspClientBuilder::new().deno_exe(deno_exe).build(); client.initialize_default(); @@ -190,17 +166,17 @@ fn bench_find_replace(deno_exe: &Path) -> Result<Duration, AnyError> { "text": "console.log(\"000\");\n" } }), - )?; + ); } for _ in 0..10 { - let (id, method, _) = client.read_request::<Value>()?; + let (id, method, _) = client.read_request::<Value>(); assert_eq!(method, "workspace/configuration"); - client.write_response(id, json!({ "enable": true }))?; + client.write_response(id, json!({ "enable": true })); } for _ in 0..3 { - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); } @@ -228,12 +204,12 @@ fn bench_find_replace(deno_exe: &Path) -> Result<Duration, AnyError> { text: "111".to_string(), }], }, - )?; + ); } for i in 0..10 { let file_name = format!("file:///a/file_{i}.ts"); - let (maybe_res, maybe_err) = client.write_request::<_, _, Value>( + client.write_request( "textDocument/formatting", lsp::DocumentFormattingParams { text_document: lsp::TextDocumentIdentifier { @@ -246,27 +222,22 @@ fn bench_find_replace(deno_exe: &Path) -> Result<Duration, AnyError> { }, work_done_progress_params: Default::default(), }, - )?; - assert!(maybe_err.is_none()); - assert!(maybe_res.is_some()); + ); } for _ in 0..3 { - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); } - let (_, response_error): (Option<Value>, Option<LspResponseError>) = - client.write_request("shutdown", json!(null))?; - assert!(response_error.is_none()); - - client.write_notification("exit", json!(null))?; + client.write_request("shutdown", json!(null)); + client.write_notification("exit", json!(null)); - Ok(client.duration()) + client.duration() } /// A test that starts up the LSP, opens a single line document, and exits. -fn bench_startup_shutdown(deno_exe: &Path) -> Result<Duration, AnyError> { +fn bench_startup_shutdown(deno_exe: &Path) -> Duration { let mut client = LspClientBuilder::new().deno_exe(deno_exe).build(); client.initialize_default(); @@ -280,9 +251,9 @@ fn bench_startup_shutdown(deno_exe: &Path) -> Result<Duration, AnyError> { "text": "console.log(Deno.args);\n" } }), - )?; + ); - let (id, method, _) = client.read_request::<Value>()?; + let (id, method, _) = client.read_request::<Value>(); assert_eq!(method, "workspace/configuration"); client.write_response( @@ -290,33 +261,31 @@ fn bench_startup_shutdown(deno_exe: &Path) -> Result<Duration, AnyError> { json!({ "enable": true }), - )?; + ); - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); - let (method, _): (String, Option<Value>) = client.read_notification()?; + let (method, _): (String, Option<Value>) = client.read_notification(); assert_eq!(method, "textDocument/publishDiagnostics"); - let (_, response_error): (Option<Value>, Option<LspResponseError>) = - client.write_request("shutdown", json!(null))?; - assert!(response_error.is_none()); + client.write_request("shutdown", json!(null)); - client.write_notification("exit", json!(null))?; + client.write_notification("exit", json!(null)); - Ok(client.duration()) + client.duration() } /// Generate benchmarks for the LSP server. -pub fn benchmarks(deno_exe: &Path) -> Result<HashMap<String, i64>, AnyError> { +pub fn benchmarks(deno_exe: &Path) -> HashMap<String, i64> { println!("-> Start benchmarking lsp"); let mut exec_times = HashMap::new(); println!(" - Simple Startup/Shutdown "); let mut times = Vec::new(); for _ in 0..10 { - times.push(bench_startup_shutdown(deno_exe)?); + times.push(bench_startup_shutdown(deno_exe)); } let mean = (times.iter().sum::<Duration>() / times.len() as u32).as_millis() as i64; @@ -326,7 +295,7 @@ pub fn benchmarks(deno_exe: &Path) -> Result<HashMap<String, i64>, AnyError> { println!(" - Big Document/Several Edits "); let mut times = Vec::new(); for _ in 0..5 { - times.push(bench_big_file_edits(deno_exe)?); + times.push(bench_big_file_edits(deno_exe)); } let mean = (times.iter().sum::<Duration>() / times.len() as u32).as_millis() as i64; @@ -336,7 +305,7 @@ pub fn benchmarks(deno_exe: &Path) -> Result<HashMap<String, i64>, AnyError> { println!(" - Find/Replace"); let mut times = Vec::new(); for _ in 0..10 { - times.push(bench_find_replace(deno_exe)?); + times.push(bench_find_replace(deno_exe)); } let mean = (times.iter().sum::<Duration>() / times.len() as u32).as_millis() as i64; @@ -346,7 +315,7 @@ pub fn benchmarks(deno_exe: &Path) -> Result<HashMap<String, i64>, AnyError> { println!(" - Code Lens"); let mut times = Vec::new(); for _ in 0..10 { - times.push(bench_code_lens(deno_exe)?); + times.push(bench_code_lens(deno_exe)); } let mean = (times.iter().sum::<Duration>() / times.len() as u32).as_millis() as i64; @@ -355,5 +324,5 @@ pub fn benchmarks(deno_exe: &Path) -> Result<HashMap<String, i64>, AnyError> { println!("<- End benchmarking lsp"); - Ok(exec_times) + exec_times } |