//! Generated PHP projects for index tests and measurements. //! //! [`generate_php_project`] writes a deterministic Composer-style project of //! any size, almost all PHP: classes, interfaces, traits, enums and function //! files in `src/`, library classes in `vendor/` (gitignored, indexed as //! library code), a few templates and config files, an ignored `var/cache/` //! and an excluded `node_modules/`. Class files are about 5 KB, the size of //! typical framework classes, so 20,000 files are about 100 MB of PHP. use std::fs; use std::path::Path; /// What [`generate_php_project`] wrote. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub struct PhpProject { /// Files the index lists (everything but `var/` and `node_modules/`). pub indexed_files: usize, /// PHP files the index parses. pub php_files: usize, /// Of those, files under `vendor/`. pub vendor_files: usize, /// Classes, interfaces, traits and enums declared in indexed files. pub classes: usize, /// Bytes of indexed PHP. pub php_bytes: u64, } /// Methods per generated class. const METHODS: usize = 14; /// Writes a project of about `files` files below `root`. /// /// Shares: 80 % PHP in `src/` (one in ten an interface, one in twenty a /// trait, one in twenty an enum, one in fifty a file of functions, the rest /// classes), 15 % PHP in `vendor/acme/framework/src/`, 3 % Twig templates, /// 2 % YAML config; plus `var/cache/` (ignored) and `node_modules/` /// (excluded) with a few PHP files each that the index must skip. /// /// Class `src/Module{m}/Service{i}.php` declares `App\Module{m}\Service{i}` /// with methods `handle0`…, properties `$items` and `$name`, and constant /// `MARKER`; vendor class `i` is `Acme\Framework\Component{m}\Widget{i}`. /// /// # Panics /// If a file cannot be written (test helper). pub fn generate_php_project(root: &Path, files: usize) -> PhpProject { let mut out = PhpProject::default(); write(root, ".gitignore", "/var/\n/vendor/\n", &mut out, false); out.indexed_files += 1; let src = files.saturating_mul(80) / 100; for i in 0..src { let module = i / 100; let (name, text) = src_file(i, module); let rel = format!("src/Module{module:03}/{name}.php"); write(root, &rel, &text, &mut out, true); if i % 50 != 7 { out.classes += 1; } } let vendor = files.saturating_mul(15) / 100; for i in 0..vendor { let module = i / 100; let rel = format!("vendor/acme/framework/src/Component{module:03}/Widget{i}.php"); let text = class_text( &format!("Acme\\Framework\\Component{module:03}"), &format!("Widget{i}"), "class", ); write(root, &rel, &text, &mut out, true); out.vendor_files += 1; out.classes += 1; } for i in 0..files.saturating_mul(3) / 100 { let rel = format!("templates/page{i}.html.twig"); write( root, &rel, "{% block body %}

{{ content }}

{% endblock %}\n", &mut out, false, ); out.indexed_files += 1; } for i in 0..files.saturating_mul(2) / 100 { let rel = format!("config/packages/package{i}.yaml"); write( root, &rel, &format!("package{i}:\n enabled: true\n"), &mut out, false, ); out.indexed_files += 1; } for i in 0..3 { let text = class_text("Cache", &format!("Cached{i}"), "class"); fs::create_dir_all(root.join("var/cache")).unwrap(); fs::write(root.join(format!("var/cache/Cached{i}.php")), &text).unwrap(); fs::create_dir_all(root.join("node_modules/pkg")).unwrap(); fs::write(root.join(format!("node_modules/pkg/Hidden{i}.php")), &text).unwrap(); } out } /// Name and text of the `i`th source file in module `module`. fn src_file(i: usize, module: usize) -> (String, String) { let namespace = format!("App\\Module{module:03}"); match i % 50 { 7 => ( format!("functions{i}"), format!( " ( format!("Contract{i}"), class_text(&namespace, &format!("Contract{i}"), "interface"), ), n if n % 20 == 5 => ( format!("Helps{i}"), class_text(&namespace, &format!("Helps{i}"), "trait"), ), n if n % 20 == 15 => ( format!("Status{i}"), enum_text(&namespace, &format!("Status{i}")), ), _ => ( format!("Service{i}"), class_text(&namespace, &format!("Service{i}"), "class"), ), } } /// A class, interface or trait of about 5 KB. fn class_text(namespace: &str, name: &str, keyword: &str) -> String { let mut s = format!( "items[$key])) {{\n return $this->items[$key];\n }}\n $this->items[$key] = strtoupper($name);\n return null;\n }}\n\n" )); } s.push_str("}\n"); s } /// A backed enum with a few cases and a method. fn enum_text(namespace: &str, name: &str) -> String { format!( "value);\n }}\n}}\n" ) } /// Writes `text` to `root/rel`, creating directories, and counts it as an /// indexed PHP file if `php`. fn write(root: &Path, rel: &str, text: &str, out: &mut PhpProject, php: bool) { let path = root.join(rel); if let Some(parent) = path.parent() { fs::create_dir_all(parent).unwrap(); } fs::write(path, text).unwrap(); if php { out.indexed_files += 1; out.php_files += 1; out.php_bytes += text.len() as u64; } }