summaryrefslogtreecommitdiffstats
path: root/openpgp/benches/decrypt_message.rs
blob: d1dca1d3fb5136ae6dc7674dee4097936094d747 (plain)
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
use criterion::{
    criterion_group, criterion_main, BenchmarkId, Criterion, Throughput,
};

use sequoia_openpgp as openpgp;
use openpgp::cert::Cert;
use openpgp::parse::Parse;

use crate::common::{decrypt, encrypt};

static PASSWORD: &str = "password";

lazy_static::lazy_static! {
    static ref TESTY: Cert =
        Cert::from_bytes(&include_bytes!("../tests/data/keys/testy-private.pgp")[..])
        .unwrap();
    static ref ZEROS_1_MB: Vec<u8> = vec![0; 1024 * 1024];
    static ref ZEROS_10_MB: Vec<u8> = vec![0; 10 * 1024 * 1024];
}

fn decrypt_cert(bytes: &[u8], cert: &Cert) {
    let mut sink = Vec::new();
    decrypt::decrypt_with_cert(&mut sink, bytes, cert).unwrap();
}

fn decrypt_password(bytes: &[u8]) {
    let mut sink = Vec::new();
    decrypt::decrypt_with_password(&mut sink, bytes, PASSWORD).unwrap();
}

fn bench_decrypt(c: &mut Criterion) {
    let mut group = c.benchmark_group("decrypt message");

    // Encrypt a very short, medium and very long message,
    // and then benchmark decryption.
    let messages = &[b"Hello world.", &ZEROS_1_MB[..], &ZEROS_10_MB[..]];

    // Encrypt and decrypt with password
    messages.iter().for_each(|m| {
        let encrypted = encrypt::encrypt_with_password(m, PASSWORD).unwrap();
        group.throughput(Throughput::Bytes(encrypted.len() as u64));
        group.bench_with_input(
            BenchmarkId::new("password", m.len()),
            &encrypted,
            |b, e| b.iter(|| decrypt_password(e)),
        );
    });

    // Encrypt and decrypt with a cert
    messages.iter().for_each(|m| {
        let encrypted = encrypt::encrypt_to_cert(m, &TESTY).unwrap();
        group.throughput(Throughput::Bytes(encrypted.len() as u64));
        group.bench_with_input(
            BenchmarkId::new("cert", m.len()),
            &encrypted,
            |b, e| b.iter(|| decrypt_cert(e, &TESTY)),
        );
    });

    group.finish();
}

criterion_group!(benches, bench_decrypt);
criterion_main!(benches);