Style_NonActive.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Runtime.CompilerServices;
  2. using Avalonia.Controls;
  3. using Avalonia.PropertyStore;
  4. using Avalonia.Styling;
  5. using Avalonia.UnitTests;
  6. using BenchmarkDotNet.Attributes;
  7. #nullable enable
  8. namespace Avalonia.Benchmarks.Styling
  9. {
  10. [MemoryDiagnoser]
  11. public class Style_NonActive
  12. {
  13. private TestClass _target = null!;
  14. public Style_NonActive()
  15. {
  16. RuntimeHelpers.RunClassConstructor(typeof(TestClass).TypeHandle);
  17. }
  18. [GlobalSetup]
  19. public void Setup()
  20. {
  21. _target = new TestClass();
  22. var style = new Style(x => x.OfType<TestClass>().Class("foo"))
  23. {
  24. Setters = { new Setter(TestClass.StringProperty, "foo") }
  25. };
  26. StyleHelpers.TryAttach(style, _target);
  27. _target.SetValue(TestClass.StringProperty, "foo");
  28. _target.SetValue(TestClass.Struct1Property, new(1));
  29. _target.SetValue(TestClass.Struct2Property, new(1));
  30. _target.SetValue(TestClass.Struct3Property, new(1));
  31. _target.SetValue(TestClass.Struct4Property, new(1));
  32. _target.SetValue(TestClass.Struct5Property, new(1));
  33. _target.SetValue(TestClass.Struct6Property, new(1));
  34. _target.SetValue(TestClass.Struct7Property, new(1));
  35. _target.SetValue(TestClass.Struct8Property, new(1));
  36. }
  37. [Benchmark]
  38. public void Toggle_NonActive_Style_Activation()
  39. {
  40. for (var i = 0; i < 100; ++i)
  41. {
  42. _target.Classes.Add("foo");
  43. _target.Classes.Remove("foo");
  44. }
  45. }
  46. private class TestClass : Control
  47. {
  48. public static readonly StyledProperty<string?> StringProperty =
  49. AvaloniaProperty.Register<TestClass, string?>("String");
  50. public static readonly StyledProperty<Struct1> Struct1Property =
  51. AvaloniaProperty.Register<TestClass, Struct1>("Struct1");
  52. public static readonly StyledProperty<Struct2> Struct2Property =
  53. AvaloniaProperty.Register<TestClass, Struct2>("Struct2");
  54. public static readonly StyledProperty<Struct3> Struct3Property =
  55. AvaloniaProperty.Register<TestClass, Struct3>("Struct3");
  56. public static readonly StyledProperty<Struct4> Struct4Property =
  57. AvaloniaProperty.Register<TestClass, Struct4>("Struct4");
  58. public static readonly StyledProperty<Struct5> Struct5Property =
  59. AvaloniaProperty.Register<TestClass, Struct5>("Struct5");
  60. public static readonly StyledProperty<Struct6> Struct6Property =
  61. AvaloniaProperty.Register<TestClass, Struct6>("Struct6");
  62. public static readonly StyledProperty<Struct7> Struct7Property =
  63. AvaloniaProperty.Register<TestClass, Struct7>("Struct7");
  64. public static readonly StyledProperty<Struct8> Struct8Property =
  65. AvaloniaProperty.Register<TestClass, Struct8>("Struct8");
  66. }
  67. }
  68. }