encoding.rs (5437B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024, 2025, 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 use std::{hint::black_box, str::FromStr}; 18 19 use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; 20 use taler_common::{ 21 encoding::{base32, base64, hex}, 22 types::base32::Base32, 23 }; 24 25 fn parser(c: &mut Criterion) { 26 macro_rules! bench32 { 27 ($group:expr, $size:expr) => {{ 28 const N: usize = $size; 29 $group.throughput(Throughput::Bytes(N as u64)); 30 31 $group.bench_function(BenchmarkId::new("encode", N), |b| { 32 b.iter_batched( 33 rand::random::<[u8; N]>, 34 |case| black_box(base32::encode(&case)), 35 BatchSize::LargeInput, 36 ) 37 }); 38 $group.bench_function(BenchmarkId::new("fmt", N), |b| { 39 b.iter_batched( 40 Base32::<N>::rand, 41 |case| black_box(case.to_string()), 42 BatchSize::LargeInput, 43 ) 44 }); 45 $group.bench_function(BenchmarkId::new("decode", N), |b| { 46 b.iter_batched( 47 || Base32::<N>::rand().to_string(), 48 |case| black_box(base32::decode(case.as_bytes())), 49 BatchSize::LargeInput, 50 ) 51 }); 52 $group.bench_function(BenchmarkId::new("decode_static", N), |b| { 53 b.iter_batched( 54 || Base32::<N>::rand().to_string(), 55 |case| black_box(Base32::<N>::from_str(&case).unwrap()), 56 BatchSize::LargeInput, 57 ) 58 }); 59 }}; 60 } 61 let mut group = c.benchmark_group("base32"); 62 63 bench32!(group, 64); 64 bench32!(group, 1024); 65 bench32!(group, 16 * 1024); 66 bench32!(group, 1024 * 1024); 67 68 group.finish(); 69 70 macro_rules! bench64 { 71 ($group:expr, $size:expr) => {{ 72 const N: usize = $size; 73 $group.throughput(Throughput::Bytes(N as u64)); 74 75 $group.bench_function(BenchmarkId::new("encode", N), |b| { 76 b.iter_batched( 77 rand::random::<[u8; N]>, 78 |case| black_box(base64::encode(&case)), 79 BatchSize::LargeInput, 80 ) 81 }); 82 $group.bench_function(BenchmarkId::new("fmt", N), |b| { 83 b.iter_batched( 84 rand::random::<[u8; N]>, 85 |case| black_box(base64::fmt(case).to_string()), 86 BatchSize::LargeInput, 87 ) 88 }); 89 $group.bench_function(BenchmarkId::new("decode", N), |b| { 90 b.iter_batched( 91 || base64::encode(rand::random::<[u8; N]>()), 92 |case| black_box(base64::decode(case.as_bytes())), 93 BatchSize::LargeInput, 94 ) 95 }); 96 $group.bench_function(BenchmarkId::new("random", N), |b| { 97 b.iter_batched( 98 || base64::encode(rand::random::<[u8; N]>()), 99 |case| black_box(base64::decode(case.as_bytes())), 100 BatchSize::LargeInput, 101 ) 102 }); 103 }}; 104 } 105 let mut group = c.benchmark_group("base64"); 106 107 bench64!(group, 64); 108 bench64!(group, 1024); 109 bench64!(group, 16 * 1024); 110 bench64!(group, 1024 * 1024); 111 112 group.finish(); 113 114 macro_rules! bench16 { 115 ($group:expr, $size:expr) => {{ 116 const N: usize = $size; 117 $group.throughput(Throughput::Bytes(N as u64)); 118 119 $group.bench_function(BenchmarkId::new("encode", N), |b| { 120 b.iter_batched( 121 rand::random::<[u8; N]>, 122 |case| black_box(hex::encode(&case)), 123 BatchSize::LargeInput, 124 ) 125 }); 126 $group.bench_function(BenchmarkId::new("fmt", N), |b| { 127 b.iter_batched( 128 rand::random::<[u8; N]>, 129 |case| black_box(hex::fmt(case).to_string()), 130 BatchSize::LargeInput, 131 ) 132 }); 133 $group.bench_function(BenchmarkId::new("decode", N), |b| { 134 b.iter_batched( 135 || hex::encode(rand::random::<[u8; N]>()), 136 |case| black_box(hex::decode(case.as_bytes())), 137 BatchSize::LargeInput, 138 ) 139 }); 140 }}; 141 } 142 let mut group = c.benchmark_group("base16"); 143 144 bench16!(group, 64); 145 bench16!(group, 1024); 146 bench16!(group, 16 * 1024); 147 bench16!(group, 1024 * 1024); 148 149 group.finish(); 150 } 151 152 criterion_group!(benches, parser); 153 criterion_main!(benches);