Add throughput to derive benchmark

This commit is contained in:
René Kijewski 2025-04-20 23:33:50 +02:00
parent 4c0398ad67
commit 704a33beda
2 changed files with 14 additions and 5 deletions

View File

@ -23,7 +23,7 @@ required-features = ["__standalone"]
parser = { package = "askama_parser", version = "=0.13.0", path = "../askama_parser" }
basic-toml = { version = "0.1.1", optional = true }
pulldown-cmark = { version = "0.12.0", optional = true, default-features = false }
pulldown-cmark = { version = "0.13.0", optional = true, default-features = false }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }

View File

@ -1,30 +1,37 @@
use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
use criterion::{BatchSize, Criterion, Throughput, criterion_group, criterion_main};
use quote::quote;
criterion_main!(benches);
criterion_group!(benches, hello_world, librustdoc);
fn hello_world(c: &mut Criterion) {
let source = "<html><body><h1>Hello, {{user}}!</h1></body></html>";
let ts = quote! {
#[derive(Template)]
#[template(
source = "<html><body><h1>Hello, {{user}}!</h1></body></html>",
source = #source,
ext = "html"
)]
struct Hello<'a> {
user: &'a str,
}
};
c.bench_function("hello_world", |b| {
let mut g = c.benchmark_group("synthetic");
g.throughput(Throughput::Bytes(source.len().try_into().unwrap()));
g.bench_function("hello_world", |b| {
b.iter_batched(
|| ts.clone(),
askama_derive_standalone::derive_template,
BatchSize::LargeInput,
);
});
g.finish();
}
fn librustdoc(c: &mut Criterion) {
let mut g = c.benchmark_group("librustdoc");
macro_rules! benches {
($($name:expr => $struct:item)*) => { $({
const SOURCE: &str =
@ -35,7 +42,8 @@ fn librustdoc(c: &mut Criterion) {
#[template(source = #SOURCE, ext = "html")]
$struct
};
c.bench_function($name, |b| {
g.throughput(Throughput::Bytes(SOURCE.len().try_into().unwrap()));
g.bench_function($name, |b| {
b.iter_batched(
|| ts.clone(),
askama_derive_standalone::derive_template,
@ -133,4 +141,5 @@ fn librustdoc(c: &mut Criterion) {
size: u64,
}
}
g.finish();
}