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