ControlsBenchmark.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Avalonia.Controls;
  4. using Avalonia.UnitTests;
  5. using BenchmarkDotNet.Attributes;
  6. namespace Avalonia.Benchmarks.Layout
  7. {
  8. [MemoryDiagnoser]
  9. public class ControlsBenchmark : IDisposable
  10. {
  11. private readonly IDisposable _app;
  12. private readonly TestRoot _root;
  13. public ControlsBenchmark()
  14. {
  15. _app = UnitTestApplication.Start(
  16. TestServices.StyledWindow.With(
  17. renderInterface: new NullRenderingPlatform(),
  18. threadingInterface: new NullThreadingPlatform()));
  19. _root = new TestRoot(true, null)
  20. {
  21. Renderer = new NullRenderer()
  22. };
  23. _root.LayoutManager.ExecuteInitialLayoutPass();
  24. }
  25. [Benchmark]
  26. [MethodImpl(MethodImplOptions.NoInlining)]
  27. public void CreateCalendar()
  28. {
  29. var calendar = new Calendar();
  30. _root.Child = calendar;
  31. _root.LayoutManager.ExecuteLayoutPass();
  32. }
  33. [Benchmark]
  34. [MethodImpl(MethodImplOptions.NoInlining)]
  35. public void CreateButton()
  36. {
  37. var button = new Button();
  38. _root.Child = button;
  39. _root.LayoutManager.ExecuteLayoutPass();
  40. }
  41. [Benchmark]
  42. [MethodImpl(MethodImplOptions.NoInlining)]
  43. public void CreateTextBox()
  44. {
  45. var textBox = new TextBox();
  46. _root.Child = textBox;
  47. _root.LayoutManager.ExecuteLayoutPass();
  48. }
  49. public void Dispose()
  50. {
  51. _app.Dispose();
  52. }
  53. }
  54. }