forked from tauri-apps/tauri
/
run_benchmark.rs
301 lines (258 loc) · 8.04 KB
/
run_benchmark.rs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use anyhow::Result;
use std::{
collections::{HashMap, HashSet},
env,
path::Path,
process::{Command, Stdio},
};
mod utils;
/// The list of the examples of the benchmark name and binary relative path
fn get_all_benchmarks() -> Vec<(String, String)> {
vec![
(
"tauri_hello_world".into(),
format!(
"tests/target/{}/release/bench_helloworld",
utils::get_target()
),
),
(
"tauri_cpu_intensive".into(),
format!(
"tests/target/{}/release/bench_cpu_intensive",
utils::get_target()
),
),
(
"tauri_3mb_transfer".into(),
format!(
"tests/target/{}/release/bench_files_transfer",
utils::get_target()
),
),
]
}
fn run_strace_benchmarks(new_data: &mut utils::BenchResult) -> Result<()> {
use std::io::Read;
let mut thread_count = HashMap::<String, u64>::new();
let mut syscall_count = HashMap::<String, u64>::new();
for (name, example_exe) in get_all_benchmarks() {
let mut file = tempfile::NamedTempFile::new()?;
Command::new("strace")
.args([
"-c",
"-f",
"-o",
file.path().to_str().unwrap(),
utils::bench_root_path().join(example_exe).to_str().unwrap(),
])
.stdout(Stdio::inherit())
.spawn()?
.wait()?;
let mut output = String::new();
file.as_file_mut().read_to_string(&mut output)?;
let strace_result = utils::parse_strace_output(&output);
// Note, we always have 1 thread. Use cloneX calls as counter for additional threads created.
let clone = 1
+ strace_result.get("clone").map(|d| d.calls).unwrap_or(0)
+ strace_result.get("clone3").map(|d| d.calls).unwrap_or(0);
let total = strace_result.get("total").unwrap().calls;
thread_count.insert(name.to_string(), clone);
syscall_count.insert(name.to_string(), total);
}
new_data.thread_count = thread_count;
new_data.syscall_count = syscall_count;
Ok(())
}
fn run_max_mem_benchmark() -> Result<HashMap<String, u64>> {
let mut results = HashMap::<String, u64>::new();
for (name, example_exe) in get_all_benchmarks() {
let benchmark_file = utils::target_dir().join(format!("mprof{}_.dat", name));
let benchmark_file = benchmark_file.to_str().unwrap();
let proc = Command::new("mprof")
.args([
"run",
"-C",
"-o",
benchmark_file,
utils::bench_root_path().join(example_exe).to_str().unwrap(),
])
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()?;
let proc_result = proc.wait_with_output()?;
println!("{:?}", proc_result);
results.insert(
name.to_string(),
utils::parse_max_mem(benchmark_file).unwrap(),
);
}
Ok(results)
}
fn rlib_size(target_dir: &std::path::Path, prefix: &str) -> u64 {
let mut size = 0;
let mut seen = std::collections::HashSet::new();
for entry in std::fs::read_dir(target_dir.join("deps")).unwrap() {
let entry = entry.unwrap();
let os_str = entry.file_name();
let name = os_str.to_str().unwrap();
if name.starts_with(prefix) && name.ends_with(".rlib") {
let start = name.split('-').next().unwrap().to_string();
if seen.contains(&start) {
println!("skip {}", name);
} else {
seen.insert(start);
size += entry.metadata().unwrap().len();
println!("check size {} {}", name, size);
}
}
}
assert!(size > 0);
size
}
fn get_binary_sizes(target_dir: &Path) -> Result<HashMap<String, u64>> {
let mut sizes = HashMap::<String, u64>::new();
let wry_size = rlib_size(target_dir, "libwry");
println!("wry {} bytes", wry_size);
sizes.insert("wry_rlib".to_string(), wry_size);
// add size for all EXEC_TIME_BENCHMARKS
for (name, example_exe) in get_all_benchmarks() {
let meta = std::fs::metadata(example_exe).unwrap();
sizes.insert(name.to_string(), meta.len());
}
Ok(sizes)
}
/// (target OS, target triple)
const TARGETS: &[(&str, &[&str])] = &[
(
"Windows",
&[
"x86_64-pc-windows-gnu",
"i686-pc-windows-gnu",
"i686-pc-windows-msvc",
"x86_64-pc-windows-msvc",
],
),
(
"Linux",
&[
"x86_64-unknown-linux-gnu",
"i686-unknown-linux-gnu",
"aarch64-unknown-linux-gnu",
],
),
("macOS", &["x86_64-apple-darwin", "aarch64-apple-darwin"]),
];
fn cargo_deps() -> HashMap<String, usize> {
let mut results = HashMap::new();
for (os, targets) in TARGETS {
for target in *targets {
let mut cmd = Command::new("cargo");
cmd.arg("tree");
cmd.arg("--no-dedupe");
cmd.args(["--edges", "normal"]);
cmd.args(["--prefix", "none"]);
cmd.args(["--target", target]);
cmd.current_dir(&utils::tauri_root_path());
let full_deps = cmd.output().expect("failed to run cargo tree").stdout;
let full_deps = String::from_utf8(full_deps).expect("cargo tree output not utf-8");
let count = full_deps.lines().collect::<HashSet<_>>().len() - 1; // output includes wry itself
// set the count to the highest count seen for this OS
let existing = results.entry(os.to_string()).or_default();
*existing = count.max(*existing);
assert!(count > 10); // sanity check
}
}
results
}
const RESULT_KEYS: &[&str] = &["mean", "stddev", "user", "system", "min", "max"];
fn run_exec_time(target_dir: &Path) -> Result<HashMap<String, HashMap<String, f64>>> {
let benchmark_file = target_dir.join("hyperfine_results.json");
let benchmark_file = benchmark_file.to_str().unwrap();
let mut command = [
"hyperfine",
"--export-json",
benchmark_file,
"--show-output",
"--warmup",
"3",
]
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>();
for (_, example_exe) in get_all_benchmarks() {
command.push(
utils::bench_root_path()
.join(example_exe)
.to_str()
.unwrap()
.to_string(),
);
}
utils::run(&command.iter().map(|s| s.as_ref()).collect::<Vec<_>>());
let mut results = HashMap::<String, HashMap<String, f64>>::new();
let hyperfine_results = utils::read_json(benchmark_file)?;
for ((name, _), data) in get_all_benchmarks().iter().zip(
hyperfine_results
.as_object()
.unwrap()
.get("results")
.unwrap()
.as_array()
.unwrap(),
) {
let data = data.as_object().unwrap().clone();
results.insert(
name.to_string(),
data
.into_iter()
.filter(|(key, _)| RESULT_KEYS.contains(&key.as_str()))
.map(|(key, val)| (key, val.as_f64().unwrap()))
.collect(),
);
}
Ok(results)
}
fn main() -> Result<()> {
// download big files if not present
let json_3mb = utils::home_path().join(".tauri_3mb.json");
if !json_3mb.exists() {
utils::download_file(
"https://github.com/lemarier/tauri-test/releases/download/v2.0.0/json_3mb.json",
json_3mb,
);
}
println!("Starting tauri benchmark");
let target_dir = utils::target_dir();
env::set_current_dir(&utils::bench_root_path())?;
let format =
time::format_description::parse("[year]-[month]-[day]T[hour]:[minute]:[second]Z").unwrap();
let now = time::OffsetDateTime::now_utc();
let mut new_data = utils::BenchResult {
created_at: now.format(&format).unwrap(),
sha1: utils::run_collect(&["git", "rev-parse", "HEAD"])
.0
.trim()
.to_string(),
exec_time: run_exec_time(&target_dir)?,
binary_size: get_binary_sizes(&target_dir)?,
cargo_deps: cargo_deps(),
..Default::default()
};
if cfg!(target_os = "linux") {
run_strace_benchmarks(&mut new_data)?;
new_data.max_memory = run_max_mem_benchmark()?;
}
println!("===== <BENCHMARK RESULTS>");
serde_json::to_writer_pretty(std::io::stdout(), &new_data)?;
println!("\n===== </BENCHMARK RESULTS>");
if let Some(filename) = target_dir.join("bench.json").to_str() {
utils::write_json(filename, &serde_json::to_value(&new_data)?)?;
} else {
eprintln!("Cannot write bench.json, path is invalid");
}
Ok(())
}