ControlsBenchmark.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Avalonia.Controls;
  4. using Avalonia.Headless;
  5. using Avalonia.Threading;
  6. using Avalonia.UnitTests;
  7. using BenchmarkDotNet.Attributes;
  8. namespace Avalonia.Benchmarks.Layout
  9. {
  10. [MemoryDiagnoser]
  11. public class ControlsBenchmark : IDisposable
  12. {
  13. private readonly IDisposable _app;
  14. private readonly TestRoot _root;
  15. public ControlsBenchmark()
  16. {
  17. _app = UnitTestApplication.Start(TestServices.StyledWindow);
  18. _root = new TestRoot(true, null)
  19. {
  20. Renderer = new NullRenderer()
  21. };
  22. _root.LayoutManager.ExecuteInitialLayoutPass();
  23. }
  24. [Benchmark]
  25. [MethodImpl(MethodImplOptions.NoInlining)]
  26. public void CreateCalendar()
  27. {
  28. var calendar = new Calendar();
  29. _root.Child = calendar;
  30. _root.LayoutManager.ExecuteLayoutPass();
  31. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  32. }
  33. [Benchmark]
  34. [MethodImpl(MethodImplOptions.NoInlining)]
  35. public void CreateCalendarWithLoaded()
  36. {
  37. using var subscription = Control.LoadedEvent.AddClassHandler<Control>((c, s) => { });
  38. var calendar = new Calendar();
  39. _root.Child = calendar;
  40. _root.LayoutManager.ExecuteLayoutPass();
  41. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  42. }
  43. [Benchmark]
  44. [MethodImpl(MethodImplOptions.NoInlining)]
  45. public void CreateControl()
  46. {
  47. var control = new Control();
  48. _root.Child = control;
  49. _root.LayoutManager.ExecuteLayoutPass();
  50. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  51. }
  52. [Benchmark]
  53. [MethodImpl(MethodImplOptions.NoInlining)]
  54. public void CreateDecorator()
  55. {
  56. var control = new Decorator();
  57. _root.Child = control;
  58. _root.LayoutManager.ExecuteLayoutPass();
  59. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  60. }
  61. [Benchmark]
  62. [MethodImpl(MethodImplOptions.NoInlining)]
  63. public void CreateScrollViewer()
  64. {
  65. var control = new ScrollViewer();
  66. _root.Child = control;
  67. _root.LayoutManager.ExecuteLayoutPass();
  68. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  69. }
  70. [Benchmark]
  71. [MethodImpl(MethodImplOptions.NoInlining)]
  72. public void CreateButton()
  73. {
  74. var button = new Button();
  75. _root.Child = button;
  76. _root.LayoutManager.ExecuteLayoutPass();
  77. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  78. }
  79. [Benchmark]
  80. [MethodImpl(MethodImplOptions.NoInlining)]
  81. public void CreateTextBox()
  82. {
  83. var textBox = new TextBox();
  84. _root.Child = textBox;
  85. _root.LayoutManager.ExecuteLayoutPass();
  86. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  87. }
  88. public void Dispose()
  89. {
  90. _app.Dispose();
  91. }
  92. }
  93. }