ControlsBenchmark.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Avalonia.Controls;
  4. using Avalonia.Threading;
  5. using Avalonia.UnitTests;
  6. using BenchmarkDotNet.Attributes;
  7. namespace Avalonia.Benchmarks.Layout
  8. {
  9. [MemoryDiagnoser]
  10. public class ControlsBenchmark : IDisposable
  11. {
  12. private readonly IDisposable _app;
  13. private readonly TestRoot _root;
  14. public ControlsBenchmark()
  15. {
  16. _app = UnitTestApplication.Start(
  17. TestServices.StyledWindow.With(
  18. renderInterface: new NullRenderingPlatform(),
  19. threadingInterface: new NullThreadingPlatform(),
  20. standardCursorFactory: new NullCursorFactory()));
  21. _root = new TestRoot(true, null)
  22. {
  23. Renderer = new NullRenderer()
  24. };
  25. _root.LayoutManager.ExecuteInitialLayoutPass();
  26. }
  27. [Benchmark]
  28. [MethodImpl(MethodImplOptions.NoInlining)]
  29. public void CreateCalendar()
  30. {
  31. var calendar = new Calendar();
  32. _root.Child = calendar;
  33. _root.LayoutManager.ExecuteLayoutPass();
  34. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  35. }
  36. [Benchmark]
  37. [MethodImpl(MethodImplOptions.NoInlining)]
  38. public void CreateCalendarWithLoaded()
  39. {
  40. using var subscription = Control.LoadedEvent.AddClassHandler<Control>((c, s) => { });
  41. var calendar = new Calendar();
  42. _root.Child = calendar;
  43. _root.LayoutManager.ExecuteLayoutPass();
  44. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  45. }
  46. [Benchmark]
  47. [MethodImpl(MethodImplOptions.NoInlining)]
  48. public void CreateButton()
  49. {
  50. var button = new Button();
  51. _root.Child = button;
  52. _root.LayoutManager.ExecuteLayoutPass();
  53. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  54. }
  55. [Benchmark]
  56. [MethodImpl(MethodImplOptions.NoInlining)]
  57. public void CreateTextBox()
  58. {
  59. var textBox = new TextBox();
  60. _root.Child = textBox;
  61. _root.LayoutManager.ExecuteLayoutPass();
  62. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  63. }
  64. public void Dispose()
  65. {
  66. _app.Dispose();
  67. }
  68. }
  69. }