Measure.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls;
  4. using Avalonia.Layout;
  5. using Avalonia.UnitTests;
  6. using BenchmarkDotNet.Attributes;
  7. namespace Avalonia.Benchmarks.Layout
  8. {
  9. [MemoryDiagnoser]
  10. public class Measure : IDisposable
  11. {
  12. private IDisposable _app;
  13. private TestRoot root;
  14. private List<Control> controls = new List<Control>();
  15. public Measure()
  16. {
  17. _app = UnitTestApplication.Start(TestServices.RealLayoutManager);
  18. var panel = new StackPanel();
  19. root = new TestRoot { Child = panel };
  20. controls.Add(panel);
  21. CreateChildren(panel, 3, 5);
  22. LayoutManager.Instance.ExecuteInitialLayoutPass(root);
  23. }
  24. public void Dispose()
  25. {
  26. _app.Dispose();
  27. }
  28. [Benchmark]
  29. public void Remeasure_Half()
  30. {
  31. var random = new Random(1);
  32. foreach (var control in controls)
  33. {
  34. if (random.Next(2) == 0)
  35. {
  36. control.InvalidateMeasure();
  37. }
  38. }
  39. LayoutManager.Instance.ExecuteLayoutPass();
  40. }
  41. private void CreateChildren(IPanel parent, int childCount, int iterations)
  42. {
  43. for (var i = 0; i < childCount; ++i)
  44. {
  45. var control = new StackPanel();
  46. parent.Children.Add(control);
  47. if (iterations > 0)
  48. {
  49. CreateChildren(control, childCount, iterations - 1);
  50. }
  51. controls.Add(control);
  52. }
  53. }
  54. }
  55. }