]> git.proxmox.com Git - proxmox-backup.git/blob - examples/cipherbench.rs
40e4984bd8d270809f7e41964f75d49a9659affc
[proxmox-backup.git] / examples / cipherbench.rs
1 use anyhow::Error;
2
3 // chacha20-poly1305
4
5 fn rate_test(name: &str, bench: &dyn Fn() -> usize) {
6
7 print!("{:<20} ", name);
8
9 let start = std::time::SystemTime::now();
10 let duration = std::time::Duration::new(1, 0);
11
12 let mut bytes = 0;
13
14 loop {
15 bytes += bench();
16 let elapsed = start.elapsed().unwrap();
17 if elapsed > duration { break; }
18 }
19
20 let elapsed = start.elapsed().unwrap();
21 let elapsed = (elapsed.as_secs() as f64) +
22 (elapsed.subsec_millis() as f64)/1000.0;
23
24 println!("{:>8.1} MB/s", (bytes as f64)/(elapsed*1024.0*1024.0));
25 }
26
27
28 fn main() -> Result<(), Error> {
29
30 let input = proxmox::sys::linux::random_data(1024*1024)?;
31
32 rate_test("crc32", &|| {
33 let mut crchasher = crc32fast::Hasher::new();
34 crchasher.update(&input);
35 let _checksum = crchasher.finalize();
36 input.len()
37 });
38
39 rate_test("zstd", &|| {
40 zstd::block::compress(&input, 1).unwrap();
41 input.len()
42 });
43
44 rate_test("sha256", &|| {
45 openssl::sha::sha256(&input);
46 input.len()
47 });
48
49 let key = proxmox::sys::linux::random_data(32)?;
50
51 let iv = proxmox::sys::linux::random_data(16)?;
52
53 let cipher = openssl::symm::Cipher::aes_256_gcm();
54
55 rate_test("aes-256-gcm", &|| {
56 let mut tag = [0u8;16];
57 openssl::symm::encrypt_aead(
58 cipher,
59 &key,
60 Some(&iv),
61 b"",
62 &input,
63 &mut tag).unwrap();
64 input.len()
65 });
66
67 let cipher = openssl::symm::Cipher::chacha20_poly1305();
68
69 rate_test("chacha20-poly1305", &|| {
70 let mut tag = [0u8;16];
71 openssl::symm::encrypt_aead(
72 cipher,
73 &key,
74 Some(&iv[..12]),
75 b"",
76 &input,
77 &mut tag).unwrap();
78 input.len()
79 });
80
81 Ok(())
82 }