From ce704097a4b6090376e8201782111d9903fb4ee0 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 4 Jul 2017 21:27:32 +0900 Subject: [PATCH] Add benches for rendering crate --- components/rendering/Cargo.toml | 1 - components/rendering/benches/all.rs | 100 +++++++++ components/rendering/src/markdown.rs | 287 ------------------------- components/rendering/tests/markdown.rs | 286 ++++++++++++++++++++++++ 4 files changed, 386 insertions(+), 288 deletions(-) create mode 100644 components/rendering/benches/all.rs create mode 100644 components/rendering/tests/markdown.rs diff --git a/components/rendering/Cargo.toml b/components/rendering/Cargo.toml index 540dd63c..3a59740d 100644 --- a/components/rendering/Cargo.toml +++ b/components/rendering/Cargo.toml @@ -13,7 +13,6 @@ slug = "0.1" serde = "1.0" serde_derive = "1.0" - errors = { path = "../errors" } front_matter = { path = "../front_matter" } utils = { path = "../utils" } diff --git a/components/rendering/benches/all.rs b/components/rendering/benches/all.rs new file mode 100644 index 00000000..999268cf --- /dev/null +++ b/components/rendering/benches/all.rs @@ -0,0 +1,100 @@ +#![feature(test)] +extern crate test; +extern crate tera; + +extern crate rendering; +extern crate front_matter; + +use std::collections::HashMap; + +use tera::Tera; +use rendering::{Context, markdown_to_html}; +use front_matter::InsertAnchor; + +static CONTENT: &'static str = r#" +# Modus cognitius profanam ne duae virtutis mundi + +## Ut vita + +Lorem markdownum litora, care ponto nomina, et ut aspicit gelidas sui et +purpureo genuit. Tamen colla venientis [delphina](http://nil-sol.com/ecquis) +Tusci et temptata citaeque curam isto ubi vult vulnere reppulit. + +- Seque vidit flendoque de quodam +- Dabit minimos deiecto caputque noctis pluma +- Leti coniunx est Helicen +- Illius pulvereumque Icare inpositos +- Vivunt pereo pluvio tot ramos Olenios gelidis +- Quater teretes natura inde + +### A subsection + +Protinus dicunt, breve per, et vivacis genus Orphei munere. Me terram [dimittere +casside](http://corpus.org/) pervenit saxo primoque frequentat genuum sorori +praeferre causas Libys. Illud in serpit adsuetam utrimque nunc haberent, +**terrae si** veni! Hectoreis potes sumite [Mavortis retusa](http://tua.org/) +granum captantur potuisse Minervae, frugum. + +> Clivo sub inprovisoque nostrum minus fama est, discordia patrem petebat precatur +absumitur, poena per sit. Foramina *tamen cupidine* memor supplex tollentes +dictum unam orbem, Anubis caecae. Viderat formosior tegebat satis, Aethiopasque +sit submisso coniuge tristis ubi! + +## Praeceps Corinthus totidem quem crus vultum cape + +```rs +#[derive(Debug)] +pub struct Site { + /// The base path of the gutenberg site + pub base_path: PathBuf, + /// The parsed config for the site + pub config: Config, + pub pages: HashMap, + pub sections: HashMap, + pub tera: Tera, + live_reload: bool, + output_path: PathBuf, + static_path: PathBuf, + pub tags: Option, + pub categories: Option, + /// A map of all .md files (section and pages) and their permalink + /// We need that if there are relative links in the content that need to be resolved + pub permalinks: HashMap, +} +``` + +## More stuff +And a shortcode: + +{{ youtube(id="my_youtube_id") }} + +### Another subsection +Gotta make the toc do a little bit of work + +# A big title + +- hello +- world +- ! + +```py +if __name__ == "__main__": + gen_site("basic-blog", [""], 250, paginate=True) +``` +"#; + +#[bench] +fn bench_markdown_to_html_with_highlighting(b: &mut test::Bencher) { + let tera_ctx = Tera::default(); + let permalinks_ctx = HashMap::new(); + let context = Context::new(&tera_ctx, true, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); + b.iter(|| markdown_to_html(CONTENT, &context)); +} + +#[bench] +fn bench_markdown_to_html_without_highlighting(b: &mut test::Bencher) { + let tera_ctx = Tera::default(); + let permalinks_ctx = HashMap::new(); + let context = Context::new(&tera_ctx, false, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); + b.iter(|| markdown_to_html(CONTENT, &context)); +} diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index 1b2fa289..99258de6 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -262,290 +262,3 @@ pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec None => Ok((html.replace("

", ""), make_table_of_contents(headers))), } } - - -#[cfg(test)] -mod tests { - use std::collections::HashMap; - - use tera::Tera; - - use front_matter::InsertAnchor; - use templates::GUTENBERG_TERA; - use context::Context; - - use super::markdown_to_html; - - #[test] - fn can_do_markdown_to_html_simple() { - let tera_ctx = Tera::default(); - let permalinks_ctx = HashMap::new(); - let context = Context::new(&tera_ctx, true, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); - let res = markdown_to_html("hello", &context).unwrap(); - assert_eq!(res.0, "

hello

\n"); - } - - #[test] - fn doesnt_highlight_code_block_with_highlighting_off() { - let tera_ctx = Tera::default(); - let permalinks_ctx = HashMap::new(); - let mut context = Context::new(&tera_ctx, true, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); - context.highlight_code = false; - let res = markdown_to_html("```\n$ gutenberg server\n```", &context).unwrap(); - assert_eq!( - res.0, - "
$ gutenberg server\n
\n" - ); - } - - #[test] - fn can_highlight_code_block_no_lang() { - let tera_ctx = Tera::default(); - let permalinks_ctx = HashMap::new(); - let context = Context::new(&tera_ctx, true, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); - let res = markdown_to_html("```\n$ gutenberg server\n$ ping\n```", &context).unwrap(); - assert_eq!( - res.0, - "
\n$ gutenberg server\n$ ping\n
" - ); - } - - #[test] - fn can_highlight_code_block_with_lang() { - let tera_ctx = Tera::default(); - let permalinks_ctx = HashMap::new(); - let context = Context::new(&tera_ctx, true, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); - let res = markdown_to_html("```python\nlist.append(1)\n```", &context).unwrap(); - assert_eq!( - res.0, - "
\nlist.append(1)\n
" - ); - } - - #[test] - fn can_higlight_code_block_with_unknown_lang() { - let tera_ctx = Tera::default(); - let permalinks_ctx = HashMap::new(); - let context = Context::new(&tera_ctx, true, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); - let res = markdown_to_html("```yolo\nlist.append(1)\n```", &context).unwrap(); - // defaults to plain text - assert_eq!( - res.0, - "
\nlist.append(1)\n
" - ); - } - - #[test] - fn can_render_shortcode() { - let permalinks_ctx = HashMap::new(); - let context = Context::new(&GUTENBERG_TERA, true, "base16-ocean-dark".to_string(), "", &permalinks_ctx, InsertAnchor::None); - let res = markdown_to_html(r#" -Hello - -{{ youtube(id="ub36ffWAqgQ") }} - "#, &context).unwrap(); - assert!(res.0.contains("

Hello

\n
")); - assert!(res.0.contains(r#"