Browse Source

Sorting benches

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
11c89fa663
3 changed files with 46 additions and 4 deletions
  1. +27
    -0
      benches/sorting.rs
  2. +14
    -1
      benches/unit.rs
  3. +5
    -3
      src/content/sorting.rs

+ 27
- 0
benches/sorting.rs View File

@@ -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));
}

+ 14
- 1
benches/unit.rs View File

@@ -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(&section, 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));
}

+ 5
- 3
src/content/sorting.rs View File

@@ -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<Page>, sort_by: SortBy) -> (Vec<Page>, Vec<Page>) {
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<Page>, sort_by: SortBy) -> (Vec<Page>, Vec<Page>) {
}
}
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)


Loading…
Cancel
Save