Skip to content

Commit

Permalink
feat(bench): add tauri_3mb_transfer test (tauri-apps#1895)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemarier authored Jun 23, 2021
1 parent 47f7558 commit f729b5e
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 44 deletions.
4 changes: 3 additions & 1 deletion tooling/bench/src/build_benchmark_jsons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ fn main() {

// write json's
utils::write_json(
tauri_data.to_str().expect("Something wrong with tauri_data"),
tauri_data
.to_str()
.expect("Something wrong with tauri_data"),
&serde_json::to_value(&all_data).expect("Unable to build final json (alls)"),
)
.expect(format!("Unable to write {:?}", tauri_data).as_str());
Expand Down
15 changes: 11 additions & 4 deletions tooling/bench/src/run_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ fn get_all_benchmarks() -> Vec<(String, String)> {
utils::get_target()
),
),
/*
FIXME: require next release of tao
(
"tauri_3mb_transfer".into(),
format!(
"tests/target/{}/release/tauri_3mb_transfer",
"tests/target/{}/release/bench_files_transfer",
utils::get_target()
),
),
*/
]
}

Expand Down Expand Up @@ -250,6 +247,16 @@ fn run_exec_time(target_dir: &Path) -> Result<HashMap<String, HashMap<String, f6
}

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();
Expand Down
39 changes: 38 additions & 1 deletion tooling/bench/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{collections::HashMap, fs, io::{BufRead, BufReader}, path::PathBuf, process::{Command, Output, Stdio}};
use std::{
collections::HashMap,
fs,
io::{BufRead, BufReader},
path::PathBuf,
process::{Command, Output, Stdio},
};

#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct BenchResult {
Expand Down Expand Up @@ -51,6 +57,14 @@ pub fn bench_root_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

#[allow(dead_code)]
pub fn home_path() -> PathBuf {
#[cfg(any(target_os = "macos", target_os = "linux"))]
return PathBuf::from(env!("HOME"));
#[cfg(any(target_os = "windows"))]
return PathBuf::from(env!("HOMEPATH"));
}

#[allow(dead_code)]
pub fn tauri_root_path() -> PathBuf {
bench_root_path()
Expand Down Expand Up @@ -192,3 +206,26 @@ pub fn write_json(filename: &str, value: &Value) -> Result<()> {
serde_json::to_writer(f, value)?;
Ok(())
}

#[allow(dead_code)]
pub fn download_file(url: &str, filename: PathBuf) {
if !url.starts_with("http:") && !url.starts_with("https:") {
fs::copy(url, filename).unwrap();
return;
}

// Downloading with curl this saves us from adding
// a Rust HTTP client dependency.
println!("Downloading {}", url);
let status = Command::new("curl")
.arg("-L")
.arg("-s")
.arg("-o")
.arg(&filename)
.arg(&url)
.status()
.unwrap();

assert!(status.success());
assert!(filename.exists());
}
139 changes: 128 additions & 11 deletions tooling/bench/tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 28 additions & 25 deletions tooling/bench/tests/files_transfer/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,37 @@ <h1>Welcome to Tauri!</h1>

<script>
window.addEventListener("DOMContentLoaded", (event) => {
window.__TAURI__.invoke("tauri", {
__tauriModule: "Fs",
message: {
cmd: "readBinaryFile",
path: ".tauri_3mb.json",
options: {
// home folder
dir: 11
}
},
}).then((_data) => {
window.__TAURI__.invoke("tauri", {
__tauriModule: "Process",
window.__TAURI__
.invoke("tauri", {
__tauriModule: "Fs",
message: {
cmd: "exit",
exitCode: 0,
cmd: "readBinaryFile",
path: ".tauri_3mb.json",
options: {
// home folder
dir: 11,
},
},
})
.then((_data) => {
window.__TAURI__.invoke("tauri", {
__tauriModule: "Process",
message: {
cmd: "exit",
exitCode: 0,
},
});
})
.catch((_error) => {
// error
window.__TAURI__.invoke("tauri", {
__tauriModule: "Process",
message: {
cmd: "exit",
exitCode: 1,
},
});
});
}).catch((_error) => {
// error
window.__TAURI__.invoke("tauri", {
__tauriModule: "Process",
message: {
cmd: "exit",
exitCode: 1,
},
});
});
});
</script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion tooling/bench/tests/files_transfer/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tauri-build = { path = "../../../../../core/tauri-build", features = [ "codegen"
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
tauri = { path = "../../../../../core/tauri", features = [] }
tauri = { path = "../../../../../core/tauri", features = ["fs-read-binary-file"] }

[features]
default = [ "custom-protocol" ]
Expand Down
5 changes: 4 additions & 1 deletion tooling/bench/tests/files_transfer/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
}
},
"allowlist": {
"all": false
"all": false,
"fs": {
"readBinaryFile": true
}
},
"windows": [
{
Expand Down

0 comments on commit f729b5e

Please sign in to comment.