diff --git a/tests/Avalonia.Benchmarks/ControlHierarchyCreator.cs b/tests/Avalonia.Benchmarks/ControlHierarchyCreator.cs new file mode 100644 index 0000000000..1c9ba4e6ae --- /dev/null +++ b/tests/Avalonia.Benchmarks/ControlHierarchyCreator.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using Avalonia.Controls; + +namespace Avalonia.Benchmarks +{ + internal class ControlHierarchyCreator + { + public static List CreateChildren(List controls, IPanel parent, int childCount, int innerCount, int iterations) + { + for (var i = 0; i < childCount; ++i) + { + var control = new StackPanel(); + parent.Children.Add(control); + + for (int j = 0; j < innerCount; ++j) + { + var child = new Button(); + + parent.Children.Add(child); + + controls.Add(child); + } + + if (iterations > 0) + { + CreateChildren(controls, control, childCount, innerCount, iterations - 1); + } + + controls.Add(control); + } + + return controls; + } + } +} diff --git a/tests/Avalonia.Benchmarks/Layout/Measure.cs b/tests/Avalonia.Benchmarks/Layout/Measure.cs index b0490d8a0f..d03d17b4d3 100644 --- a/tests/Avalonia.Benchmarks/Layout/Measure.cs +++ b/tests/Avalonia.Benchmarks/Layout/Measure.cs @@ -1,7 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Runtime.CompilerServices; using Avalonia.Controls; -using Avalonia.Layout; using Avalonia.UnitTests; using BenchmarkDotNet.Attributes; @@ -10,48 +9,34 @@ namespace Avalonia.Benchmarks.Layout [MemoryDiagnoser] public class Measure { - private TestRoot root; - private List controls = new List(); + private readonly TestRoot _root; + private readonly List _controls = new List(); public Measure() { var panel = new StackPanel(); - root = new TestRoot { Child = panel }; - controls.Add(panel); - CreateChildren(panel, 3, 5); - root.LayoutManager.ExecuteInitialLayoutPass(root); - } - - [Benchmark] - public void Remeasure_Half() - { - var random = new Random(1); - foreach (var control in controls) + _root = new TestRoot { - if (random.Next(2) == 0) - { - control.InvalidateMeasure(); - } - } + Child = panel, + Renderer = new NullRenderer() + }; - root.LayoutManager.ExecuteLayoutPass(); + _controls.Add(panel); + _controls = ControlHierarchyCreator.CreateChildren(_controls, panel, 3, 5, 5); + + _root.LayoutManager.ExecuteInitialLayoutPass(_root); } - private void CreateChildren(IPanel parent, int childCount, int iterations) + [Benchmark, MethodImpl(MethodImplOptions.NoInlining)] + public void Remeasure() { - for (var i = 0; i < childCount; ++i) + foreach (var control in _controls) { - var control = new StackPanel(); - parent.Children.Add(control); - - if (iterations > 0) - { - CreateChildren(control, childCount, iterations - 1); - } - - controls.Add(control); + control.InvalidateMeasure(); } + + _root.LayoutManager.ExecuteLayoutPass(); } } } diff --git a/tests/Avalonia.Benchmarks/NullRenderer.cs b/tests/Avalonia.Benchmarks/NullRenderer.cs new file mode 100644 index 0000000000..7167eafc87 --- /dev/null +++ b/tests/Avalonia.Benchmarks/NullRenderer.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using Avalonia.Rendering; +using Avalonia.VisualTree; + +namespace Avalonia.Benchmarks +{ + internal class NullRenderer : IRenderer + { + public bool DrawFps { get; set; } + public bool DrawDirtyRects { get; set; } + public event EventHandler SceneInvalidated; + + public void AddDirty(IVisual visual) + { + } + + public void Dispose() + { + } + + public IEnumerable HitTest(Point p, IVisual root, Func filter) => null; + + public void Paint(Rect rect) + { + } + + public void RecalculateChildren(IVisual visual) + { + } + + public void Resized(Size size) + { + } + + public void Start() + { + } + + public void Stop() + { + } + } +}