subject.rs (3482B)
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; 18 19 use criterion::{Criterion, criterion_group, criterion_main}; 20 use rand::{SeedableRng, seq::IndexedRandom}; 21 use taler_api::subject::parse_incoming_unstructured; 22 23 fn parser(c: &mut Criterion) { 24 const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789::: \t\t\t\t\n\n\n\n----++++"; 25 let mut rng = rand::rngs::SmallRng::seed_from_u64(42); 26 let real_simple = [ 27 "Taler TEGY6d9mh9pgwvwpgs0z0095z854xegfy7jj202yd0esp8p0za60", 28 "00Q979QSMJ29S7BJT3DDAVC5A0DR5Z05B7N0QT1RCBQ8FXJPZ6RG", 29 "Taler NDDCAM9XN4HJZFTBD8V6FNE2FJE8GY734PJ5AGQMY06C8D4HB3Z0", 30 "Taler KYC:TEGY6d9mh9pgwvwpgs0z0095z854xegfy7jj202yd0esp8p0za60", 31 "KYC:00Q979QSMJ29S7BJT3DDAVC5A0DR5Z05B7N0QT1RCBQ8FXJPZ6RG", 32 "Taler KYC:NDDCAM9XN4HJZFTBD8V6FNE2FJE8GY734PJ5AGQMY06C8D4HB3Z0", 33 ]; 34 let real_splitted = [ 35 "Taler TEGY6d9mh9pgwvwpgs0z0095z854xegfy7j j202yd0esp8p0za60", 36 "00Q979QSMJ29S7BJT3DDAVC5A0DR5Z05B7N 0QT1RCBQ8FXJPZ6RG", 37 "Taler NDDCAM9XN4HJZFTBD8V6FNE2FJE8G Y734PJ5AGQMY06C8D4HB3Z0", 38 "Taler KYC:TEGY6d9mh9pgwvwpgs0z0095z854xegfy7j j202yd0esp8p0za60", 39 "KYC: 00Q979QSMJ29S7BJT3DDAVC5A0DR5Z05B7N 0QT1RCBQ8FXJPZ6RG", 40 "Taler KIC:NDDCAM9XN4HJZFTBD8V6FNE2FJE8G Y734PJ5AGQMY06C8D4HB3Z0", 41 ]; 42 let randoms: Vec<String> = (0..30) 43 .map(|_| { 44 CHARS 45 .choose_iter(&mut rng) 46 .unwrap() 47 .take(256) 48 .map(|c| *c as char) 49 .collect::<String>() 50 }) 51 .collect(); 52 let chunks: Vec<String> = [ 53 "TEGY6d9mh9pgwvwpgs0z0095z854xegfy7jj202yd0esp8p0za60", 54 "00Q979QSMJ29S7BJT3DDAVC5A0DR5Z05B7N0QT1RCBQ8FXJPZ6RG", 55 "NDDCAM9XN4HJZFTBD8V6FNE2FJE8GY734PJ5AGQMY06C8D4HB3Z0", 56 "4MZT6RS3RVB3B0E2RDMYW0YRA3Y0VPHYV0CYDE6XBB0YMPFXCEG0", 57 ] 58 .iter() 59 .flat_map(|key| { 60 (1..key.len()).map(|chunk_size| { 61 key.as_bytes() 62 .chunks(chunk_size) 63 .flat_map(|c| [std::str::from_utf8(c).unwrap(), " "]) 64 .collect() 65 }) 66 }) 67 .collect(); 68 69 fn bench<A: AsRef<str>>( 70 c: &mut Criterion, 71 name: &str, 72 cases: impl IntoIterator<Item = A> + Copy, 73 ) { 74 c.bench_function(name, |b| { 75 b.iter(|| { 76 for case in cases { 77 parse_incoming_unstructured(black_box(case.as_ref())).ok(); 78 } 79 }) 80 }); 81 } 82 83 bench(c, "subject_real_simple", real_simple); 84 bench(c, "subject_real_splitted", real_splitted); 85 bench(c, "subject_rng", &randoms); 86 bench(c, "subject_chunks", &chunks); 87 } 88 89 criterion_group!(benches, parser); 90 criterion_main!(benches);