|
| 1 | +use sqlbuild_rules::build_identity::main::install_build_identity::install_build_identity; |
| 2 | +use sqlbuild_rules::engine::main::evaluate_rows::evaluate_rows; |
| 3 | +use sqlbuild_rules::models::{EvaluateRequest, RowsRequest}; |
| 4 | + |
| 5 | +/// Counts the heap allocations of this test binary, which unlike time do not depend on the machine. |
| 6 | +#[global_allocator] |
| 7 | +static ALLOCATOR: &stats_alloc::StatsAlloc<std::alloc::System> = &stats_alloc::INSTRUMENTED_SYSTEM; |
| 8 | + |
| 9 | +/// A shared helper, `modules` modules that import it, and one hook file of 150 functions per 100. |
| 10 | +pub(crate) fn importing_modules(modules: usize) -> Vec<(String, String)> { |
| 11 | + let helper = ( |
| 12 | + "python/shared/text.py".to_owned(), |
| 13 | + "\"\"\"Shared text.\"\"\"\n\n\ndef trimmed(value: str) -> str:\n \"\"\"Return the value.\"\"\"\n return value\n".to_owned(), |
| 14 | + ); |
| 15 | + std::iter::once(helper) |
| 16 | + .chain((0..modules).map(|module| { |
| 17 | + ( |
| 18 | + format!("python/area_{}/module_{module}/entry.py", module % 40), |
| 19 | + format!( |
| 20 | + "\"\"\"Module {module}.\"\"\"\n\nfrom python.shared.text import trimmed\n\n\ndef handle_{module}(value: str) -> str:\n \"\"\"Handle module {module}.\"\"\"\n return trimmed(value)\n" |
| 21 | + ), |
| 22 | + ) |
| 23 | + })) |
| 24 | + .chain((0..modules / 100).map(|hook| { |
| 25 | + let body: String = (0..150) |
| 26 | + .map(|function| format!("def notify_{function}(ctx, reason):\n print(ctx, reason)\n\n\n")) |
| 27 | + .collect(); |
| 28 | + (format!("hooks/python/notify_{hook}.py"), body) |
| 29 | + })) |
| 30 | + .collect() |
| 31 | +} |
| 32 | + |
| 33 | +/// The codes found over `files` and the heap allocations and reallocations the evaluation made. |
| 34 | +pub(crate) fn counted_codes( |
| 35 | + files: &[(String, String)], |
| 36 | + config: &serde_json::Value, |
| 37 | +) -> (Vec<String>, usize) { |
| 38 | + install_build_identity("0123456789abcdef").expect("the test identity installs"); |
| 39 | + let directory = tempfile::tempdir().expect("project directory"); |
| 40 | + for (path, contents) in files { |
| 41 | + let file = directory.path().join(path); |
| 42 | + std::fs::create_dir_all(file.parent().expect("parent")).expect("module directory"); |
| 43 | + std::fs::write(&file, contents).expect("module file"); |
| 44 | + } |
| 45 | + let mut config = config.clone(); |
| 46 | + config["cache"] = serde_json::json!({ "enabled": false }); |
| 47 | + let request = RowsRequest { |
| 48 | + request: EvaluateRequest { |
| 49 | + project_dir: directory.path().to_string_lossy().into_owned(), |
| 50 | + config: serde_json::from_value(config).expect("rules config"), |
| 51 | + ..EvaluateRequest::default() |
| 52 | + }, |
| 53 | + memo: None, |
| 54 | + retain: false, |
| 55 | + }; |
| 56 | + let region = stats_alloc::Region::new(ALLOCATOR); |
| 57 | + let evaluation = evaluate_rows(request).expect("rules run"); |
| 58 | + let change = region.change(); |
| 59 | + let codes = evaluation |
| 60 | + .faults |
| 61 | + .into_iter() |
| 62 | + .map(|fault| fault.code) |
| 63 | + .collect(); |
| 64 | + (codes, change.allocations + change.reallocations) |
| 65 | +} |
0 commit comments