From 11c89fa6637a7538cd540cae58feb639712c02de Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sat, 1 Jul 2017 12:06:49 +0900 Subject: [PATCH] Sorting benches --- benches/sorting.rs | 27 +++++++++++++++++++++++++++ benches/unit.rs | 15 ++++++++++++++- src/content/sorting.rs | 8 +++++--- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 benches/sorting.rs diff --git a/benches/sorting.rs b/benches/sorting.rs new file mode 100644 index 00000000..429d0430 --- /dev/null +++ b/benches/sorting.rs @@ -0,0 +1,27 @@ +//! Benchmarking separate functions of Gutenberg + +#![feature(test)] +extern crate test; +extern crate gutenberg; +extern crate tempdir; + +use std::env; + +use tempdir::TempDir; +use gutenberg::{Site, sort_pages, SortBy}; + +fn setup_site(name: &str) -> Site { + let mut path = env::current_dir().unwrap().to_path_buf(); + path.push("benches"); + path.push(name); + let mut site = Site::new(&path, "config.toml").unwrap(); + site.load().unwrap(); + site +} + +#[bench] +fn bench_sort_pages_medium_blog(b: &mut test::Bencher) { + let mut site = setup_site("huge-blog"); + let section = site.sections.values().next().unwrap().clone(); + b.iter(|| sort_pages(section.pages.clone(), SortBy::None)); +} diff --git a/benches/unit.rs b/benches/unit.rs index 67db371b..ac0f000b 100644 --- a/benches/unit.rs +++ b/benches/unit.rs @@ -8,7 +8,7 @@ extern crate tempdir; use std::env; use tempdir::TempDir; -use gutenberg::Site; +use gutenberg::{Site, sort_pages}; fn setup_site(name: &str) -> Site { @@ -58,3 +58,16 @@ fn bench_render_small_section_with_pages_and_no_pagination(b: &mut test::Bencher section.meta.template = None; b.iter(|| site.render_section(§ion, true).unwrap()); } + + +#[bench] +fn bench_sort_sections_pages_medium_blog(b: &mut test::Bencher) { + let mut site = setup_site("medium-blog"); + b.iter(|| site.sort_sections_pages(None)); +} + +#[bench] +fn bench_sort_sections_pages_medium_kb(b: &mut test::Bencher) { + let mut site = setup_site("medium-kb"); + b.iter(|| site.sort_sections_pages(None)); +} diff --git a/src/content/sorting.rs b/src/content/sorting.rs index 5238d440..bfdc741c 100644 --- a/src/content/sorting.rs +++ b/src/content/sorting.rs @@ -1,3 +1,5 @@ +use rayon::prelude::*; + use content::Page; #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] @@ -25,7 +27,7 @@ pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { cannot_be_sorted.push(page); } } - can_be_sorted.sort_by(|a, b| b.meta.date().unwrap().cmp(&a.meta.date().unwrap())); + can_be_sorted.par_sort_unstable_by(|a, b| b.meta.date().unwrap().cmp(&a.meta.date().unwrap())); (can_be_sorted, cannot_be_sorted) }, @@ -40,9 +42,9 @@ pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { } } if sort_by == SortBy::Order { - can_be_sorted.sort_by(|a, b| b.meta.order().cmp(&a.meta.order())); + can_be_sorted.par_sort_unstable_by(|a, b| b.meta.order().cmp(&a.meta.order())); } else { - can_be_sorted.sort_by(|a, b| a.meta.order().cmp(&b.meta.order())); + can_be_sorted.par_sort_unstable_by(|a, b| a.meta.order().cmp(&b.meta.order())); } (can_be_sorted, cannot_be_sorted)