Skip to content

Commit

Permalink
Avoid file clobber, adjust margin, cargo fmt (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezyang authored May 19, 2024
1 parent 81ed802 commit 7cf4fb9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
27 changes: 23 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{anyhow, bail};
use fxhash::FxHashMap;
use md5::{Digest, Md5};
use std::ffi::{OsStr, OsString};

use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use regex::Regex;
Expand Down Expand Up @@ -105,7 +106,8 @@ pub fn parse_path(path: &PathBuf, config: ParseConfig) -> anyhow::Result<ParseOu
// Each entry is a compile id => (link, rendered name, output number)
// For files, link and rendered name are the same
// For links, you can specify a custom name for the link
let mut directory: FxIndexMap<Option<CompileId>, Vec<(String, String, i32)>> = FxIndexMap::default();
let mut directory: FxIndexMap<Option<CompileId>, Vec<(String, String, i32)>> =
FxIndexMap::default();

let mut metrics_index: CompilationMetricsIndex = FxIndexMap::default();
let stack_index: RefCell<StackIndex> = RefCell::new(FxHashMap::default());
Expand Down Expand Up @@ -239,11 +241,28 @@ pub fn parse_path(path: &PathBuf, config: ParseConfig) -> anyhow::Result<ParseOu
match results {
Ok(results) => {
for parser_result in results {
match parser_result {
ParserOutput::File(filename, out) => {
match parser_result {
ParserOutput::File(raw_filename, out) => {
let filename = if let Some(stem) = raw_filename.file_stem() {
let mut r = OsString::new();
r.push(stem);
r.push(OsStr::new("_"));
r.push(output_count.to_string());
if let Some(e) = raw_filename.extension() {
r.push(OsStr::new("."));
r.push(e);
};
raw_filename.with_file_name(r)
} else {
raw_filename
};
output.push((filename.clone(), out));
let filename_str = format!("{}", filename.to_string_lossy());
compile_directory.push((filename_str.clone(), filename_str, output_count));
compile_directory.push((
filename_str.clone(),
filename_str,
output_count,
));
output_count += 1;
}
ParserOutput::Link(name, url) => {
Expand Down
16 changes: 6 additions & 10 deletions src/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub trait StructuredLogParser {
fn name(&self) -> &'static str;
}


// Takes a filename and a payload and writes that payload into a the file
fn simple_file_output(
filename: &str,
Expand Down Expand Up @@ -249,11 +248,7 @@ fn generate_html_output(payload: &str) -> Result<String, anyhow::Error> {
&syntax,
&theme_set.themes["InspiredGitHub"],
);
let wrapped_html = format!(
"<div style=\"padding: 10px; border: 1px solid #ffffff; border-radius: 5px; background-color: #ffffff;\">{}</div>",
html.unwrap()
);
Ok(wrapped_html)
Ok(html?)
}

pub struct OptimizeDdpSplitChildParser;
Expand Down Expand Up @@ -290,9 +285,7 @@ impl StructuredLogParser for LinkParser {
"link_parser"
}
fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
e.link
.as_ref()
.map(|m| Metadata::Link(m))
e.link.as_ref().map(|m| Metadata::Link(m))
}

fn parse<'e>(
Expand All @@ -304,7 +297,10 @@ impl StructuredLogParser for LinkParser {
_payload: &str,
) -> anyhow::Result<ParserResults> {
if let Metadata::Link(m) = metadata {
Ok(Vec::from([ParserOutput::Link(m.name.clone(), m.url.clone())]))
Ok(Vec::from([ParserOutput::Link(
m.name.clone(),
m.url.clone(),
)]))
} else {
Err(anyhow::anyhow!("Expected Link Metadata"))
}
Expand Down
1 change: 0 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::sync::Mutex;


// Main function returns a list of files to save
pub type ParseOutput = Vec<(PathBuf, String)>;
pub type CompilationMetricsIndex = FxIndexMap<Option<CompileId>, Vec<CompilationMetricsMetadata>>;
Expand Down
32 changes: 16 additions & 16 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ fn prefix_exists(map: &HashMap<PathBuf, String>, prefix: &str) -> bool {
#[test]
fn test_parse_simple() {
let expected_files = [
"0_0_0/aot_forward_graph.txt",
"0_0_0/dynamo_output_graph.txt",
"0_0_0/aot_forward_graph",
"0_0_0/dynamo_output_graph",
"index.html",
"failures_and_restarts.html",
"0_0_0/inductor_post_grad_graph.txt",
"0_0_0/inductor_output_code", // This always has an output hash, so we use prefixes instead of full
"0_0_0/dynamo_guards.html",
"0_0_0/inductor_post_grad_graph",
"0_0_0/inductor_output_code",
"0_0_0/dynamo_guards",
];
// Read the test file
// simple.log was generated from the following:
Expand All @@ -43,15 +43,15 @@ fn test_parse_simple() {
#[test]
fn test_parse_compilation_metrics() {
let expected_files = [
"0_0_1/dynamo_output_graph.txt",
"0_0_1/dynamo_guards.html",
"0_0_1/compilation_metrics.html",
"1_0_1/dynamo_output_graph.txt",
"1_0_1/dynamo_guards.html",
"1_0_1/compilation_metrics.html",
"2_0_0/dynamo_output_graph.txt",
"2_0_0/dynamo_guards.html",
"2_0_0/compilation_metrics.html",
"0_0_1/dynamo_output_graph",
"0_0_1/dynamo_guards",
"0_0_1/compilation_metrics",
"1_0_1/dynamo_output_graph",
"1_0_1/dynamo_guards",
"1_0_1/compilation_metrics",
"2_0_0/dynamo_output_graph",
"2_0_0/dynamo_guards",
"2_0_0/compilation_metrics",
"index.html",
"failures_and_restarts.html",
];
Expand Down Expand Up @@ -79,8 +79,8 @@ fn test_parse_compilation_metrics() {
#[test]
fn test_parse_compilation_failures() {
let expected_files = [
"0_0_0/dynamo_output_graph.txt",
"0_0_0/compilation_metrics.html",
"0_0_0/dynamo_output_graph",
"0_0_0/compilation_metrics",
"index.html",
"failures_and_restarts.html",
];
Expand Down

0 comments on commit 7cf4fb9

Please sign in to comment.