Skip to main content

xml_perf_test/
xml_perf_test.rs

1//! XML parsing performance test
2//! 
3//! Run with: cargo run --release --example xml_perf_test
4
5use std::time::Instant;
6
7fn main() {
8    // Read the test HTML file
9    let html_path = std::env::args().nth(1).unwrap_or_else(|| {
10        eprintln!("Usage: xml_perf_test <path-to-html>");
11        eprintln!("Example: cargo run --release --example xml_perf_test /path/to/printpdf.html");
12        std::process::exit(1);
13    });
14    
15    println!("Loading HTML from: {}", html_path);
16    let html = std::fs::read_to_string(&html_path).expect("Failed to read HTML file");
17    println!("HTML size: {} bytes ({} KB)", html.len(), html.len() / 1024);
18    
19    // Count some statistics
20    let span_count = html.matches("<span").count();
21    let div_count = html.matches("<div").count();
22    let total_tags = html.matches("<").count();
23    println!("Approximate tag counts: {} spans, {} divs, {} total '<' chars", span_count, div_count, total_tags);
24    
25    println!("\n=== Starting XML parsing benchmark ===\n");
26    
27    let start = Instant::now();
28    
29    // Call the XML parser
30    match azul_layout::xml::parse_xml_string(&html) {
31        Ok(nodes) => {
32            let elapsed = start.elapsed();
33            println!("\n=== Parsing complete ===");
34            println!("Time: {:?}", elapsed);
35            println!("Root nodes: {}", nodes.len());
36            
37            // Count total nodes recursively
38            fn count_nodes(nodes: &[azul_core::xml::XmlNodeChild]) -> usize {
39                let mut count = nodes.len();
40                for node in nodes {
41                    if let azul_core::xml::XmlNodeChild::Element(elem) = node {
42                        count += count_nodes(elem.children.as_ref());
43                    }
44                }
45                count
46            }
47            
48            let total_nodes = count_nodes(&nodes);
49            println!("Total nodes (recursive): {}", total_nodes);
50            println!("Nodes per second: {:.0}", total_nodes as f64 / elapsed.as_secs_f64());
51        }
52        Err(e) => {
53            let elapsed = start.elapsed();
54            println!("\n=== Parsing FAILED after {:?} ===", elapsed);
55            println!("Error: {:?}", e);
56        }
57    }
58}