|
| 1 | +#![allow(clippy::disallowed_names)] |
| 2 | + |
| 3 | +use red_knot::program::Program; |
| 4 | +use red_knot::Workspace; |
| 5 | +use red_knot_module_resolver::{set_module_resolution_settings, ModuleResolutionSettings}; |
| 6 | +use ruff_benchmark::criterion::{ |
| 7 | + criterion_group, criterion_main, BatchSize, Criterion, Throughput, |
| 8 | +}; |
| 9 | +use ruff_db::file_system::{FileSystemPath, MemoryFileSystem}; |
| 10 | +use ruff_db::parsed::parsed_module; |
| 11 | +use ruff_db::vfs::{system_path_to_file, VfsFile}; |
| 12 | +use ruff_db::Upcast; |
| 13 | + |
| 14 | +static FOO_CODE: &str = r#" |
| 15 | +import typing |
| 16 | +
|
| 17 | +from bar import Bar |
| 18 | +
|
| 19 | +class Foo(Bar): |
| 20 | + def foo() -> str: |
| 21 | + return "foo" |
| 22 | +
|
| 23 | + @typing.override |
| 24 | + def bar() -> str: |
| 25 | + return "foo_bar" |
| 26 | +"#; |
| 27 | + |
| 28 | +static BAR_CODE: &str = r#" |
| 29 | +class Bar: |
| 30 | + def bar() -> str: |
| 31 | + return "bar" |
| 32 | +
|
| 33 | + def random(arg: int) -> int: |
| 34 | + if arg == 1: |
| 35 | + return 48472783 |
| 36 | + if arg < 10: |
| 37 | + return 20 |
| 38 | + return 36673 |
| 39 | +"#; |
| 40 | + |
| 41 | +static TYPING_CODE: &str = r#" |
| 42 | +def override(): ... |
| 43 | +"#; |
| 44 | + |
| 45 | +struct Case { |
| 46 | + program: Program, |
| 47 | + fs: MemoryFileSystem, |
| 48 | + foo: VfsFile, |
| 49 | + bar: VfsFile, |
| 50 | + typing: VfsFile, |
| 51 | +} |
| 52 | + |
| 53 | +fn setup_case() -> Case { |
| 54 | + let fs = MemoryFileSystem::new(); |
| 55 | + let foo_path = FileSystemPath::new("/src/foo.py"); |
| 56 | + let bar_path = FileSystemPath::new("/src/bar.py"); |
| 57 | + let typing_path = FileSystemPath::new("/src/typing.pyi"); |
| 58 | + fs.write_files([ |
| 59 | + (foo_path, FOO_CODE), |
| 60 | + (bar_path, BAR_CODE), |
| 61 | + (typing_path, TYPING_CODE), |
| 62 | + ]) |
| 63 | + .unwrap(); |
| 64 | + |
| 65 | + let workspace_root = FileSystemPath::new("/src"); |
| 66 | + let workspace = Workspace::new(workspace_root.to_path_buf()); |
| 67 | + |
| 68 | + let mut program = Program::new(workspace, fs.clone()); |
| 69 | + let foo = system_path_to_file(&program, foo_path).unwrap(); |
| 70 | + |
| 71 | + set_module_resolution_settings( |
| 72 | + &mut program, |
| 73 | + ModuleResolutionSettings { |
| 74 | + extra_paths: vec![], |
| 75 | + workspace_root: workspace_root.to_path_buf(), |
| 76 | + site_packages: None, |
| 77 | + custom_typeshed: None, |
| 78 | + }, |
| 79 | + ); |
| 80 | + |
| 81 | + program.workspace_mut().open_file(foo); |
| 82 | + |
| 83 | + let bar = system_path_to_file(&program, bar_path).unwrap(); |
| 84 | + let typing = system_path_to_file(&program, typing_path).unwrap(); |
| 85 | + |
| 86 | + Case { |
| 87 | + program, |
| 88 | + fs, |
| 89 | + foo, |
| 90 | + bar, |
| 91 | + typing, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +fn benchmark_without_parse(criterion: &mut Criterion) { |
| 96 | + let mut group = criterion.benchmark_group("red_knot/check_file"); |
| 97 | + group.throughput(Throughput::Bytes(FOO_CODE.len() as u64)); |
| 98 | + |
| 99 | + group.bench_function("red_knot_check_file[without_parse]", |b| { |
| 100 | + b.iter_batched( |
| 101 | + || { |
| 102 | + let case = setup_case(); |
| 103 | + // Pre-parse the module to only measure the semantic time. |
| 104 | + parsed_module(case.program.upcast(), case.foo); |
| 105 | + parsed_module(case.program.upcast(), case.bar); |
| 106 | + parsed_module(case.program.upcast(), case.typing); |
| 107 | + case |
| 108 | + }, |
| 109 | + |case| { |
| 110 | + let Case { program, foo, .. } = case; |
| 111 | + let result = program.check_file(foo).unwrap(); |
| 112 | + |
| 113 | + assert_eq!(result.as_slice(), [] as [String; 0]); |
| 114 | + }, |
| 115 | + BatchSize::SmallInput, |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + group.finish(); |
| 120 | +} |
| 121 | + |
| 122 | +fn benchmark_incremental(criterion: &mut Criterion) { |
| 123 | + let mut group = criterion.benchmark_group("red_knot/check_file"); |
| 124 | + group.throughput(Throughput::Bytes(FOO_CODE.len() as u64)); |
| 125 | + |
| 126 | + group.bench_function("red_knot_check_file[incremental]", |b| { |
| 127 | + b.iter_batched( |
| 128 | + || { |
| 129 | + let mut case = setup_case(); |
| 130 | + case.program.check_file(case.foo).unwrap(); |
| 131 | + |
| 132 | + case.fs |
| 133 | + .write_file( |
| 134 | + FileSystemPath::new("/src/foo.py"), |
| 135 | + format!("{BAR_CODE}\n# A comment\n"), |
| 136 | + ) |
| 137 | + .unwrap(); |
| 138 | + |
| 139 | + case.bar.touch(&mut case.program); |
| 140 | + case |
| 141 | + }, |
| 142 | + |case| { |
| 143 | + let Case { program, foo, .. } = case; |
| 144 | + let result = program.check_file(foo).unwrap(); |
| 145 | + |
| 146 | + assert_eq!(result.as_slice(), [] as [String; 0]); |
| 147 | + }, |
| 148 | + BatchSize::SmallInput, |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + group.finish(); |
| 153 | +} |
| 154 | + |
| 155 | +fn benchmark_cold(criterion: &mut Criterion) { |
| 156 | + let mut group = criterion.benchmark_group("red_knot/check_file"); |
| 157 | + group.throughput(Throughput::Bytes(FOO_CODE.len() as u64)); |
| 158 | + |
| 159 | + group.bench_function("red_knot_check_file[cold]", |b| { |
| 160 | + b.iter_batched( |
| 161 | + setup_case, |
| 162 | + |case| { |
| 163 | + let Case { program, foo, .. } = case; |
| 164 | + let result = program.check_file(foo).unwrap(); |
| 165 | + |
| 166 | + assert_eq!(result.as_slice(), [] as [String; 0]); |
| 167 | + }, |
| 168 | + BatchSize::SmallInput, |
| 169 | + ); |
| 170 | + }); |
| 171 | + |
| 172 | + group.finish(); |
| 173 | +} |
| 174 | + |
| 175 | +criterion_group!(cold, benchmark_without_parse); |
| 176 | +criterion_group!(without_parse, benchmark_cold); |
| 177 | +criterion_group!(incremental, benchmark_incremental); |
| 178 | +criterion_main!(without_parse, cold, incremental); |
0 commit comments