pack.rs (2961B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022-2025 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 use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; 17 use uri_pack::{pack_uri, unpack_uri}; 18 19 /// Ascii char that can be packed into 5 bits 20 pub(crate) const PACKED: [u8; 30] = [ 21 b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p', 22 b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z', b'.', b'/', b'-', b'%', 23 ]; 24 25 fn rand_compat(size: usize) -> String { 26 String::from_utf8( 27 std::iter::repeat_with(|| fastrand::u8(b'!'..=b'~')) 28 .take(size) 29 .collect(), 30 ) 31 .unwrap() 32 } 33 34 fn rand_simple(size: usize) -> String { 35 String::from_utf8( 36 std::iter::repeat_with(|| PACKED[fastrand::usize(..PACKED.len())]) 37 .take(size) 38 .collect(), 39 ) 40 .unwrap() 41 } 42 43 fn criterion_benchmark(c: &mut Criterion) { 44 let mut group = c.benchmark_group("Uri"); 45 for size in [50, 500, 4048].iter() { 46 group.throughput(Throughput::Bytes(*size as u64)); 47 group.bench_with_input(BenchmarkId::new("pack rand", size), size, |b, &size| { 48 b.iter_batched( 49 || rand_compat(size), 50 |uri| pack_uri(&uri).unwrap(), 51 criterion::BatchSize::SmallInput, 52 ) 53 }); 54 group.bench_with_input(BenchmarkId::new("unpack rand", size), size, |b, &size| { 55 b.iter_batched( 56 || pack_uri(&rand_compat(size)).unwrap(), 57 |packed| unpack_uri(&packed), 58 criterion::BatchSize::SmallInput, 59 ) 60 }); 61 group.bench_with_input(BenchmarkId::new("pack simple", size), size, |b, &size| { 62 b.iter_batched( 63 || rand_simple(size), 64 |uri| pack_uri(&uri).unwrap(), 65 criterion::BatchSize::SmallInput, 66 ) 67 }); 68 group.bench_with_input(BenchmarkId::new("unpack simple", size), size, |b, &size| { 69 b.iter_batched( 70 || pack_uri(&rand_simple(size)).unwrap(), 71 |packed| unpack_uri(&packed), 72 criterion::BatchSize::SmallInput, 73 ) 74 }); 75 } 76 group.finish(); 77 } 78 79 criterion_group!(benches, criterion_benchmark); 80 criterion_main!(benches);