| 1 |
use askama::Template; |
| 2 |
use criterion::{Criterion, black_box, criterion_group, criterion_main}; |
| 3 |
|
| 4 |
criterion_main!(benches); |
| 5 |
criterion_group!(benches, functions); |
| 6 |
|
| 7 |
fn functions(c: &mut Criterion) { |
| 8 |
c.bench_function("escape JSON", escape_json); |
| 9 |
c.bench_function("escape JSON (pretty)", escape_json_pretty); |
| 10 |
c.bench_function("escape JSON for HTML", escape_json_for_html); |
| 11 |
c.bench_function("escape JSON for HTML (pretty)", escape_json_for_html_pretty); |
| 12 |
} |
| 13 |
|
| 14 |
fn escape_json(b: &mut criterion::Bencher<'_>) { |
| 15 |
#[derive(Template)] |
| 16 |
#[template(ext = "html", source = "{{self.0|json|safe}}")] |
| 17 |
struct Tmpl(&'static str); |
| 18 |
|
| 19 |
b.iter(|| { |
| 20 |
let mut len = 0; |
| 21 |
for &s in black_box(STRINGS) { |
| 22 |
len += Tmpl(s).to_string().len(); |
| 23 |
} |
| 24 |
len |
| 25 |
}); |
| 26 |
} |
| 27 |
|
| 28 |
fn escape_json_pretty(b: &mut criterion::Bencher<'_>) { |
| 29 |
#[derive(Template)] |
| 30 |
#[template(ext = "html", source = "{{self.0|json(2)|safe}}")] |
| 31 |
struct Tmpl(&'static str); |
| 32 |
|
| 33 |
b.iter(|| { |
| 34 |
let mut len = 0; |
| 35 |
for &s in black_box(STRINGS) { |
| 36 |
len += Tmpl(s).to_string().len(); |
| 37 |
} |
| 38 |
len |
| 39 |
}); |
| 40 |
} |
| 41 |
|
| 42 |
fn escape_json_for_html(b: &mut criterion::Bencher<'_>) { |
| 43 |
#[derive(Template)] |
| 44 |
#[template(ext = "html", source = "{{self.0|json}}")] |
| 45 |
struct Tmpl(&'static str); |
| 46 |
|
| 47 |
b.iter(|| { |
| 48 |
let mut len = 0; |
| 49 |
for &s in black_box(STRINGS) { |
| 50 |
len += Tmpl(s).to_string().len(); |
| 51 |
} |
| 52 |
len |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
fn escape_json_for_html_pretty(b: &mut criterion::Bencher<'_>) { |
| 57 |
#[derive(Template)] |
| 58 |
#[template(ext = "html", source = "{{self.0|json(2)}}")] |
| 59 |
struct Tmpl(&'static str); |
| 60 |
|
| 61 |
b.iter(|| { |
| 62 |
let mut len = 0; |
| 63 |
for &s in black_box(STRINGS) { |
| 64 |
len += Tmpl(s).to_string().len(); |
| 65 |
} |
| 66 |
len |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
const STRINGS: &[&str] = include!("strings.inc"); |