2021-10-08 11:19:37 +02:00
use anyhow ::Error ;
2019-06-13 17:16:43 +02:00
// chacha20-poly1305
2019-06-13 17:24:57 +02:00
fn rate_test ( name : & str , bench : & dyn Fn ( ) -> usize ) {
2019-06-13 17:16:43 +02:00
2019-12-17 08:49:34 +01:00
print! ( " {:<20} " , name ) ;
2019-06-13 17:16:43 +02:00
let start = std ::time ::SystemTime ::now ( ) ;
let duration = std ::time ::Duration ::new ( 1 , 0 ) ;
let mut bytes = 0 ;
loop {
bytes + = bench ( ) ;
let elapsed = start . elapsed ( ) . unwrap ( ) ;
if elapsed > duration { break ; }
}
let elapsed = start . elapsed ( ) . unwrap ( ) ;
let elapsed = ( elapsed . as_secs ( ) as f64 ) +
( elapsed . subsec_millis ( ) as f64 ) / 1000.0 ;
2019-12-17 08:49:34 +01:00
println! ( " {:>8.1} MB/s " , ( bytes as f64 ) / ( elapsed * 1024.0 * 1024.0 ) ) ;
2019-06-13 17:16:43 +02:00
}
fn main ( ) -> Result < ( ) , Error > {
let input = proxmox ::sys ::linux ::random_data ( 1024 * 1024 ) ? ;
2019-06-21 15:58:00 +02:00
rate_test ( " crc32 " , & | | {
let mut crchasher = crc32fast ::Hasher ::new ( ) ;
crchasher . update ( & input ) ;
let _checksum = crchasher . finalize ( ) ;
input . len ( )
} ) ;
2019-06-13 17:24:57 +02:00
rate_test ( " zstd " , & | | {
zstd ::block ::compress ( & input , 1 ) . unwrap ( ) ;
input . len ( )
} ) ;
2019-06-13 17:16:43 +02:00
rate_test ( " sha256 " , & | | {
openssl ::sha ::sha256 ( & input ) ;
input . len ( )
} ) ;
2019-06-13 17:24:57 +02:00
let key = proxmox ::sys ::linux ::random_data ( 32 ) ? ;
let iv = proxmox ::sys ::linux ::random_data ( 16 ) ? ;
2019-06-13 17:16:43 +02:00
let cipher = openssl ::symm ::Cipher ::aes_256_gcm ( ) ;
rate_test ( " aes-256-gcm " , & | | {
let mut tag = [ 0 u8 ; 16 ] ;
openssl ::symm ::encrypt_aead (
cipher ,
& key ,
Some ( & iv ) ,
b " " ,
& input ,
& mut tag ) . unwrap ( ) ;
input . len ( )
} ) ;
let cipher = openssl ::symm ::Cipher ::chacha20_poly1305 ( ) ;
rate_test ( " chacha20-poly1305 " , & | | {
let mut tag = [ 0 u8 ; 16 ] ;
openssl ::symm ::encrypt_aead (
cipher ,
& key ,
2019-12-17 08:45:20 +01:00
Some ( & iv [ .. 12 ] ) ,
2019-06-13 17:16:43 +02:00
b " " ,
& input ,
& mut tag ) . unwrap ( ) ;
input . len ( )
} ) ;
Ok ( ( ) )
}