Skip to content

Commit

Permalink
add cli binary and flamegraph generation script
Browse files Browse the repository at this point in the history
  • Loading branch information
Fogapod committed Nov 9, 2023
1 parent e809bb5 commit db75e6c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
flamegraph.svg
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ homepage = "https://github.com/Fogapod/pink_accents"
license = "AGPL-3.0"
keywords = ["text"]
categories = ["text-processing"]
exclude = [
"/.github",
"/examples",
"/tests/sample_text.txt",
]
include = ["/README.md", "/Cargo.toml", "/src/*.rs", "LICENSE"]

[dependencies]
rand = "0.8"
regex = "1.10"
log = "0.4"
serde = { version = "1", features = ["derive"], optional = true }
ron = { version = "0.8", optional = true }
clap = { version = "4.4.7", optional = true, features = ["derive"] }

[dev-dependencies]
ron = { version = "0.8" }
Expand All @@ -28,10 +26,15 @@ criterion = { version = "0.4", features = ["html_reports"] }
[features]
default = ["deserialize"]
deserialize = ["dep:serde"]
cli = ["dep:clap", "deserialize", "dep:ron"]

[profile.dev]
opt-level = 3

[[bin]]
name = "pink_accents"
required-features = ["cli"]

[[bench]]
name = "accents"
harness = false
18 changes: 18 additions & 0 deletions scripts/flamegraph.sh
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
69 changes: 69 additions & 0 deletions src/main.rs
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(())
}

0 comments on commit db75e6c

Please sign in to comment.