-
Notifications
You must be signed in to change notification settings - Fork 289
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
CLI Options #446
Merged
brettkolodny
merged 4 commits into
open-web3-stack:orml_bencher
from
brettkolodny:orml_bencher
Apr 15, 2021
Merged
CLI Options #446
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -7,10 +7,15 @@ version = "0.1.0" | |
authors = ["Laminar Developers <[email protected]>"] | ||
edition = "2018" | ||
|
||
[[bin]] | ||
path = "src/bin.rs" | ||
name = "codegen" | ||
|
||
[dependencies] | ||
linregress = { version = "0.4.0", optional = true } | ||
handlebars = {version = "3.5.2", optional = true } | ||
serde = { features = ['derive'], optional = true, version = "1.0.119" } | ||
serde_json = "1.0" | ||
codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"], default-features = false } | ||
sp-core = { version = "3.0.0", default-features = false } | ||
sp-std = { version = "3.0.0", default-features = false } | ||
|
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,20 @@ | ||
[package] | ||
name = "weight-gen" | ||
description = "CLI for generating weight from bencher output" | ||
license = "Apache-2.0" | ||
version = "0.1.0" | ||
authors = ["Laminar Developers <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
serde = { features = ['derive'], optional = true, version = "1.0.119" } | ||
serde_json = "1.0" | ||
clap = "3.0.0-beta.2" | ||
handlebars = {version = "3.5.2", optional = true } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"handlebars", | ||
"serde/std", | ||
] |
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,185 @@ | ||
use serde::{Serialize, Deserialize}; | ||
use clap::{AppSettings, Clap}; | ||
use std::io::Read; | ||
|
||
#[derive(Clap)] | ||
#[clap(version = "01.0", author = "Laminar Developers <[email protected]>")] | ||
#[clap(setting = AppSettings::ColoredHelp)] | ||
struct Opts { | ||
input: Option<String>, | ||
#[clap(short, long)] | ||
template: Option<String>, | ||
#[clap(short, long)] | ||
header: Option<String>, | ||
#[clap(short, long)] | ||
out: Option<String>, | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
#[derive(Serialize, Deserialize, Default, Debug, Clone)] | ||
pub struct BenchData { | ||
pub name: String, | ||
pub base_weight: u64, | ||
pub base_reads: u32, | ||
pub base_writes: u32, | ||
} | ||
|
||
#[derive(Serialize, Default, Debug, Clone)] | ||
struct TemplateData { | ||
pub header: String, | ||
pub benchmarks: Vec<BenchData>, | ||
} | ||
|
||
// A Handlebars helper to add an underscore after every 3rd character, | ||
// i.e. a separator for large numbers. | ||
#[derive(Clone, Copy)] | ||
struct UnderscoreHelper; | ||
impl handlebars::HelperDef for UnderscoreHelper { | ||
fn call<'reg: 'rc, 'rc>( | ||
&self, | ||
h: &handlebars::Helper, | ||
_: &handlebars::Handlebars, | ||
_: &handlebars::Context, | ||
_rc: &mut handlebars::RenderContext, | ||
out: &mut dyn handlebars::Output, | ||
) -> handlebars::HelperResult { | ||
use handlebars::JsonRender; | ||
let param = h.param(0).unwrap(); | ||
let underscore_param = underscore(param.value().render()); | ||
out.write(&underscore_param)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
// Add an underscore after every 3rd character, i.e. a separator for large | ||
// numbers. | ||
fn underscore<Number>(i: Number) -> String | ||
where | ||
Number: std::string::ToString, | ||
{ | ||
let mut s = String::new(); | ||
let i_str = i.to_string(); | ||
let a = i_str.chars().rev().enumerate(); | ||
for (idx, val) in a { | ||
if idx != 0 && idx % 3 == 0 { | ||
s.insert(0, '_'); | ||
} | ||
s.insert(0, val); | ||
} | ||
s | ||
} | ||
|
||
// A helper to join a string of vectors. | ||
#[derive(Clone, Copy)] | ||
struct JoinHelper; | ||
impl handlebars::HelperDef for JoinHelper { | ||
fn call<'reg: 'rc, 'rc>( | ||
&self, | ||
h: &handlebars::Helper, | ||
_: &handlebars::Handlebars, | ||
_: &handlebars::Context, | ||
_rc: &mut handlebars::RenderContext, | ||
out: &mut dyn handlebars::Output, | ||
) -> handlebars::HelperResult { | ||
use handlebars::JsonRender; | ||
let param = h.param(0).unwrap(); | ||
let value = param.value(); | ||
let joined = if value.is_array() { | ||
value | ||
.as_array() | ||
.unwrap() | ||
.iter() | ||
.map(|v| v.render()) | ||
.collect::<Vec<String>>() | ||
.join(" ") | ||
} else { | ||
value.render() | ||
}; | ||
out.write(&joined)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn parse_stdio() -> Option<Vec<BenchData>> { | ||
let mut buffer = String::new(); | ||
let stdin = std::io::stdin(); | ||
let mut handle = stdin.lock(); | ||
|
||
handle.read_to_string(&mut buffer).unwrap(); | ||
|
||
let lines: Vec<&str> = buffer.split("\n").collect(); | ||
for line in lines { | ||
let json = serde_json::from_str(line); | ||
|
||
if let Ok(data) = json { | ||
return Some(data); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn main() { | ||
let opts: Opts = Opts::parse(); | ||
|
||
let benchmarks: Vec<BenchData> = { | ||
if let Some(data) = opts.input { | ||
serde_json::from_str(&data).expect("Could not parse JSON data") | ||
} else { | ||
parse_stdio().expect("Could not parse JSON data") | ||
} | ||
}; | ||
|
||
let mut handlebars = handlebars::Handlebars::new(); | ||
handlebars.register_helper("underscore", Box::new(UnderscoreHelper)); | ||
handlebars.register_helper("join", Box::new(JoinHelper)); | ||
// Don't HTML escape any characters. | ||
handlebars.register_escape_fn(|s| -> String { s.to_string() }); | ||
|
||
// Use empty header if a header path is not given. | ||
let header = { | ||
if let Some(path) = opts.header { | ||
let header_string = ::std::fs::read_to_string(&path) | ||
.expect(&format!("Header file not found at {}", &path)); | ||
|
||
header_string | ||
} else { | ||
String::from("") | ||
} | ||
}; | ||
|
||
let hbs_data = TemplateData { | ||
header, | ||
benchmarks, | ||
}; | ||
|
||
const DEFAULT_TEMPLATE: &str = include_str!("./template.hbs"); | ||
|
||
// Use default template if template path is not given. | ||
let template = { | ||
if let Some(path) = opts.template { | ||
let template_string = ::std::fs::read_to_string(&path) | ||
.expect(&format!("Template file not found at {}", &path)); | ||
|
||
template_string | ||
} else { | ||
String::from(DEFAULT_TEMPLATE) | ||
} | ||
}; | ||
|
||
// Write benchmark to file or print to terminal if output path is not given. | ||
if let Some(path) = opts.out { | ||
let mut output_file = ::std::fs::File::create(&path) | ||
.expect(&format!("Could not create output file at {}", &path)); | ||
|
||
handlebars | ||
.render_template_to_write(&template, &hbs_data, &mut output_file) | ||
.unwrap(); | ||
} else { | ||
let template_string = handlebars | ||
.render_template(&template, &hbs_data) | ||
.unwrap(); | ||
|
||
println!("{}", template_string); | ||
} | ||
} |
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,23 @@ | ||
{{header}} | ||
|
||
#![allow(unused_parens)] | ||
#![allow(unused_imports)] | ||
#![allow(dead_code)] | ||
|
||
use frame_support::{traits::Get, weights::Weight}; | ||
use sp_std::marker::PhantomData; | ||
|
||
pub struct ModuleWeights<T>(PhantomData<T>); | ||
impl<T: frame_system::Config> ModuleWeights<T> { | ||
{{~#each benchmarks as |benchmark|}} | ||
pub fn {{benchmark.name~}} () -> Weight { | ||
({{underscore benchmark.base_weight}} as Weight) | ||
{{~#if (ne benchmark.base_reads "0")}} | ||
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight)) | ||
{{~/if}} | ||
{{~#if (ne benchmark.base_writes "0")}} | ||
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight)) | ||
{{~/if}} | ||
} | ||
{{~/each}} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use expect instead of unwrap