|
| 1 | +use content_tag::{Options, Preprocessor}; |
| 2 | +use std::time::Instant; |
| 3 | + |
| 4 | +fn bench_parse(name: &str, src: &str, iterations: u32) -> f64 { |
| 5 | + // Warmup |
| 6 | + for _ in 0..100 { |
| 7 | + let p = Preprocessor::new(); |
| 8 | + let _ = p.parse(src, Options::default()); |
| 9 | + } |
| 10 | + |
| 11 | + // Run 3 rounds, take the minimum |
| 12 | + let mut best = f64::MAX; |
| 13 | + for _ in 0..3 { |
| 14 | + let start = Instant::now(); |
| 15 | + for _ in 0..iterations { |
| 16 | + let p = Preprocessor::new(); |
| 17 | + let _ = p.parse(src, Options::default()); |
| 18 | + } |
| 19 | + let elapsed = start.elapsed(); |
| 20 | + let per_iter = elapsed.as_nanos() as f64 / iterations as f64; |
| 21 | + if per_iter < best { |
| 22 | + best = per_iter; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + println!( |
| 27 | + "{:<55} {:>8.1}µs per parse ({} chars)", |
| 28 | + name, |
| 29 | + best / 1000.0, |
| 30 | + src.len(), |
| 31 | + ); |
| 32 | + best / 1000.0 |
| 33 | +} |
| 34 | + |
| 35 | +fn main() { |
| 36 | + // Global warmup: run a few hundred parses to warm CPU caches |
| 37 | + // before any measured benchmarks. |
| 38 | + { |
| 39 | + let w = "import Component from '@glimmer/component';\nclass C extends Component { <template>hi</template> }"; |
| 40 | + for _ in 0..500 { |
| 41 | + let p = Preprocessor::new(); |
| 42 | + let _ = p.parse(w, Options::default()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // The same component is used as baseline across all tests. |
| 47 | + let base_component = r#" |
| 48 | +import Component from '@glimmer/component'; |
| 49 | +class Comp extends Component { |
| 50 | + <template> |
| 51 | + <div class="container"> |
| 52 | + <h1>{{this.title}}</h1> |
| 53 | + <p>{{this.description}}</p> |
| 54 | + </div> |
| 55 | + </template> |
| 56 | +} |
| 57 | +"#; |
| 58 | + |
| 59 | + // ========================================================= |
| 60 | + // Test 1: Scaling by number of templates |
| 61 | + // Same component repeated N times. |
| 62 | + // ========================================================= |
| 63 | + println!("=== Scaling by template count ===\n"); |
| 64 | + |
| 65 | + for repeats in [1, 2, 5, 10, 20] { |
| 66 | + let src = base_component.repeat(repeats); |
| 67 | + bench_parse( |
| 68 | + &format!("{} templates ({} chars)", repeats, src.len()), |
| 69 | + &src, |
| 70 | + 3000, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + // ========================================================= |
| 75 | + // Test 2: Scaling by template content size |
| 76 | + // Same component, but with extra rows inside the template. |
| 77 | + // ========================================================= |
| 78 | + println!("\n=== Scaling by template content size ===\n"); |
| 79 | + |
| 80 | + let extra_row = " <div class=\"item\">{{this.value}}</div>\n"; |
| 81 | + |
| 82 | + // Baseline: the component as-is (0 extra rows) |
| 83 | + bench_parse( |
| 84 | + &format!("0 extra rows ({} chars)", base_component.len()), |
| 85 | + base_component, |
| 86 | + 3000, |
| 87 | + ); |
| 88 | + |
| 89 | + for num_rows in [10, 50, 200] { |
| 90 | + let extra_content = extra_row.repeat(num_rows); |
| 91 | + let src = base_component.replace( |
| 92 | + " <p>{{this.description}}</p>", |
| 93 | + &format!(" <p>{{{{this.description}}}}</p>\n{}", extra_content), |
| 94 | + ); |
| 95 | + bench_parse( |
| 96 | + &format!("{} extra rows inside template ({} chars)", num_rows, src.len()), |
| 97 | + &src, |
| 98 | + 3000, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + // ========================================================= |
| 103 | + // Test 3: Scaling by JS code before the template |
| 104 | + // Same component, but with extra JS lines before it. |
| 105 | + // ========================================================= |
| 106 | + println!("\n=== Scaling by JS code before template ===\n"); |
| 107 | + |
| 108 | + let extra_line = "const x = 'some padding code to increase byte offset';\n"; |
| 109 | + |
| 110 | + // Baseline: the component as-is (0 extra lines) |
| 111 | + bench_parse( |
| 112 | + &format!("0 extra lines ({} chars)", base_component.len()), |
| 113 | + base_component, |
| 114 | + 3000, |
| 115 | + ); |
| 116 | + |
| 117 | + for num_lines in [10, 50, 200] { |
| 118 | + let prefix = extra_line.repeat(num_lines); |
| 119 | + let src = format!("{}{}", prefix, base_component); |
| 120 | + bench_parse( |
| 121 | + &format!( |
| 122 | + "{} extra JS lines before template ({} chars)", |
| 123 | + num_lines, |
| 124 | + src.len() |
| 125 | + ), |
| 126 | + &src, |
| 127 | + 3000, |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + // ========================================================= |
| 132 | + // Test 4: Typical real-world files |
| 133 | + // ========================================================= |
| 134 | + println!("\n=== Typical files ===\n"); |
| 135 | + |
| 136 | + let no_template = r#" |
| 137 | +import { tracked } from '@glimmer/tracking'; |
| 138 | +import { action } from '@ember/object'; |
| 139 | +import Service, { service } from '@ember/service'; |
| 140 | +
|
| 141 | +export default class AuthService extends Service { |
| 142 | + @service declare session: any; |
| 143 | + @tracked count = 0; |
| 144 | +
|
| 145 | + @action |
| 146 | + increment() { this.count++; } |
| 147 | +
|
| 148 | + get doubled() { return this.count * 2; } |
| 149 | +}"#; |
| 150 | + |
| 151 | + bench_parse( |
| 152 | + &format!("base component (1 template, {} chars)", base_component.len()), |
| 153 | + base_component, |
| 154 | + 5000, |
| 155 | + ); |
| 156 | + bench_parse("utility file (no template)", no_template, 5000); |
| 157 | +} |
0 commit comments