Skip to content

Commit

Permalink
Merge pull request gobanos#17 from gobanos/try_run
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
gobanos authored Dec 6, 2018
2 parents ef051c4 + f6a3d57 commit 3f0261a
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 84 deletions.
110 changes: 52 additions & 58 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-aoc"
version = "0.1.4"
version = "0.2.0"
authors = ["Grégory Obanos <gregory.obanos@gmail.com>", "Olivier Pinon <oliv.pinon@gmail.com>"]
description = "Cargo Advent of Code Helper"
license = "MIT/Apache-2.0"
Expand Down
103 changes: 96 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,15 @@ impl AOCApp {
let cargo_content = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/template/Cargo-run.toml.tpl"
)).replace("{CRATE_NAME}", &pm.name);
)).replace("{CRATE_NAME}", &pm.name)
.replace(
"{PROFILE}",
if args.is_present("profile") {
"[profile.release]\ndebug = true"
} else {
""
},
);

let template = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
Expand All @@ -183,11 +191,9 @@ impl AOCApp {
)
};

let input = format!("{}/day{}.txt", year, dp.day.0);

body += &template
.replace("{DAY}", &day.0.to_string())
.replace("{RUNNER_NAME}", &name)
.replace("{INPUT}", &input)
.replace("{RUNNER_DISPLAY}", &display);
}

Expand All @@ -202,6 +208,7 @@ impl AOCApp {
"/template/src/main.rs.tpl"
)).replace("{CRATE_SLUG}", &pm.slug)
.replace("{YEAR}", &day_parts.year.to_string())
.replace("{INPUT}", &template_input(day, year))
.replace("{BODY}", &body);

fs::create_dir_all("target/aoc/aoc-autobuild/src")
Expand Down Expand Up @@ -245,7 +252,15 @@ impl AOCApp {
let cargo_content = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/template/Cargo-bench.toml.tpl"
)).replace("{CRATE_NAME}", &pm.name);
)).replace("{CRATE_NAME}", &pm.name)
.replace(
"{PROFILE}",
if args.is_present("profile") {
"[profile.release]\ndebug = true"
} else {
""
},
);

let bench_tpl = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
Expand All @@ -257,11 +272,21 @@ impl AOCApp {
"/template/benches/part.rs.tpl"
));

let gen_tpl = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/template/benches/gen.rs.tpl"
));

let impl_tpl = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/template/benches/impl.rs.tpl"
));

let gen_impl_tpl = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/template/benches/gen_impl.rs.tpl"
));

