Skip to content

Commit

Permalink
tests: add criterion bench
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Aug 26, 2024
1 parent abe81ee commit 7da3b0a
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 1 deletion.
150 changes: 149 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ update-informer = ["dep:update-informer"]
web-image = ["dep:reqwest"]
dump-json = ["dep:serde_json"]

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }

[[bench]]
name = "template"
harness = false

[dependencies]
# logging
log = "0.4.17"
Expand Down
50 changes: 50 additions & 0 deletions benches/template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::hint::black_box;

Check warning on line 1 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs

Check warning on line 1 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs
use criterion::{criterion_group, criterion_main, Criterion};

use matugen::{scheme::{get_custom_color_schemes, get_schemes, SchemesEnum}, template_util::template::{self, get_render_data, render_template}};
use template::add_engine_filters;
use upon::{Engine, Syntax};

fn parse_template(data: &str) {
let source_color = material_colors::color::Argb::new(255, 255, 0, 0);

let syntax = Syntax::builder().expr("{{", "}}").block("<*", "*>").build();
let mut engine = Engine::with_syntax(syntax);

add_engine_filters(&mut engine);

Check warning on line 14 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs

Check warning on line 14 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs

let (scheme_dark, scheme_light) = get_schemes(source_color, &None, &None);
let schemes = get_custom_color_schemes(
source_color,
scheme_dark,
scheme_light,
&None,
&None,
&None
);
let render_data = get_render_data(&schemes, &source_color,&SchemesEnum::Dark, &None, None).unwrap();

engine.add_template("a", data.repeat(50)).expect("failed to add template");
render_template(&engine, &"a".to_string(), &render_data, None).expect("failed to render template");
}

fn criterion_benchmark(c: &mut Criterion) {
let data =

Check warning on line 32 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs

Check warning on line 32 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs
r#"
<* for name, value in colors *>
{{name}} {{value.default.rgba}};
<* endfor *>
"#;

Check warning on line 37 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs

Check warning on line 37 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs
let data_filter =
r#"
<* for name, value in colors *>
{{name | replace: "_", "-" }} {{value.default.rgba | set_alpha: 0.7 | set_hue: -180.0 }};
<* endfor *>
"#;

Check warning on line 43 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs

Check warning on line 43 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs

c.bench_function("parse 20", |b| b.iter(|| parse_template(black_box(&data.repeat(20)))));
c.bench_function("parse 20 filters", |b| b.iter(|| parse_template(black_box(&data_filter.repeat(20)))));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

Check warning on line 50 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs

Check warning on line 50 in benches/template.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/matugen/matugen/benches/template.rs

0 comments on commit 7da3b0a

Please sign in to comment.