Style_Apply.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using Avalonia.Controls;
  5. using Avalonia.PropertyStore;
  6. using Avalonia.Styling;
  7. using Avalonia.UnitTests;
  8. using BenchmarkDotNet.Attributes;
  9. #nullable enable
  10. namespace Avalonia.Benchmarks.Styling
  11. {
  12. [MemoryDiagnoser]
  13. public class Style_Apply
  14. {
  15. private List<Style> _styles = new();
  16. public Style_Apply()
  17. {
  18. RuntimeHelpers.RunClassConstructor(typeof(TestClass).TypeHandle);
  19. }
  20. [Params(1, 5, 50)]
  21. public int MatchingStyles { get; set; }
  22. [Params(1, 5, 50)]
  23. public int NonMatchingStyles { get; set; }
  24. [GlobalSetup]
  25. public void Setup()
  26. {
  27. _styles.Clear();
  28. for (var i = 0; i < MatchingStyles; ++i)
  29. {
  30. _styles.Add(new Style(x => x.OfType<TestClass>())
  31. {
  32. Setters = { new Setter(TestClass.StringProperty, "foo") }
  33. });
  34. }
  35. for (var i = 0; i < NonMatchingStyles; ++i)
  36. {
  37. _styles.Add(new Style(x => x.OfType<TestClass2>().Class("missing"))
  38. {
  39. Setters = { new Setter(TestClass.StringProperty, "foo") }
  40. });
  41. }
  42. }
  43. [Benchmark]
  44. public void Apply_Simple_Styles()
  45. {
  46. var target = new TestClass();
  47. target.GetValueStore().BeginStyling();
  48. foreach (var style in _styles)
  49. StyleHelpers.TryAttach(style, target);
  50. target.GetValueStore().EndStyling();
  51. }
  52. private class TestClass : Control
  53. {
  54. public static readonly StyledProperty<string?> StringProperty =
  55. AvaloniaProperty.Register<TestClass, string?>("String");
  56. public static readonly StyledProperty<Struct1> Struct1Property =
  57. AvaloniaProperty.Register<TestClass, Struct1>("Struct1");
  58. public static readonly StyledProperty<Struct2> Struct2Property =
  59. AvaloniaProperty.Register<TestClass, Struct2>("Struct2");
  60. public static readonly StyledProperty<Struct3> Struct3Property =
  61. AvaloniaProperty.Register<TestClass, Struct3>("Struct3");
  62. public static readonly StyledProperty<Struct4> Struct4Property =
  63. AvaloniaProperty.Register<TestClass, Struct4>("Struct4");
  64. public static readonly StyledProperty<Struct5> Struct5Property =
  65. AvaloniaProperty.Register<TestClass, Struct5>("Struct5");
  66. public static readonly StyledProperty<Struct6> Struct6Property =
  67. AvaloniaProperty.Register<TestClass, Struct6>("Struct6");
  68. public static readonly StyledProperty<Struct7> Struct7Property =
  69. AvaloniaProperty.Register<TestClass, Struct7>("Struct7");
  70. public static readonly StyledProperty<Struct8> Struct8Property =
  71. AvaloniaProperty.Register<TestClass, Struct8>("Struct8");
  72. }
  73. private class TestClass2 : Control
  74. {
  75. }
  76. }
  77. }