let matching_parts = day_parts.iter().filter(|dp| dp.day == day).filter(|dp| {
if let Some(p) = part {
dp.part == p
Expand Down Expand Up @@ -301,7 +326,7 @@ impl AOCApp {
} else {
format!("day{}_part{}", dp.day.0, dp.part.0)
},
).replace("{INPUT}", &format!("{}/day{}.txt", year, dp.day.0))
).replace("{DAY}", &dp.day.0.to_string())
.replace(
"{NAME}",
if let Some(n) = &dp.name {
Expand All @@ -318,11 +343,67 @@ impl AOCApp {
Err("No matching day & part found")?;
}

let gens = if args.is_present("generator") {
let mut parts: Vec<_> = matching_parts.clone().map(|dp| dp.part).collect();
parts.sort();
parts.dedup();

parts
.into_iter()
.map(|p| {
let gen_name = format!("day{}", day.0);
gen_tpl
.replace("{GEN_NAME}", &gen_name)
.replace("{DAY}", &day.0.to_string())
.replace(
"{IMPLS}",
&matching_parts
.clone()
.filter(|dp| dp.part == p)
.map(|dp| {
gen_impl_tpl
.replace(
"{RUNNER_NAME}",
&if let Some(n) = &dp.name {
format!(
"day{}_part{}_{}",
dp.day.0,
dp.part.0,
n.to_lowercase()
)
} else {
format!("day{}_part{}", dp.day.0, dp.part.0)
},
).replace("{DAY}", &dp.day.0.to_string())
.replace(
"{NAME}",
if let Some(n) = &dp.name {
&n
} else {
"(default)"
},
).replace("{GEN_NAME}", &gen_name)
}).collect::<String>(),
)
}).collect()
} else {
String::new()
};

self.download_input(day, year)?;

let main_content = bench_tpl
.replace("{CRATE_SLUG}", &pm.slug)
.replace("{PARTS}", &body);
.replace("{PARTS}", &body)
.replace("{GENS}", &gens)
.replace(
"{BENCHMARKS}",
if args.is_present("generator") {
"aoc_benchmark, input_benchmark"
} else {
"aoc_benchmark"
},
).replace("{INPUTS}", &template_input(day, year));

fs::create_dir_all("target/aoc/aoc-autobench/benches")
.expect("failed to create autobench directory");
Expand Down Expand Up @@ -357,3 +438,11 @@ impl AOCApp {
Ok(())
}
}

fn template_input(day: Day, year: u32) -> String {
include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/template/input.rs.tpl"
)).replace("{DAY}", &day.0.to_string())
.replace("{YEAR}", &year.to_string())
}
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use app::AOCApp;
fn main() {
// Parses the attributes (CLAP)
let matches = App::new("cargo-aoc")
.version("1.0")
.version("0.2.0")
.about("Cargo helper for Advent of Code")
.author("gobanos <gregory.obanos@gmail.com>")
.arg(Arg::with_name("dummy").hidden(true).possible_value("aoc"))
Expand All @@ -35,6 +35,10 @@ fn main() {
.short("p")
.help("Specifies the part. Defaults to both parts.")
.takes_value(true),
).arg(
Arg::with_name("profile")
.short("x")
.help("Add debug info for profiling tools."),
).subcommand(
SubCommand::with_name("bench")
.about("Benchmark your solutions")
Expand All @@ -52,6 +56,14 @@ fn main() {
Arg::with_name("open")
.short("o")
.help("Opens the benchmark information in the browser"),
).arg(
Arg::with_name("generator")
.short("g")
.help("Also benchmark generator functions."),
).arg(
Arg::with_name("profile")
.short("x")
.help("Add debug info for profiling tools."),
),
).subcommand(
SubCommand::with_name("credentials")
Expand Down
6 changes: 4 additions & 2 deletions template/Cargo-bench.toml.tpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
[package]
name = "aoc-autobench"
version = "0.1.0"
version = "0.2.0"
authors = ["Grégory Obanos <gregory.obanos@gmail.com>"]

[dependencies]
{CRATE_NAME} = { path = "../../.." }
aoc-runner = "0.1.0"
aoc-runner = "0.2.0"

[dev-dependencies]
criterion = "0.2.5"

{PROFILE}

[[bench]]
name = "aoc_benchmark"
harness = false
6 changes: 4 additions & 2 deletions template/Cargo-run.toml.tpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[package]
name = "aoc-autobuild"
version = "0.1.0"
version = "0.2.0"
authors = ["Grégory Obanos <gregory.obanos@gmail.com>"]

[dependencies]
{CRATE_NAME} = { path = "../../.." }
aoc-runner = "0.1.0"
aoc-runner = "0.2.0"

{PROFILE}
18 changes: 17 additions & 1 deletion template/benches/aoc_benchmark.rs.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ use {CRATE_SLUG}::*;
use aoc_runner::ArcStr;
use criterion::Criterion;
use criterion::Fun;
use std::fmt::Display;

#[inline]
fn black_box(t: &dyn Display) {
criterion::black_box(t);
}

fn aoc_benchmark(c: &mut Criterion) {
{INPUTS}

{PARTS}
}

criterion_group!(benches, aoc_benchmark);
#[allow(unused_variables)]
#[allow(dead_code)]
fn input_benchmark(c: &mut Criterion) {
{INPUTS}

{GENS}
}

criterion_group!(benches, {BENCHMARKS});
criterion_main!(benches);
6 changes: 6 additions & 0 deletions template/benches/gen.rs.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

let mut {GEN_NAME} = Vec::new();

{IMPLS}

c.bench_functions("Generator Day{DAY}", {GEN_NAME}, ());
6 changes: 6 additions & 0 deletions template/benches/gen_impl.rs.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

{
let input = input_day{DAY}.clone();
let fun = Fun::new("{NAME}", move |b, _| b.iter(|| Factory::{RUNNER_NAME}(input.clone()).unwrap()));
{GEN_NAME}.push(fun);
}
5 changes: 3 additions & 2 deletions template/benches/impl.rs.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

{
let runner = Factory::{RUNNER_NAME}(ArcStr::from(include_str!("../../../../input/{INPUT}")));
let fun = Fun::new("{NAME}", move |b, _| b.iter(|| runner.run()));
let runner = Factory::{RUNNER_NAME}(input_day{DAY}.clone())
.expect("failed to generate input for {NAME}");
let fun = Fun::new("{NAME}", move |b, _| b.iter(|| runner.bench(black_box)));
{PART_NAME}.push(fun);
}
2 changes: 2 additions & 0 deletions template/input.rs.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

let input_day{DAY} = ArcStr::from(include_str!("../../../../input/{YEAR}/day{DAY}.txt"));
4 changes: 4 additions & 0 deletions template/src/main.rs.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ extern crate {CRATE_SLUG};
extern crate aoc_runner;

use {CRATE_SLUG}::*;
use std::time::Instant;
use aoc_runner::ArcStr;

fn main() {
println!("AOC {YEAR}");
{INPUT}

{BODY}
}
28 changes: 18 additions & 10 deletions template/src/runner.rs.tpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
{
use std::time::Instant;
use aoc_runner::ArcStr;

let start_time = Instant::now();
let runner = Factory::{RUNNER_NAME}(ArcStr::from(include_str!("../../../../input/{INPUT}")));
let inter_time = Instant::now();
let result = runner.run();
let final_time = Instant::now();
println!("{RUNNER_DISPLAY} : {}\n\tgenerator: {:?},\n\trunner: {:?}\n", result, (inter_time - start_time), (final_time - inter_time));
}
{
let start_time = Instant::now();
match Factory::{RUNNER_NAME}(input_day{DAY}.clone()) {
Ok(runner) => {
let inter_time = Instant::now();
match runner.try_run() {
Ok(result) => {
let final_time = Instant::now();
println!("{RUNNER_DISPLAY} : {}\n\tgenerator: {:?},\n\trunner: {:?}\n", result, (inter_time - start_time), (final_time - inter_time));
},
Err(e) => eprintln!("{RUNNER_DISPLAY} : FAILED while running :\n{:#?}\n", e)
}
},
Err(e) => eprintln!("{RUNNER_DISPLAY} : FAILED while generating :\n{:#?}\n", e)
}
}

0 comments on commit 3f0261a

Please sign in to comment.