|
| 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 | + "{:<35} {:>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 | + println!("=== Fine-grained scaling: 1 to 20 templates (ASCII) ===\n"); |
| 37 | + |
| 38 | + let chunk = r#" |
| 39 | +import Component from '@glimmer/component'; |
| 40 | +class Comp extends Component { |
| 41 | + <template> |
| 42 | + <div class="container"> |
| 43 | + <h1>{{this.title}}</h1> |
| 44 | + <p>{{this.description}}</p> |
| 45 | + </div> |
| 46 | + </template> |
| 47 | +} |
| 48 | +"#; |
| 49 | + |
| 50 | + let mut results = Vec::new(); |
| 51 | + for repeats in 1..=20 { |
| 52 | + let src = chunk.repeat(repeats); |
| 53 | + let us = bench_parse( |
| 54 | + &format!("{:>2} templates ({:>4} chars)", repeats, src.len()), |
| 55 | + &src, |
| 56 | + 3000, |
| 57 | + ); |
| 58 | + results.push((repeats, src.len(), us)); |
| 59 | + } |
| 60 | + |
| 61 | + println!("\n=== Per-template marginal cost ===\n"); |
| 62 | + for i in 1..results.len() { |
| 63 | + let (t, _, us) = results[i]; |
| 64 | + let (_, _, prev_us) = results[i - 1]; |
| 65 | + let marginal = us - prev_us; |
| 66 | + println!( |
| 67 | + "template {:>2}: +{:>5.1}µs marginal cost ({:.1}µs total, {:.1}µs/template avg)", |
| 68 | + t, |
| 69 | + marginal, |
| 70 | + us, |
| 71 | + us / t as f64 |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + println!("\n=== Non-ASCII scaling: 1 to 10 templates ===\n"); |
| 76 | + |
| 77 | + let mb_chunk = " |
| 78 | +import Component from '@glimmer/component'; |
| 79 | +class Comp extends Component { |
| 80 | + <template> |
| 81 | + <div class=\"container\"> |
| 82 | + <h1>{{this.title}} 🎉 中文</h1> |
| 83 | + <p>{{this.description}}</p> |
| 84 | + </div> |
| 85 | + </template> |
| 86 | +} |
| 87 | +"; |
| 88 | + |
| 89 | + for repeats in [1, 2, 3, 5, 10] { |
| 90 | + let src = mb_chunk.repeat(repeats); |
| 91 | + bench_parse( |
| 92 | + &format!( |
| 93 | + "{:>2} templates, multibyte ({:>4} chars)", |
| 94 | + repeats, |
| 95 | + src.len() |
| 96 | + ), |
| 97 | + &src, |
| 98 | + 3000, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + println!("\n=== Typical .gts files ===\n"); |
| 103 | + |
| 104 | + let small = r#" |
| 105 | +import Component from '@glimmer/component'; |
| 106 | +export default class extends Component { |
| 107 | + <template><div>{{this.title}}</div></template> |
| 108 | +}"#; |
| 109 | + |
| 110 | + let no_template = r#" |
| 111 | +import { tracked } from '@glimmer/tracking'; |
| 112 | +import { action } from '@ember/object'; |
| 113 | +import Service, { service } from '@ember/service'; |
| 114 | +
|
| 115 | +export default class AuthService extends Service { |
| 116 | + @service declare session: any; |
| 117 | + @tracked count = 0; |
| 118 | +
|
| 119 | + @action |
| 120 | + increment() { this.count++; } |
| 121 | +
|
| 122 | + get doubled() { return this.count * 2; } |
| 123 | +}"#; |
| 124 | + |
| 125 | + bench_parse("small component (1 template)", small, 5000); |
| 126 | + bench_parse("utility file (no template)", no_template, 5000); |
| 127 | +} |
0 commit comments