11#![ allow( clippy:: disallowed_names) ]
22use ruff_benchmark:: criterion;
3+ use ruff_benchmark:: real_world_projects:: RealWorldProject ;
34
45use std:: ops:: Range ;
56
@@ -11,10 +12,10 @@ use ruff_benchmark::TestFile;
1112use ruff_db:: diagnostic:: { Diagnostic , DiagnosticId , Severity } ;
1213use ruff_db:: files:: { File , system_path_to_file} ;
1314use ruff_db:: source:: source_text;
14- use ruff_db:: system:: { MemoryFileSystem , SystemPath , SystemPathBuf , TestSystem } ;
15+ use ruff_db:: system:: { InMemorySystem , MemoryFileSystem , SystemPath , SystemPathBuf , TestSystem } ;
1516use ruff_python_ast:: PythonVersion ;
1617use ty_project:: metadata:: options:: { EnvironmentOptions , Options } ;
17- use ty_project:: metadata:: value:: RangedValue ;
18+ use ty_project:: metadata:: value:: { RangedValue , RelativePathBuf } ;
1819use ty_project:: watch:: { ChangeEvent , ChangedKind } ;
1920use ty_project:: { Db , ProjectDatabase , ProjectMetadata } ;
2021
@@ -347,10 +348,141 @@ fn benchmark_many_tuple_assignments(criterion: &mut Criterion) {
347348 } ) ;
348349}
349350
351+ #[ track_caller]
352+ fn bench_project ( project : RealWorldProject , criterion : & mut Criterion , max_diagnostics : usize ) {
353+ fn setup (
354+ metadata : & ProjectMetadata ,
355+ system : & TestSystem ,
356+ check_paths : & [ & SystemPath ] ,
357+ ) -> ProjectDatabase {
358+ // Create new database instance and collect files for this instance
359+ let mut db = ProjectDatabase :: new ( metadata. clone ( ) , system. clone ( ) ) . unwrap ( ) ;
360+
361+ db. project ( ) . set_included_paths (
362+ & mut db,
363+ check_paths. iter ( ) . map ( |path| path. to_path_buf ( ) ) . collect ( ) ,
364+ ) ;
365+ db
366+ }
367+
368+ fn check_project ( db : & mut ProjectDatabase , max_diagnostics : usize ) {
369+ let result = db. check ( ) ;
370+ // Don't assert specific diagnostic count for real-world projects
371+ // as they may have legitimate type issues
372+ let diagnostics = result. len ( ) ;
373+
374+ assert ! (
375+ diagnostics > 1 && diagnostics <= max_diagnostics,
376+ "Expected between {} and {} diagnostics but got {}" ,
377+ 1 ,
378+ max_diagnostics,
379+ diagnostics
380+ ) ;
381+ }
382+
383+ setup_rayon ( ) ;
384+
385+ let setup_project = project. setup ( ) . expect ( "Failed to setup project" ) ;
386+
387+ let fs = setup_project
388+ . copy_to_memory_fs ( )
389+ . expect ( "Failed to copy project to memory fs" ) ;
390+ let system = TestSystem :: new ( InMemorySystem :: from_memory_fs ( fs) ) ;
391+
392+ let src_root = SystemPath :: new ( "/" ) ;
393+ let mut metadata = ProjectMetadata :: discover ( src_root, & system) . unwrap ( ) ;
394+
395+ metadata. apply_options ( Options {
396+ environment : Some ( EnvironmentOptions {
397+ python_version : Some ( RangedValue :: cli ( setup_project. config . python_version ) ) ,
398+ python : ( !setup_project. config ( ) . dependencies . is_empty ( ) )
399+ . then_some ( RelativePathBuf :: cli ( SystemPath :: new ( ".venv" ) ) ) ,
400+ ..EnvironmentOptions :: default ( )
401+ } ) ,
402+ ..Options :: default ( )
403+ } ) ;
404+
405+ let check_paths = setup_project. check_paths ( ) ;
406+
407+ criterion. bench_function ( & format ! ( "project[{}]" , setup_project. config. name) , |b| {
408+ b. iter_batched_ref (
409+ || setup ( & metadata, & system, check_paths) ,
410+ |db| check_project ( db, max_diagnostics) ,
411+ BatchSize :: SmallInput ,
412+ ) ;
413+ } ) ;
414+ }
415+
416+ fn pydantic ( criterion : & mut Criterion ) {
417+ // Setup the colour-science project (expensive, done once)
418+ let project = RealWorldProject {
419+ name : "pydantic" ,
420+ repository : "https://github.com/pydantic/pydantic" ,
421+ commit : "0c4a22b64b23dfad27387750cf07487efc45eb05" ,
422+ paths : & [ SystemPath :: new ( "pydantic" ) ] ,
423+ dependencies : & [
424+ "annotated-types" ,
425+ "pydantic-core" ,
426+ "typing-extensions" ,
427+ "typing-inspection" ,
428+ ] ,
429+ max_dep_date : "2025-06-17" ,
430+ python_version : PythonVersion :: PY39 ,
431+ } ;
432+
433+ bench_project ( project, criterion, 1000 ) ;
434+ }
435+
436+ fn hydra ( criterion : & mut Criterion ) {
437+ // Setup the colour-science project (expensive, done once)
438+ let project = RealWorldProject {
439+ name : "hydra-zen" ,
440+ repository : "https://github.com/mit-ll-responsible-ai/hydra-zen" ,
441+ commit : "dd2b50a9614c6f8c46c5866f283c8f7e7a960aa8" ,
442+ paths : & [ SystemPath :: new ( "src" ) ] ,
443+ dependencies : & [ "pydantic" , "beartype" , "hydra-core" ] ,
444+ max_dep_date : "2025-06-17" ,
445+ python_version : PythonVersion :: PY313 ,
446+ } ;
447+
448+ bench_project ( project, criterion, 100 ) ;
449+ }
450+
451+ fn attrs ( criterion : & mut Criterion ) {
452+ // Setup the colour-science project (expensive, done once)
453+ let project = RealWorldProject {
454+ name : "attrs" ,
455+ repository : "https://github.com/python-attrs/attrs" ,
456+ commit : "a6ae894aad9bc09edc7cdad8c416898784ceec9b" ,
457+ paths : & [ SystemPath :: new ( "src" ) ] ,
458+ dependencies : & [ ] ,
459+ max_dep_date : "2025-06-17" ,
460+ python_version : PythonVersion :: PY313 ,
461+ } ;
462+
463+ bench_project ( project, criterion, 100 ) ;
464+ }
465+
466+ fn anyio ( criterion : & mut Criterion ) {
467+ // Setup the colour-science project (expensive, done once)
468+ let project = RealWorldProject {
469+ name : "anyio" ,
470+ repository : "https://github.com/agronholm/anyio" ,
471+ commit : "561d81270a12f7c6bbafb5bc5fad99a2a13f96be" ,
472+ paths : & [ SystemPath :: new ( "src" ) ] ,
473+ dependencies : & [ ] ,
474+ max_dep_date : "2025-06-17" ,
475+ python_version : PythonVersion :: PY313 ,
476+ } ;
477+
478+ bench_project ( project, criterion, 100 ) ;
479+ }
480+
350481criterion_group ! ( check_file, benchmark_cold, benchmark_incremental) ;
351482criterion_group ! (
352483 micro,
353484 benchmark_many_string_assignments,
354485 benchmark_many_tuple_assignments,
355486) ;
356- criterion_main ! ( check_file, micro) ;
487+ criterion_group ! ( project, anyio, attrs, hydra) ;
488+ criterion_main ! ( check_file, micro, project) ;
0 commit comments