//! Parse timing for a generated 1 MB PHP file (budget: under 50 ms). //! //! Ignored by default because timings in debug builds mean nothing. Run in //! release on an idle machine: //! `cargo test --release -p lutra-syntax --test perf -- --ignored --nocapture`. #![allow( clippy::unwrap_used, clippy::indexing_slicing, clippy::string_slice, reason = "tests may panic" )] use std::time::{Duration, Instant}; use lutra_syntax::{Parser, folding, outline}; use lutra_text::Edit; /// Generates a PHP file of at least `bytes` bytes made of namespaced /// classes with properties, a promoted constructor, methods, arrays and /// doc comments. fn generate(bytes: usize) -> String { let mut text = String::from( " 1, 'b' => [2, 3], 'c' => "four {index}"]; public function __construct(private readonly Collection $collection, protected ?string $name = null) {{ }} public function find(int $id, string ...$tags): ?array {{ foreach ($this->items as $key => $value) {{ if ($key === $id && \in_array($value, $tags, true)) {{ return ['id' => $id, 'value' => $value]; }} }} return match ($id) {{ 0 => null, default => $this->collection->map(fn (int $x): int => $x * {index})->all(), }}; }} public function count(): int {{ return \count($this->items) + self::LIMIT; }} }} "# )); index += 1; } text } /// The fastest of `runs` timings of `work`. fn best_of(runs: usize, mut work: impl FnMut()) -> Duration { (0..runs) .map(|_| { let started = Instant::now(); work(); started.elapsed() }) .min() .unwrap() } #[test] #[ignore = "timing; run in release"] fn parse_one_megabyte() { let text = generate(1024 * 1024); let mut parser = Parser::new().unwrap(); let tree = parser.parse(&text); assert!(!tree.has_errors()); let parse = best_of(5, || { parser.parse(&text); }); let outline_time = best_of(5, || { outline(&tree); }); let folding_time = best_of(5, || { folding(&tree); }); let middle = text.len() / 2; let middle = (middle..text.len()) .find(|offset| text.is_char_boundary(*offset)) .unwrap(); let reparse = best_of(5, || { parser.reparse(&tree, &[Edit::insert(middle, " ")]).unwrap(); }); println!( "1 MB PHP ({} bytes, {} top-level items): parse {:?}, reparse after one keystroke {:?}, outline {:?}, folding {:?}", text.len(), outline(&tree).len(), parse, reparse, outline_time, folding_time ); }