|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +//! Measure listing and resolving visible reference lenses in a 10,000-line file. |
| 9 | +
|
| 10 | +use std::cell::RefCell; |
| 11 | +use std::fs; |
| 12 | +use std::time::Duration; |
| 13 | + |
| 14 | +use criterion::Criterion; |
| 15 | +use criterion::criterion_group; |
| 16 | +use criterion::criterion_main; |
| 17 | +use lsp_types::CodeLens; |
| 18 | +use lsp_types::CodeLensRequest; |
| 19 | +use lsp_types::CodeLensResolveRequest; |
| 20 | +use lsp_types::Uri; |
| 21 | +use pyrefly_lsp_test::IndexingMode; |
| 22 | +use pyrefly_lsp_test::LspArgs; |
| 23 | +use pyrefly_lsp_test::object_model::InitializeSettings; |
| 24 | +use pyrefly_lsp_test::object_model::LspInteraction; |
| 25 | +use pyrefly_lsp_test::object_model::LspInteractionArgs; |
| 26 | +use pyrefly_util::telemetry::NoTelemetry; |
| 27 | +use serde_json::json; |
| 28 | +use tempfile::TempDir; |
| 29 | + |
| 30 | +const DEFINITIONS: usize = 2500; |
| 31 | +const VISIBLE_LENSES: usize = 20; |
| 32 | +const IMPORTERS: usize = 10; |
| 33 | + |
| 34 | +/// Measure warm LSP requests with fixture creation and initial analysis excluded. |
| 35 | +fn code_lens(c: &mut Criterion) { |
| 36 | + let root = TempDir::new().unwrap(); |
| 37 | + fs::write( |
| 38 | + root.path().join("pyrefly.toml"), |
| 39 | + "project-includes = [\"**/*.py\"]\n", |
| 40 | + ) |
| 41 | + .unwrap(); |
| 42 | + let mut source = String::new(); |
| 43 | + for index in 0..DEFINITIONS { |
| 44 | + source.push_str(&format!( |
| 45 | + "def function_{index}() -> int:\n return {index}\n\nfunction_{index}()\n" |
| 46 | + )); |
| 47 | + } |
| 48 | + assert_eq!(source.lines().count(), 10_000); |
| 49 | + fs::write(root.path().join("large.py"), source).unwrap(); |
| 50 | + let mut usage = "import large\n".to_owned(); |
| 51 | + for index in 0..VISIBLE_LENSES { |
| 52 | + usage.push_str(&format!("large.function_{index}()\n")); |
| 53 | + } |
| 54 | + for index in 0..IMPORTERS { |
| 55 | + fs::write(root.path().join(format!("usage_{index}.py")), &usage).unwrap(); |
| 56 | + } |
| 57 | + |
| 58 | + let uri = Uri::from_file_path(root.path().join("large.py")).unwrap(); |
| 59 | + let mut interaction = LspInteraction::new_with_args(LspInteractionArgs { |
| 60 | + args: LspArgs { |
| 61 | + indexing_mode: IndexingMode::LazyBlocking, |
| 62 | + ..LspInteractionArgs::default().args |
| 63 | + }, |
| 64 | + telemetry: Box::new(NoTelemetry), |
| 65 | + ..Default::default() |
| 66 | + }); |
| 67 | + interaction.client.set_message_logging(false); |
| 68 | + interaction.set_root(root.path().to_path_buf()); |
| 69 | + interaction |
| 70 | + .initialize(InitializeSettings { |
| 71 | + workspace_folders: Some(vec![( |
| 72 | + "benchmark".to_owned(), |
| 73 | + Uri::from_file_path(root.path()).unwrap(), |
| 74 | + )]), |
| 75 | + initialization_options: Some(json!({"pyrefly": { |
| 76 | + "supportsReferencesCodeLens": true, |
| 77 | + "referencesCodeLens": true |
| 78 | + }})), |
| 79 | + ..Default::default() |
| 80 | + }) |
| 81 | + .unwrap(); |
| 82 | + interaction.client.did_open("large.py"); |
| 83 | + let params = json!({"textDocument": {"uri": uri}}); |
| 84 | + let lenses = RefCell::new(Vec::<CodeLens>::new()); |
| 85 | + interaction |
| 86 | + .client |
| 87 | + .send_request::<CodeLensRequest>(params.clone()) |
| 88 | + .expect_response_with(|response| { |
| 89 | + *lenses.borrow_mut() = response.unwrap(); |
| 90 | + assert_eq!(lenses.borrow().len(), DEFINITIONS); |
| 91 | + true |
| 92 | + }) |
| 93 | + .unwrap(); |
| 94 | + let visible: Vec<_> = lenses |
| 95 | + .into_inner() |
| 96 | + .into_iter() |
| 97 | + .take(VISIBLE_LENSES) |
| 98 | + .map(|lens| json!(lens)) |
| 99 | + .collect(); |
| 100 | + let resolve_visible = || { |
| 101 | + for lens in &visible { |
| 102 | + interaction |
| 103 | + .client |
| 104 | + .send_request::<CodeLensResolveRequest>(lens.clone()) |
| 105 | + .expect_response_with(|resolved| { |
| 106 | + assert_eq!(resolved.command.unwrap().title, "11 references"); |
| 107 | + true |
| 108 | + }) |
| 109 | + .unwrap(); |
| 110 | + } |
| 111 | + }; |
| 112 | + resolve_visible(); |
| 113 | + |
| 114 | + let mut group = c.benchmark_group("code_lens/10000_lines"); |
| 115 | + group.sample_size(10); |
| 116 | + group.warm_up_time(Duration::from_secs(1)); |
| 117 | + group.measurement_time(Duration::from_secs(2)); |
| 118 | + group.bench_function("list", |b| { |
| 119 | + b.iter(|| { |
| 120 | + interaction |
| 121 | + .client |
| 122 | + .send_request::<CodeLensRequest>(params.clone()) |
| 123 | + .expect_response_with(|response| { |
| 124 | + let lenses = response.unwrap(); |
| 125 | + assert_eq!(lenses.len(), DEFINITIONS); |
| 126 | + assert!(lenses.iter().all(|lens| lens.command.is_none())); |
| 127 | + true |
| 128 | + }) |
| 129 | + .unwrap(); |
| 130 | + }); |
| 131 | + }); |
| 132 | + group.bench_function("resolve_20_visible", |b| b.iter(resolve_visible)); |
| 133 | + group.finish(); |
| 134 | + interaction.shutdown().unwrap(); |
| 135 | +} |
| 136 | + |
| 137 | +criterion_group!(benches, code_lens); |
| 138 | +criterion_main!(benches); |
0 commit comments