main.rs (2549B)
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 std::cmp::Ordering; 17 18 use uri_pack::pack_uri; 19 20 /// Compute efficiency statistics of uri-pack using domains from majestic_million 21 fn main() { 22 let mut majestic = csv::Reader::from_reader(include_str!("majestic_million.csv").as_bytes()); 23 let mut ascii_counter = [0u64; 255]; 24 let mut before_len = 0; 25 let mut after_len = 0; 26 let mut bigger = 0; 27 let mut same = 0; 28 let mut smaller = 0; 29 for record in majestic.records() { 30 let domain = &record.unwrap()[2]; 31 for ascii in domain.as_bytes() { 32 ascii_counter[*ascii as usize] += 1; 33 } 34 let before = domain.len(); 35 let after = pack_uri(domain).unwrap().len(); 36 before_len += before; 37 after_len += after; 38 match before.cmp(&after) { 39 Ordering::Less => bigger += 1, 40 Ordering::Equal => same += 1, 41 Ordering::Greater => smaller += 1, 42 } 43 } 44 let sum: u64 = ascii_counter.iter().sum(); 45 let max_len = ascii_counter.iter().max().unwrap_or(&0).to_string().len(); 46 for (ascii, count) in ascii_counter 47 .into_iter() 48 .enumerate() 49 .filter(|(_, count)| *count > 0) 50 { 51 println!( 52 "{} {:>4$} {:.2}% {:=<5$}", 53 ascii as u8 as char, 54 count, 55 count as f32 / sum as f32 * 100., 56 "", 57 max_len, 58 (count * 200 / sum) as usize, 59 ) 60 } 61 let count = bigger + smaller + same; 62 println!( 63 "\nBefore ~{}b After ~{}b\nBigger {:.2}% Same {:.2}% Smaller {:.2}%", 64 before_len / count, 65 after_len / count, 66 (bigger as f32 / count as f32 * 100.), 67 (same as f32 / count as f32 * 100.), 68 (smaller as f32 / count as f32 * 100.) 69 ); 70 }