|
| 1 | +mod support; |
| 2 | + |
| 3 | +use divan::Bencher; |
| 4 | +use support::db::Db; |
| 5 | +use support::fixtures::template_fixtures; |
| 6 | +use support::fixtures::TemplateFixture; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + divan::main(); |
| 10 | +} |
| 11 | + |
| 12 | +#[divan::bench(args = template_fixtures())] |
| 13 | +fn parse_template_fixture(fixture: &TemplateFixture) { |
| 14 | + let mut db = Db::new(); |
| 15 | + let file = db.file_with_contents(fixture.path.clone(), &fixture.source); |
| 16 | + if let Some(nodelist) = djls_templates::parse_template(&db, file) { |
| 17 | + divan::black_box(nodelist.nodelist(&db).len()); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +#[divan::bench] |
| 22 | +fn parse_all_templates(bencher: Bencher) { |
| 23 | + let fixtures = template_fixtures(); |
| 24 | + bencher.bench_local(|| { |
| 25 | + let mut db = Db::new(); |
| 26 | + for fixture in fixtures { |
| 27 | + let file = db.file_with_contents(fixture.path.clone(), &fixture.source); |
| 28 | + if let Some(nodelist) = djls_templates::parse_template(&db, file) { |
| 29 | + divan::black_box(nodelist.nodelist(&db).len()); |
| 30 | + } |
| 31 | + } |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +#[divan::bench(args = template_fixtures())] |
| 36 | +fn parse_template_incremental(bencher: Bencher, fixture: &TemplateFixture) { |
| 37 | + let mut db = Db::new(); |
| 38 | + let file = db.file_with_contents(fixture.path.clone(), &fixture.source); |
| 39 | + |
| 40 | + // Prime caches with the baseline source so the benchmark measures the incremental path. |
| 41 | + let _ = djls_templates::parse_template(&db, file); |
| 42 | + |
| 43 | + let original = fixture.source.clone(); |
| 44 | + let modified = { |
| 45 | + let mut text = original.clone(); |
| 46 | + text.push(' '); |
| 47 | + text |
| 48 | + }; |
| 49 | + |
| 50 | + let mut revision = 1_u64; |
| 51 | + let mut use_modified = true; |
| 52 | + |
| 53 | + bencher.bench_local(move || { |
| 54 | + let contents = if use_modified { &modified } else { &original }; |
| 55 | + use_modified = !use_modified; |
| 56 | + |
| 57 | + db.set_file_contents(file, contents, revision); |
| 58 | + revision = revision.wrapping_add(1); |
| 59 | + |
| 60 | + if let Some(nodelist) = djls_templates::parse_template(&db, file) { |
| 61 | + divan::black_box(nodelist.nodelist(&db).len()); |
| 62 | + } |
| 63 | + }); |
| 64 | +} |
0 commit comments