summaryrefslogtreecommitdiffstats
path: root/vendor/base64ct/benches/mod.rs
blob: 4d1b8c65b4a72d9239181465f936ee8dadf90334 (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
//! `base64ct` benchmarks

#![feature(test)]
extern crate test;

use base64ct::{Base64Unpadded, Encoding};
use test::Bencher;

const B64_LEN: usize = 100_002;
const RAW_LEN: usize = (3 * B64_LEN) / 4;

#[inline(never)]
fn get_raw_data() -> Vec<u8> {
    (0..RAW_LEN).map(|i| i as u8).collect()
}

#[inline(never)]
fn get_b64_data() -> String {
    (0..B64_LEN)
        .map(|i| match (i % 64) as u8 {
            v @ 0..=25 => (v + 'A' as u8) as char,
            v @ 26..=51 => (v - 26 + 'a' as u8) as char,
            v @ 52..=61 => (v - 52 + '0' as u8) as char,
            62 => '+',
            _ => '/',
        })
        .collect()
}

#[bench]
fn decode_bench(b: &mut Bencher) {
    let b64_data = get_b64_data();
    let mut buf = get_raw_data();
    b.iter(|| {
        let out = Base64Unpadded::decode(&b64_data, &mut buf).unwrap();
        test::black_box(out);
    });
    b.bytes = RAW_LEN as u64;
}

#[bench]
fn decode_in_place_bench(b: &mut Bencher) {
    let mut b64_data = get_b64_data().into_bytes();
    b.iter(|| {
        // since it works on the same buffer over and over,
        // almost always `out` will be an error
        let out = Base64Unpadded::decode_in_place(&mut b64_data);
        let _ = test::black_box(out);
    });
    b.bytes = RAW_LEN as u64;
}

#[bench]
fn encode_bench(b: &mut Bencher) {
    let mut buf = get_b64_data().into_bytes();
    let raw_data = get_raw_data();
    b.iter(|| {
        let out = Base64Unpadded::encode(&raw_data, &mut buf).unwrap();
        test::black_box(out);
    });
    b.bytes = RAW_LEN as u64;
}