using System; using System.Xml; using BenchmarkDotNet.Attributes; using Umbraco.Tests.Benchmarks.Config; namespace Umbraco.Tests.Benchmarks; [QuickRunWithMemoryDiagnoserConfig] public class XmlBenchmarks { private const bool UseLegacySchema = false; private XmlDocument _xml; [GlobalSetup] public void Setup() { var templateId = 0; var xmlText = @" ]> 1 This is some content]]> "; _xml = new XmlDocument(); _xml.LoadXml(xmlText); } [GlobalCleanup] public void Cleanup() => _xml = null; [Benchmark] public void XmlWithXPath() { var xpath = "/root/* [@isDoc and @urlName='home']//* [@isDoc and @urlName='sub1']//* [@isDoc and @urlName='sub2']"; var elt = _xml.SelectSingleNode(xpath); if (elt == null) { Console.WriteLine("ERR"); } } [Benchmark] public void XmlWithNavigation() { var elt = _xml.DocumentElement; var id = NavigateElementRoute(elt, new[] { "home", "sub1", "sub2" }); if (id <= 0) { Console.WriteLine("ERR"); } } private int NavigateElementRoute(XmlElement elt, string[] urlParts) { var found = true; var i = 0; while (found && i < urlParts.Length) { found = false; foreach (XmlElement child in elt.ChildNodes) { var noNode = UseLegacySchema ? child.Name != "node" : child.GetAttributeNode("isDoc") == null; if (noNode) { continue; } if (child.GetAttribute("urlName") != urlParts[i]) { continue; } found = true; elt = child; break; } i++; } return found ? int.Parse(elt.GetAttribute("id")) : -1; } }