Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first possible annotation syntax #24

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions annotate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import sys
import subprocess as sp
import json
from dataclasses import dataclass, field

""" Piped commands not working currently
"""

@dataclass
class Annotation:
json: str
data: dict = field(init=False)

def __post_init__(self):
with open(self.json, 'r') as f:
self.data = json.load(f)

def execute_cmd(self, key, cmd):
try:
result = sp.check_output(cmd, shell=True).decode('ascii')
except sp.CalledProcessError as err:
print(f"Failed w/ {err}")
raise
else:
tmp = self.data["results"][0]
if hasattr(tmp,"annotations"):
tmp["annotations"][key] = result
else:
tmp["annotations"] = {key:result}
self.data["results"][0] = tmp

def export(self):
with open('new_' + self.json, 'w') as f:
json.dump(self.data, f, indent=2)


def main(keyword, hyperfine_json, cmd):
annon = Annotation(hyperfine_json)
annon.execute_cmd(keyword, cmd)
annon.export()

if __name__ == '__main__':
keyword = sys.argv[1]
js = sys.argv[2]
command = sys.argv[3:]
main(keyword = keyword, hyperfine_json = js, cmd = command)
4 changes: 4 additions & 0 deletions examples/duplicate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ setup = "which ls"
shell = "none"
command = "dd if={ifile} of=/tmp/Cargo.toml.dd"

[run.dd.annotations]
filesize = "ls -al /tmp/Cargo.toml.dd | awk '{{print $5}}'"
modified = "ls -al /tmp/Cargo.toml.dd | awk '{{print $6\" \"$7\" \"$8}}'"

[run.cp]
command = "cp {ifile} /tmp/Cargo.toml.cp"

Expand Down
25 changes: 25 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ use std::{collections::HashMap, fmt::Display, fs::File, io::Read, path::PathBuf}
/// Transformation to hyperfine parameters
pub trait Hyperfined {
fn to_hyperfine(&self) -> Vec<String>;
fn to_hyperfine_with_json(&self, json: &str) -> Vec<String> {
let mut params = self.to_hyperfine();
params.push("--export-json".to_string());
params.push(json.to_string());
params
}
fn to_hyperfine_with_json_and_annotations(&self, json: &str, kw: &HashMap<String, String>) -> Vec<String> {
let mut params = self.to_hyperfine();
let mut annotations: Vec<_> = kw.iter().map(|(k, v)| util::generate_jq_cmd(&k, &v, json)).collect();
match &params.iter().position(|x| x == &"--cleanup".to_string()) {
Some(ix) => {
// Cleanup used in command and annotations must be prepand
annotations.push(params.get(ix+1).unwrap().clone());
params.insert(ix + 1, annotations.join(" && "));
params.remove(ix + 2);
},
None => {
// Cleanup not used, annotations can be added as cleanup
params.push("--cleanup".to_string());
params.push(annotations.join(" && "));
}
}
params
}
}

/// Configuration for a complete Benchmark set consisting of several Runs
Expand Down Expand Up @@ -64,6 +88,7 @@ pub(crate) struct Run {
prepare: Option<String>,
setup: Option<String>,
shell: Option<String>,
pub annotations: Option<HashMap<String, String>>,
command: String,
}

Expand Down
31 changes: 18 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ mod util;
/// Command-line Interface (CLI) for the hypcmp library
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Cli {
struct Cli {
/// Configuration file [*.toml]
#[clap(value_parser)]
pub config: PathBuf,
config: PathBuf,

#[clap(flatten)]
pub verbose: clap_verbosity_flag::Verbosity,
verbose: clap_verbosity_flag::Verbosity,
}

fn main() -> std::io::Result<()> {
Expand All @@ -42,23 +42,28 @@ fn main() -> std::io::Result<()> {
for (label, run) in c.run.iter() {
debug!("Run: {run:?}");

// Initiate `hyperfine` command
let mut cmd = Command::new("hyperfine");
cmd.args(c.to_hyperfine());

let mut name = vec!["--command-name".to_string()];
name.push(label.clone());
cmd.args(name);

let mut json = vec!["--export-json".to_string()];
// Add json output to arguments
let mut filename = label.clone();
filename.push_str(".json");
let output = dir.path().join(filename).display().to_string();
json.push(output.clone());
cmd.args(json);
cmd.args(c.to_hyperfine_with_json(&output));

// Add command name to arguments
let mut name = vec!["--command-name".to_string()];
name.push(label.clone());
cmd.args(name);

cmd.args(run.to_hyperfine());
// Add run specific arguments
match &run.annotations {
Some(hm) => cmd.args(run.to_hyperfine_with_json_and_annotations(&output, hm)),
None => cmd.args(run.to_hyperfine()),
};
info!("Running: {cmd:?}");

// Execute command
let result = cmd.output()?;
if result.status.success() {
debug!("Benchmark run successful");
Expand All @@ -80,7 +85,7 @@ fn main() -> std::io::Result<()> {
} else {
let json = util::merge_json_files(&files_to_be_merged)?;
util::write_json_to_disk(json)?;
util::cleanup(files_to_be_merged, dir)?;
// util::cleanup(files_to_be_merged, dir)?;
util::checkout(current_branch)?;
}
Ok(())
Expand Down
23 changes: 23 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,26 @@ fn move_commit_label_to_cmd_name(mut json: Value) -> std::io::Result<serde_json:
}
Ok(json)
}

/// Generate jq command to manipulate `hyperfine` result json
pub fn generate_jq_cmd(key: &str, cmd: &str, json: &str) -> String {
format!(
"jq --arg {0} $({1}) '.results[0].{0}=${0}' {2} > {2}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This override of the file is not working with jq .. the output will be empty

key, cmd, json
)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn jq() {
let key = "filesize";
let cmd = "ls -al /tmp/Cargo.toml.dd | awk '{{print $5}}'";
let json = "out.json";
let expected = "jq --arg filesize $(ls -al /tmp/Cargo.toml.dd | awk '{{print $5}}') '.results[0].filesize=$filesize' out.json";

assert_eq!(expected, generate_jq_cmd(key, cmd, json))
}
}