TabbedPageBenchmark.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections;
  3. using System.Runtime.CompilerServices;
  4. using Avalonia.Controls;
  5. using Avalonia.Platform;
  6. using Avalonia.Styling;
  7. using Avalonia.Themes.Fluent;
  8. using Avalonia.Threading;
  9. using Avalonia.UnitTests;
  10. using BenchmarkDotNet.Attributes;
  11. namespace Avalonia.Benchmarks.Navigation
  12. {
  13. /// <summary>
  14. /// Measures the cost of creating a TabbedPage and applying its template
  15. /// as the number of tabs grows.
  16. /// </summary>
  17. [MemoryDiagnoser]
  18. public class TabbedPageCreationBenchmark : IDisposable
  19. {
  20. private readonly IDisposable _app;
  21. private readonly TestRoot _root;
  22. [Params(2, 5, 10)]
  23. public int TabCount { get; set; }
  24. public TabbedPageCreationBenchmark()
  25. {
  26. AssetLoader.RegisterResUriParsers();
  27. _app = UnitTestApplication.Start(TestServices.StyledWindow.With(
  28. theme: () => new Styles { new FluentTheme() }));
  29. _root = new TestRoot(true, null) { Renderer = new NullRenderer() };
  30. _root.LayoutManager.ExecuteInitialLayoutPass();
  31. }
  32. [Benchmark]
  33. [MethodImpl(MethodImplOptions.NoInlining)]
  34. public void Create()
  35. {
  36. var tp = new TabbedPage { PageTransition = null };
  37. for (var i = 0; i < TabCount; i++)
  38. ((IList)tp.Pages!).Add(new ContentPage { Header = $"Tab {i}" });
  39. _root.Child = tp;
  40. _root.LayoutManager.ExecuteLayoutPass();
  41. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  42. }
  43. public void Dispose() => _app.Dispose();
  44. }
  45. /// <summary>
  46. /// Measures the cost of switching the selected tab on a TabbedPage with five tabs (no transition).
  47. /// </summary>
  48. [MemoryDiagnoser]
  49. public class TabbedPageSwitchBenchmark : IDisposable
  50. {
  51. private readonly IDisposable _app;
  52. private readonly TestRoot _root;
  53. private TabbedPage _tabbedPage = null!;
  54. public TabbedPageSwitchBenchmark()
  55. {
  56. AssetLoader.RegisterResUriParsers();
  57. _app = UnitTestApplication.Start(TestServices.StyledWindow.With(
  58. theme: () => new Styles { new FluentTheme() }));
  59. _root = new TestRoot(true, null) { Renderer = new NullRenderer() };
  60. _root.LayoutManager.ExecuteInitialLayoutPass();
  61. }
  62. [IterationSetup]
  63. public void SetupIteration()
  64. {
  65. _tabbedPage = new TabbedPage { PageTransition = null };
  66. for (var i = 0; i < 5; i++)
  67. ((IList)_tabbedPage.Pages!).Add(new ContentPage { Header = $"Tab {i}" });
  68. _root.Child = _tabbedPage;
  69. _root.LayoutManager.ExecuteLayoutPass();
  70. _tabbedPage.SelectedIndex = 0;
  71. _root.LayoutManager.ExecuteLayoutPass();
  72. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  73. }
  74. /// <summary>
  75. /// Switch from tab 0 to tab 1 (no transition).
  76. /// </summary>
  77. [Benchmark]
  78. [MethodImpl(MethodImplOptions.NoInlining)]
  79. public void SwitchTab()
  80. {
  81. _tabbedPage.SelectedIndex = 1;
  82. _root.LayoutManager.ExecuteLayoutPass();
  83. }
  84. public void Dispose() => _app.Dispose();
  85. }
  86. }