-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cli binary and flamegraph generation script
- Loading branch information
Showing
4 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
Cargo.lock | ||
flamegraph.svg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/sh | ||
|
||
echo building CLI with debug enabled | ||
CARGO_PROFILE_RELEASE_DEBUG=true cargo build --release --features cli | ||
|
||
echo recording perfomance for 10 seconds | ||
# infinitely cycle through sample_text.txt and send lines to cli | ||
timeout 10 sh -c 'while true; do cat tests/sample_text.txt; done | while read line; do echo "$line"; done' | CARGO_PROFILE_RELEASE_DEBUG=true perf record --call-graph dwarf -- target/release/pink_accents -a examples/linux.ron > /dev/null | ||
|
||
echo folding stacks | ||
perf script | inferno-collapse-perf > stacks.folded | ||
|
||
echo generating flamegraph.svg | ||
cat stacks.folded | inferno-flamegraph > flamegraph.svg | ||
|
||
echo cleaning up | ||
rm perf.data | ||
rm stacks.folded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::{ | ||
fs::{self, File}, | ||
io::{self, BufRead}, | ||
path::PathBuf, | ||
}; | ||
|
||
use clap::Parser; | ||
|
||
use pink_accents::Accent; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(version, about)] | ||
struct Args { | ||
/// Accent file path (currently only ron supported) | ||
#[arg(short, long, group = "accent_def")] | ||
accent: Option<PathBuf>, | ||
|
||
/// Directly provided accent (ron format) | ||
#[arg(long, group = "accent_def")] | ||
accent_string: Option<String>, | ||
|
||
/// Accent severity | ||
#[arg(short, long, default_value_t = 0)] | ||
severity: u64, | ||
|
||
/// File to apply accent to. Reads from stdin if unset | ||
#[arg(short, long)] | ||
file: Option<PathBuf>, | ||
} | ||
|
||
fn apply_accent(accent: &Accent, severity: u64, line: io::Result<String>) -> Result<(), String> { | ||
println!( | ||
"{}", | ||
accent.apply( | ||
&line.map_err(|err| format!("reading line: {err}"))?, | ||
severity | ||
) | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() -> Result<(), String> { | ||
let args = Args::parse(); | ||
|
||
let accent_string = if let Some(accent) = args.accent { | ||
fs::read_to_string(accent).map_err(|err| format!("reading accent file: {err}"))? | ||
} else { | ||
// TODO: figure out how to make clap group do the check | ||
args.accent_string | ||
.ok_or_else(|| "expected either --accent or --accent-string")? | ||
}; | ||
|
||
let accent = | ||
ron::from_str::<Accent>(&accent_string).map_err(|err| format!("parsing accent: {err}"))?; | ||
|
||
if let Some(filename) = args.file { | ||
let file = File::open(filename).map_err(|err| format!("reading input file: {err}"))?; | ||
for line in io::BufReader::new(file).lines() { | ||
apply_accent(&accent, args.severity, line)?; | ||
} | ||
} else { | ||
for line in io::stdin().lines() { | ||
apply_accent(&accent, args.severity, line)?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |