Skip to content

Commit

Permalink
Merge pull request #255 from Urgau/frames-stroke
Browse files Browse the repository at this point in the history
Add frame stroke option
  • Loading branch information
jonhoo authored Jul 26, 2022
2 parents c9c07d2 + 29a9d8a commit 2f74fe5
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/bin/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;
use std::path::{Path, PathBuf};

use env_logger::Env;
use inferno::flamegraph::color::{BackgroundColor, PaletteMap, SearchColor};
use inferno::flamegraph::color::{BackgroundColor, PaletteMap, SearchColor, StrokeColor};
use inferno::flamegraph::{self, defaults, Direction, Options, Palette, TextTruncateDirection};

#[cfg(feature = "nameattr")]
Expand Down Expand Up @@ -174,6 +174,14 @@ struct Opt {
)]
search_color: SearchColor,

/// Adds an outline to every frame
#[clap(
long = "stroke-color",
default_value = defaults::STROKE_COLOR,
value_name = "STRING"
)]
stroke_color: StrokeColor,

/// Second level title (optional)
#[clap(long = "subtitle", value_name = "STRING")]
subtitle: Option<String>,
Expand Down Expand Up @@ -255,6 +263,7 @@ impl<'a> Opt {
options.negate_differentials = self.negate;
options.factor = self.factor;
options.search_color = self.search_color;
options.stroke_color = self.stroke_color;
(self.infiles, options)
}

Expand Down
22 changes: 22 additions & 0 deletions src/flamegraph/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ impl fmt::Display for SearchColor {
}
}

/// `StrokeColor::default()` is `None`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StrokeColor {
/// Color of the stroke
Color(Color),
/// No color for the stroke
None,
}

impl FromStr for StrokeColor {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "none" {
return Ok(StrokeColor::None);
}
parse_flat_bgcolor(s)
.map(|c| StrokeColor::Color(c))
.ok_or_else(|| format!("unknown color: {}", s))
}
}

impl FromStr for Palette {
type Err = String;

Expand Down
14 changes: 13 additions & 1 deletion src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use self::attrs::FrameAttrs;
pub use self::attrs::FuncFrameAttrsMap;

pub use self::color::Palette;
use self::color::{Color, SearchColor};
use self::color::{Color, SearchColor, StrokeColor};
use self::svg::{Dimension, StyleOptions};

const XPAD: usize = 10; // pad left and right
Expand Down Expand Up @@ -76,6 +76,7 @@ pub mod defaults {
define! {
COLORS: &str = "hot",
SEARCH_COLOR: &str = "#e600e6",
STROKE_COLOR: &str = "none",
TITLE: &str = "Flame Graph",
CHART_TITLE: &str = "Flame Chart",
FRAME_HEIGHT: usize = 16,
Expand Down Expand Up @@ -137,6 +138,11 @@ pub struct Options<'a> {
/// [Default value](defaults::SEARCH_COLOR).
pub search_color: SearchColor,

/// The stroke color for flame graph.
///
/// [Default value](defaults::STROKE_COLOR).
pub stroke_color: StrokeColor,

/// The title for the flame graph.
///
/// [Default value](defaults::TITLE).
Expand Down Expand Up @@ -285,6 +291,7 @@ impl<'a> Default for Options<'a> {
Options {
colors: Palette::from_str(defaults::COLORS).unwrap(),
search_color: SearchColor::from_str(defaults::SEARCH_COLOR).unwrap(),
stroke_color: StrokeColor::from_str(defaults::STROKE_COLOR).unwrap(),
title: defaults::TITLE.to_string(),
frame_height: defaults::FRAME_HEIGHT,
min_width: defaults::MIN_WIDTH,
Expand Down Expand Up @@ -500,10 +507,15 @@ where
svg::write_header(&mut svg, imageheight, opt)?;

let (bgcolor1, bgcolor2) = color::bgcolor_for(opt.bgcolors, opt.colors);
let strokecolor = match opt.stroke_color {
StrokeColor::Color(c) => Some(c.to_string()),
StrokeColor::None => None,
};
let style_options = StyleOptions {
imageheight,
bgcolor1,
bgcolor2,
strokecolor,
};

svg::write_prelude(&mut svg, &style_options, opt)?;
Expand Down
17 changes: 12 additions & 5 deletions src/flamegraph/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(super) struct StyleOptions<'a> {
pub(super) imageheight: usize,
pub(super) bgcolor1: Cow<'a, str>,
pub(super) bgcolor2: Cow<'a, str>,
pub(super) strokecolor: Option<String>,
}

pub fn write_header<W>(
Expand Down Expand Up @@ -130,11 +131,17 @@ where
"
text {{ font-family:{}; font-size:{}px; fill:rgb(0,0,0); }}
#title {{ text-anchor:middle; font-size:{}px; }}
{}",
font_type,
&opt.font_size,
titlesize,
include_str!("flamegraph.css")
",
font_type, &opt.font_size, titlesize,
))))?;
if let Some(strokecolor) = &style_options.strokecolor {
svg.write_event(Event::Text(BytesText::from_escaped_str(&format!(
"#frames > g > rect {{ stroke:{}; stroke-width:1; }}\n",
strokecolor
))))?;
}
svg.write_event(Event::Text(BytesText::from_escaped_str(include_str!(
"flamegraph.css"
))))?;
svg.write_event(Event::End(BytesEnd::borrowed(b"style")))?;

Expand Down
140 changes: 140 additions & 0 deletions tests/data/flamegraph/options/stroke_color.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,18 @@ fn search_color_non_default() {
test_flamegraph(input_file, expected_result_file, options).unwrap();
}

#[test]
fn stroke_color_non_default() {
let input_file =
"./tests/data/flamegraph/differential/perf-cycles-instructions-01-collapsed-all-diff.txt";
let expected_result_file = "./tests/data/flamegraph/options/stroke_color.svg";

let mut options = flamegraph::Options::default();
options.stroke_color = "#7d7d7d".parse().unwrap();

test_flamegraph(input_file, expected_result_file, options).unwrap();
}

#[test]
fn flamegraph_sorted_input_file() {
let input_file = "./flamegraph/test/results/perf-vertx-stacks-01-collapsed-all.txt";
Expand Down

0 comments on commit 2f74fe5

Please sign in to comment.