1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use sc_cli::Result;
use sc_service::Configuration;
use handlebars::Handlebars;
use log::info;
use serde::Serialize;
use std::{env, fs, path::PathBuf};
use crate::{
overhead::cmd::{BenchmarkType, OverheadParams},
shared::{Stats, UnderscoreHelper},
};
static VERSION: &str = env!("CARGO_PKG_VERSION");
static TEMPLATE: &str = include_str!("./weights.hbs");
#[derive(Serialize, Debug, Clone)]
pub(crate) struct TemplateData {
long_name: String,
short_name: String,
runtime_name: String,
version: String,
date: String,
hostname: String,
cpuname: String,
args: Vec<String>,
params: OverheadParams,
stats: Stats,
weight: u64,
}
impl TemplateData {
pub(crate) fn new(
t: BenchmarkType,
cfg: &Configuration,
params: &OverheadParams,
stats: &Stats,
) -> Result<Self> {
let weight = params.weight.calc_weight(stats)?;
Ok(TemplateData {
short_name: t.short_name().into(),
long_name: t.long_name().into(),
runtime_name: cfg.chain_spec.name().into(),
version: VERSION.into(),
date: chrono::Utc::now().format("%Y-%m-%d (Y/M/D)").to_string(),
hostname: params.hostinfo.hostname(),
cpuname: params.hostinfo.cpuname(),
args: env::args().collect::<Vec<String>>(),
params: params.clone(),
stats: stats.clone(),
weight,
})
}
pub fn write(&self, path: &Option<PathBuf>) -> Result<()> {
let mut handlebars = Handlebars::new();
handlebars.register_helper("underscore", Box::new(UnderscoreHelper));
handlebars.register_escape_fn(|s| -> String { s.to_string() });
let out_path = self.build_path(path)?;
let mut fd = fs::File::create(&out_path)?;
info!("Writing weights to {:?}", fs::canonicalize(&out_path)?);
handlebars
.render_template_to_write(TEMPLATE, &self, &mut fd)
.map_err(|e| format!("HBS template write: {:?}", e).into())
}
fn build_path(&self, weight_out: &Option<PathBuf>) -> Result<PathBuf> {
let mut path = weight_out.clone().unwrap_or_else(|| PathBuf::from("."));
if !path.is_dir() {
return Err("Need directory as --weight-path".into())
}
path.push(format!("{}_weights.rs", self.short_name));
Ok(path)
}
}