profile_xpath_slow_unit/
profile_xpath_slow_unit.rs1use std::time::Instant;
10use sup_xml_core::{parse_str, ParseOptions};
11use sup_xml_core::xpath::XPathContext;
12
13const FIXTURE: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
14<catalog xmlns:ex="urn:example" xml:lang="en">
15 <book id="b1" price="9.99">
16 <title>The Pragmatic Programmer</title>
17 <author>Hunt</author>
18 <author>Thomas</author>
19 <year>1999</year>
20 <tags><tag>classic</tag><tag>craft</tag></tags>
21 </book>
22 <book id="b2" price="42">
23 <title lang="en">Compilers</title>
24 <author>Aho</author>
25 <author>Sethi</author>
26 <author>Ullman</author>
27 <year>2006</year>
28 <ex:rating>5</ex:rating>
29 <!-- a comment node for comment() tests -->
30 <?xml-stylesheet href="x.xsl"?>
31 <desc><![CDATA[<not really xml>]]></desc>
32 </book>
33 <book id="b3" price="0">
34 <title/>
35 <year>-1</year>
36 <empty></empty>
37 <nested><a><b><c><d>deep</d></c></b></a></nested>
38 </book>
39 <unicode>café αβγ 中文 𝛼</unicode>
40</catalog>"#;
41
42fn time_one(ctx: &XPathContext, label: &str, expr: &str) {
43 let mut best = std::time::Duration::MAX;
44 let mut last_result = String::new();
45 for _ in 0..3 {
46 let t = Instant::now();
47 let r = ctx.eval(expr);
48 let dt = t.elapsed();
49 if dt < best { best = dt; }
50 last_result = match &r {
51 Ok(_) => "Ok".to_string(),
52 Err(e) => format!("Err: {}", e.message.chars().take(60).collect::<String>()),
53 };
54 }
55 println!("{:>9.2?} [{:>4} B] {label} → {last_result}",
56 best, expr.len());
57}
58
59fn main() {
60 let path = std::env::args().nth(1)
61 .expect("usage: profile_xpath_slow_unit <artifact-path>");
62 let bytes = std::fs::read(&path).expect("read artifact");
63 let full = std::str::from_utf8(&bytes).expect("artifact is UTF-8");
64
65 let doc = parse_str(FIXTURE, &ParseOptions::default()).expect("fixture parses");
66 let ctx = XPathContext::new(&doc);
67
68 use sup_xml_core::xpath::ast::max_predicate_nesting;
71 use sup_xml_core::xpath::parse_xpath;
72 match parse_xpath(full) {
73 Ok(e) => println!("# original artifact parses; predicate-nesting depth = {}", max_predicate_nesting(&e)),
74 Err(e) => println!("# original artifact parse-error: {}",
75 e.message.chars().take(80).collect::<String>()),
76 }
77
78 println!("# best-of-3 timing of slow-unit reductions");
79 println!("{:>9} {:>8} {}", "time", "len", "expression → result");
80 println!("{}", "-".repeat(78));
81
82 time_one(&ctx, "FULL artifact (1018 B)", full);
84
85 time_one(&ctx, "first 512 B", &full[..full.len().min(512)]);
87 time_one(&ctx, "first 256 B", &full[..full.len().min(256)]);
88 time_one(&ctx, "first 128 B", &full[..full.len().min(128)]);
89 time_one(&ctx, "first 64 B", &full[..full.len().min(64)]);
90
91 time_one(&ctx, "just nested substring chains",
93 "r*substring(/*substring(/*substring(/*substring(/*substring(/*substring(/*substring(//book[author='Hu'])))))))");
94 time_one(&ctx, "deep //*/*/* descent chain",
95 "//*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*");
96 time_one(&ctx, "nested predicates (the worst case)",
97 "//*[//*[//*[//*[//*[//*[//*[.='x']]]]]]]");
98 time_one(&ctx, "nested //*/* in predicate",
99 "//*[/*//*//*//*//*//*//*//*//*//*]");
100 time_one(&ctx, "wildcard arithmetic chain",
101 "//*/*/*/* * * * * * * * * * //*/*/*");
102 time_one(&ctx, "many top-level *",
103 "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
104
105 time_one(&ctx, "trivial root", "/");
107 time_one(&ctx, "trivial wildcard", "/*");
108 time_one(&ctx, "all elements", "//*");
109 time_one(&ctx, "all elements with trivial predicate", "//*[true()]");
110 time_one(&ctx, "all elements with attribute access", "//*[@id]");
111 time_one(&ctx, "books filtered by author", "//book[author='Hunt']");
112 time_one(&ctx, "substring on string literal", "substring('hello', 2, 3)");
113 time_one(&ctx, "substring on node text", "substring(//title, 2, 3)");
114}