Style_Activation.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_Activation
  12. {
  13. private TestClass _target = null!;
  14. public Style_Activation()
  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. }
  28. [Benchmark]
  29. public void Toggle_Style_Activation_Via_Class()
  30. {
  31. for (var i = 0; i < 100; ++i)
  32. {
  33. _target.Classes.Add("foo");
  34. _target.Classes.Remove("foo");
  35. }
  36. }
  37. private class TestClass : Control
  38. {
  39. public static readonly StyledProperty<string?> StringProperty =
  40. AvaloniaProperty.Register<TestClass, string?>("String");
  41. public static readonly StyledProperty<Struct1> Struct1Property =
  42. AvaloniaProperty.Register<TestClass, Struct1>("Struct1");
  43. public static readonly StyledProperty<Struct2> Struct2Property =
  44. AvaloniaProperty.Register<TestClass, Struct2>("Struct2");
  45. public static readonly StyledProperty<Struct3> Struct3Property =
  46. AvaloniaProperty.Register<TestClass, Struct3>("Struct3");
  47. public static readonly StyledProperty<Struct4> Struct4Property =
  48. AvaloniaProperty.Register<TestClass, Struct4>("Struct4");
  49. public static readonly StyledProperty<Struct5> Struct5Property =
  50. AvaloniaProperty.Register<TestClass, Struct5>("Struct5");
  51. public static readonly StyledProperty<Struct6> Struct6Property =
  52. AvaloniaProperty.Register<TestClass, Struct6>("Struct6");
  53. public static readonly StyledProperty<Struct7> Struct7Property =
  54. AvaloniaProperty.Register<TestClass, Struct7>("Struct7");
  55. public static readonly StyledProperty<Struct8> Struct8Property =
  56. AvaloniaProperty.Register<TestClass, Struct8>("Struct8");
  57. }
  58. }
  59. }