2020-07-02 12:00:32 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use anyhow::{Error};
|
|
|
|
use serde_json::Value;
|
2020-07-10 07:13:08 +00:00
|
|
|
use serde::Serialize;
|
2020-07-02 12:00:32 +00:00
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_schema::{api, ApiType, ReturnType};
|
|
|
|
use proxmox_router::{
|
2020-07-10 07:13:08 +00:00
|
|
|
cli::{
|
|
|
|
OUTPUT_FORMAT,
|
|
|
|
ColumnConfig,
|
|
|
|
get_output_format,
|
|
|
|
format_and_print_result_full,
|
|
|
|
default_table_format_options,
|
|
|
|
},
|
2021-10-08 09:19:37 +00:00
|
|
|
ApiMethod,
|
|
|
|
RpcEnvironment,
|
2020-07-10 07:13:08 +00:00
|
|
|
};
|
2020-07-02 12:00:32 +00:00
|
|
|
|
2021-09-07 07:22:14 +00:00
|
|
|
use pbs_tools::crypt_config::CryptConfig;
|
|
|
|
use pbs_config::key_config::{KeyDerivationConfig, load_and_decrypt_key};
|
2021-07-19 08:50:18 +00:00
|
|
|
use pbs_client::tools::key_source::get_encryption_key_password;
|
|
|
|
use pbs_client::{BackupRepository, BackupWriter};
|
2021-09-06 09:48:08 +00:00
|
|
|
use pbs_datastore::data_blob::{DataBlob, DataChunkBuilder};
|
2020-07-02 12:00:32 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
KEYFILE_SCHEMA, REPO_URL_SCHEMA,
|
|
|
|
extract_repository_from_value,
|
|
|
|
record_repository,
|
|
|
|
connect,
|
|
|
|
};
|
|
|
|
|
2020-07-10 07:13:08 +00:00
|
|
|
#[api()]
|
|
|
|
#[derive(Copy, Clone, Serialize)]
|
|
|
|
/// Speed test result
|
|
|
|
struct Speed {
|
|
|
|
/// The meassured speed in Bytes/second
|
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
speed: Option<f64>,
|
|
|
|
/// Top result we want to compare with
|
|
|
|
top: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
properties: {
|
|
|
|
"tls": {
|
|
|
|
type: Speed,
|
|
|
|
},
|
|
|
|
"sha256": {
|
|
|
|
type: Speed,
|
|
|
|
},
|
|
|
|
"compress": {
|
|
|
|
type: Speed,
|
|
|
|
},
|
|
|
|
"decompress": {
|
|
|
|
type: Speed,
|
|
|
|
},
|
|
|
|
"aes256_gcm": {
|
|
|
|
type: Speed,
|
|
|
|
},
|
2020-09-24 06:55:48 +00:00
|
|
|
"verify": {
|
|
|
|
type: Speed,
|
|
|
|
},
|
2020-07-10 07:13:08 +00:00
|
|
|
},
|
|
|
|
)]
|
|
|
|
#[derive(Copy, Clone, Serialize)]
|
|
|
|
/// Benchmark Results
|
|
|
|
struct BenchmarkResult {
|
|
|
|
/// TLS upload speed
|
|
|
|
tls: Speed,
|
2020-08-18 11:46:50 +00:00
|
|
|
/// SHA256 checksum computation speed
|
2020-07-10 07:13:08 +00:00
|
|
|
sha256: Speed,
|
|
|
|
/// ZStd level 1 compression speed
|
|
|
|
compress: Speed,
|
|
|
|
/// ZStd level 1 decompression speed
|
|
|
|
decompress: Speed,
|
|
|
|
/// AES256 GCM encryption speed
|
|
|
|
aes256_gcm: Speed,
|
2020-09-24 06:55:48 +00:00
|
|
|
/// Verify speed
|
|
|
|
verify: Speed,
|
2020-07-10 07:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BENCHMARK_RESULT_2020_TOP: BenchmarkResult = BenchmarkResult {
|
|
|
|
tls: Speed {
|
|
|
|
speed: None,
|
2020-10-14 10:24:15 +00:00
|
|
|
top: 1_000_000.0 * 1235.0, // TLS to localhost, AMD Ryzen 7 2700X
|
2020-07-10 07:13:08 +00:00
|
|
|
},
|
|
|
|
sha256: Speed {
|
|
|
|
speed: None,
|
2020-09-24 06:55:48 +00:00
|
|
|
top: 1_000_000.0 * 2022.0, // AMD Ryzen 7 2700X
|
2020-07-10 07:13:08 +00:00
|
|
|
},
|
|
|
|
compress: Speed {
|
|
|
|
speed: None,
|
2020-09-24 06:55:48 +00:00
|
|
|
top: 1_000_000.0 * 752.0, // AMD Ryzen 7 2700X
|
2020-07-10 07:13:08 +00:00
|
|
|
},
|
|
|
|
decompress: Speed {
|
|
|
|
speed: None,
|
2020-09-24 06:55:48 +00:00
|
|
|
top: 1_000_000.0 * 1198.0, // AMD Ryzen 7 2700X
|
2020-07-10 07:13:08 +00:00
|
|
|
},
|
|
|
|
aes256_gcm: Speed {
|
|
|
|
speed: None,
|
2020-09-24 06:55:48 +00:00
|
|
|
top: 1_000_000.0 * 3645.0, // AMD Ryzen 7 2700X
|
|
|
|
},
|
|
|
|
verify: Speed {
|
|
|
|
speed: None,
|
|
|
|
top: 1_000_000.0 * 758.0, // AMD Ryzen 7 2700X
|
2020-07-10 07:13:08 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-07-02 12:00:32 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
repository: {
|
|
|
|
schema: REPO_URL_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-07-09 14:16:39 +00:00
|
|
|
verbose: {
|
|
|
|
description: "Verbose output.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-07-02 12:00:32 +00:00
|
|
|
keyfile: {
|
|
|
|
schema: KEYFILE_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-07-10 07:13:08 +00:00
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-07-02 12:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// Run benchmark tests
|
|
|
|
pub async fn benchmark(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2020-07-10 07:13:08 +00:00
|
|
|
let repo = extract_repository_from_value(¶m).ok();
|
2020-07-02 12:00:32 +00:00
|
|
|
|
|
|
|
let keyfile = param["keyfile"].as_str().map(PathBuf::from);
|
|
|
|
|
2020-07-09 14:16:39 +00:00
|
|
|
let verbose = param["verbose"].as_bool().unwrap_or(false);
|
|
|
|
|
2020-07-10 07:13:08 +00:00
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
2020-07-02 12:00:32 +00:00
|
|
|
let crypt_config = match keyfile {
|
|
|
|
None => None,
|
|
|
|
Some(path) => {
|
2021-03-31 10:21:47 +00:00
|
|
|
let (key, _, _) = load_and_decrypt_key(&path, &get_encryption_key_password)?;
|
2020-07-02 12:00:32 +00:00
|
|
|
let crypt_config = CryptConfig::new(key)?;
|
|
|
|
Some(Arc::new(crypt_config))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-10 07:13:08 +00:00
|
|
|
let mut benchmark_result = BENCHMARK_RESULT_2020_TOP;
|
|
|
|
|
|
|
|
// do repo tests first, because this may prompt for a password
|
|
|
|
if let Some(repo) = repo {
|
|
|
|
test_upload_speed(&mut benchmark_result, repo, crypt_config.clone(), verbose).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
test_crypt_speed(&mut benchmark_result, verbose)?;
|
|
|
|
|
|
|
|
render_result(&output_format, &benchmark_result)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// print comparison table
|
|
|
|
fn render_result(
|
|
|
|
output_format: &str,
|
|
|
|
benchmark_result: &BenchmarkResult,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut data = serde_json::to_value(benchmark_result)?;
|
2020-12-18 11:26:07 +00:00
|
|
|
let return_type = ReturnType::new(false, &BenchmarkResult::API_SCHEMA);
|
2020-07-10 07:13:08 +00:00
|
|
|
|
|
|
|
let render_speed = |value: &Value, _record: &Value| -> Result<String, Error> {
|
|
|
|
match value["speed"].as_f64() {
|
|
|
|
None => Ok(String::from("not tested")),
|
|
|
|
Some(speed) => {
|
|
|
|
let top = value["top"].as_f64().unwrap();
|
|
|
|
Ok(format!("{:.2} MB/s ({:.0}%)", speed/1_000_000.0, (speed*100.0)/top))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let options = default_table_format_options()
|
|
|
|
.column(ColumnConfig::new("tls")
|
|
|
|
.header("TLS (maximal backup upload speed)")
|
|
|
|
.right_align(false).renderer(render_speed))
|
|
|
|
.column(ColumnConfig::new("sha256")
|
2020-08-18 11:46:50 +00:00
|
|
|
.header("SHA256 checksum computation speed")
|
2020-07-10 07:13:08 +00:00
|
|
|
.right_align(false).renderer(render_speed))
|
|
|
|
.column(ColumnConfig::new("compress")
|
|
|
|
.header("ZStd level 1 compression speed")
|
|
|
|
.right_align(false).renderer(render_speed))
|
|
|
|
.column(ColumnConfig::new("decompress")
|
|
|
|
.header("ZStd level 1 decompression speed")
|
|
|
|
.right_align(false).renderer(render_speed))
|
2020-09-24 06:55:48 +00:00
|
|
|
.column(ColumnConfig::new("verify")
|
|
|
|
.header("Chunk verification speed")
|
|
|
|
.right_align(false).renderer(render_speed))
|
|
|
|
.column(ColumnConfig::new("aes256_gcm")
|
2020-07-10 07:13:08 +00:00
|
|
|
.header("AES256 GCM encryption speed")
|
|
|
|
.right_align(false).renderer(render_speed));
|
|
|
|
|
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut data, &return_type, output_format, &options);
|
2020-07-10 07:13:08 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn test_upload_speed(
|
|
|
|
benchmark_result: &mut BenchmarkResult,
|
|
|
|
repo: BackupRepository,
|
|
|
|
crypt_config: Option<Arc<CryptConfig>>,
|
|
|
|
verbose: bool,
|
|
|
|
) -> Result<(), Error> {
|
2020-07-09 15:07:22 +00:00
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
let backup_time = proxmox_time::epoch_i64();
|
2020-07-02 12:00:32 +00:00
|
|
|
|
2020-11-10 10:54:50 +00:00
|
|
|
let client = connect(&repo)?;
|
2020-07-02 12:00:32 +00:00
|
|
|
record_repository(&repo);
|
|
|
|
|
2020-07-10 07:13:08 +00:00
|
|
|
if verbose { eprintln!("Connecting to backup server"); }
|
2020-07-02 12:00:32 +00:00
|
|
|
let client = BackupWriter::start(
|
|
|
|
client,
|
|
|
|
crypt_config.clone(),
|
|
|
|
repo.store(),
|
|
|
|
"host",
|
2020-07-09 14:16:39 +00:00
|
|
|
"benchmark",
|
2020-07-02 12:00:32 +00:00
|
|
|
backup_time,
|
|
|
|
false,
|
2020-09-02 09:41:22 +00:00
|
|
|
true
|
2020-07-02 12:00:32 +00:00
|
|
|
).await?;
|
|
|
|
|
2020-07-10 07:13:08 +00:00
|
|
|
if verbose { eprintln!("Start TLS speed test"); }
|
2020-07-09 14:16:39 +00:00
|
|
|
let speed = client.upload_speedtest(verbose).await?;
|
2020-07-02 12:00:32 +00:00
|
|
|
|
2020-07-10 07:13:08 +00:00
|
|
|
eprintln!("TLS speed: {:.2} MB/s", speed/1_000_000.0);
|
|
|
|
|
|
|
|
benchmark_result.tls.speed = Some(speed);
|
2020-07-02 12:00:32 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-07-09 15:07:22 +00:00
|
|
|
|
2020-07-10 07:13:08 +00:00
|
|
|
// test hash/crypt/compress speed
|
|
|
|
fn test_crypt_speed(
|
|
|
|
benchmark_result: &mut BenchmarkResult,
|
|
|
|
_verbose: bool,
|
|
|
|
) -> Result<(), Error> {
|
2020-07-09 15:07:22 +00:00
|
|
|
|
|
|
|
let pw = b"test";
|
|
|
|
|
|
|
|
let kdf = KeyDerivationConfig::Scrypt {
|
|
|
|
n: 65536,
|
|
|
|
r: 8,
|
|
|
|
p: 1,
|
|
|
|
salt: Vec::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let testkey = kdf.derive_key(pw)?;
|
|
|
|
|
|
|
|
let crypt_config = CryptConfig::new(testkey)?;
|
|
|
|
|
2020-09-24 06:55:48 +00:00
|
|
|
//let random_data = proxmox::sys::linux::random_data(1024*1024)?;
|
|
|
|
let mut random_data = vec![];
|
|
|
|
// generate pseudo random byte sequence
|
|
|
|
for i in 0..256*1024 {
|
|
|
|
for j in 0..4 {
|
|
|
|
let byte = ((i >> (j<<3))&0xff) as u8;
|
|
|
|
random_data.push(byte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(random_data.len(), 1024*1024);
|
2020-07-09 15:07:22 +00:00
|
|
|
|
|
|
|
let start_time = std::time::Instant::now();
|
|
|
|
|
|
|
|
let mut bytes = 0;
|
|
|
|
loop {
|
|
|
|
openssl::sha::sha256(&random_data);
|
|
|
|
bytes += random_data.len();
|
|
|
|
if start_time.elapsed().as_micros() > 1_000_000 { break; }
|
|
|
|
}
|
|
|
|
let speed = (bytes as f64)/start_time.elapsed().as_secs_f64();
|
2020-07-10 07:13:08 +00:00
|
|
|
benchmark_result.sha256.speed = Some(speed);
|
2020-07-09 15:07:22 +00:00
|
|
|
|
2021-01-19 10:37:49 +00:00
|
|
|
eprintln!("SHA256 speed: {:.2} MB/s", speed/1_000_000.0);
|
2020-07-09 15:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
let start_time = std::time::Instant::now();
|
|
|
|
|
|
|
|
let mut bytes = 0;
|
|
|
|
loop {
|
|
|
|
let mut reader = &random_data[..];
|
|
|
|
zstd::stream::encode_all(&mut reader, 1)?;
|
|
|
|
bytes += random_data.len();
|
2020-07-10 07:13:08 +00:00
|
|
|
if start_time.elapsed().as_micros() > 3_000_000 { break; }
|
2020-07-09 15:07:22 +00:00
|
|
|
}
|
|
|
|
let speed = (bytes as f64)/start_time.elapsed().as_secs_f64();
|
2020-07-10 07:13:08 +00:00
|
|
|
benchmark_result.compress.speed = Some(speed);
|
2020-07-09 15:07:22 +00:00
|
|
|
|
2021-01-19 10:37:49 +00:00
|
|
|
eprintln!("Compression speed: {:.2} MB/s", speed/1_000_000.0);
|
2020-07-09 15:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
let start_time = std::time::Instant::now();
|
|
|
|
|
|
|
|
let compressed_data = {
|
|
|
|
let mut reader = &random_data[..];
|
|
|
|
zstd::stream::encode_all(&mut reader, 1)?
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = 0;
|
|
|
|
loop {
|
|
|
|
let mut reader = &compressed_data[..];
|
|
|
|
let data = zstd::stream::decode_all(&mut reader)?;
|
|
|
|
bytes += data.len();
|
|
|
|
if start_time.elapsed().as_micros() > 1_000_000 { break; }
|
|
|
|
}
|
|
|
|
let speed = (bytes as f64)/start_time.elapsed().as_secs_f64();
|
2020-07-10 07:13:08 +00:00
|
|
|
benchmark_result.decompress.speed = Some(speed);
|
2020-07-09 15:07:22 +00:00
|
|
|
|
2021-01-19 10:37:49 +00:00
|
|
|
eprintln!("Decompress speed: {:.2} MB/s", speed/1_000_000.0);
|
2020-07-09 15:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
let start_time = std::time::Instant::now();
|
|
|
|
|
|
|
|
let mut bytes = 0;
|
|
|
|
loop {
|
|
|
|
let mut out = Vec::new();
|
2021-09-06 09:48:08 +00:00
|
|
|
DataBlob::encrypt_benchmark(&crypt_config, &random_data, &mut out)?;
|
2020-07-09 15:07:22 +00:00
|
|
|
bytes += random_data.len();
|
|
|
|
if start_time.elapsed().as_micros() > 1_000_000 { break; }
|
|
|
|
}
|
|
|
|
let speed = (bytes as f64)/start_time.elapsed().as_secs_f64();
|
2020-07-10 07:13:08 +00:00
|
|
|
benchmark_result.aes256_gcm.speed = Some(speed);
|
2020-07-09 15:07:22 +00:00
|
|
|
|
2021-01-19 10:37:49 +00:00
|
|
|
eprintln!("AES256/GCM speed: {:.2} MB/s", speed/1_000_000.0);
|
2020-07-09 15:07:22 +00:00
|
|
|
|
2020-09-24 06:55:48 +00:00
|
|
|
|
|
|
|
let start_time = std::time::Instant::now();
|
|
|
|
|
|
|
|
let (chunk, digest) = DataChunkBuilder::new(&random_data)
|
|
|
|
.compress(true)
|
|
|
|
.build()?;
|
|
|
|
|
|
|
|
let mut bytes = 0;
|
|
|
|
loop {
|
|
|
|
chunk.verify_unencrypted(random_data.len(), &digest)?;
|
|
|
|
bytes += random_data.len();
|
|
|
|
if start_time.elapsed().as_micros() > 1_000_000 { break; }
|
|
|
|
}
|
|
|
|
let speed = (bytes as f64)/start_time.elapsed().as_secs_f64();
|
|
|
|
benchmark_result.verify.speed = Some(speed);
|
|
|
|
|
2021-01-19 10:37:49 +00:00
|
|
|
eprintln!("Verify speed: {:.2} MB/s", speed/1_000_000.0);
|
2020-09-24 06:55:48 +00:00
|
|
|
|
2020-07-09 15:07:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|