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 BenchmarkDotNet.Attributes;
  6. #nullable enable
  7. namespace Avalonia.Benchmarks.Styling
  8. {
  9. [MemoryDiagnoser]
  10. public class Style_Activation
  11. {
  12. private TestClass _target = null!;
  13. public Style_Activation()
  14. {
  15. RuntimeHelpers.RunClassConstructor(typeof(TestClass).TypeHandle);
  16. }
  17. [GlobalSetup]
  18. public void Setup()
  19. {
  20. _target = new TestClass();
  21. var style = new Style(x => x.OfType<TestClass>().Class("foo"))
  22. {
  23. Setters = { new Setter(TestClass.StringProperty, "foo") }
  24. };
  25. style.TryAttach(_target, null, FrameType.Style);
  26. }
  27. [Benchmark]
  28. public void Toggle_Style_Activation_Via_Class()
  29. {
  30. for (var i = 0; i < 100; ++i)
  31. {
  32. _target.Classes.Add("foo");
  33. _target.Classes.Remove("foo");
  34. }
  35. }
  36. private class TestClass : Control
  37. {
  38. public static readonly StyledProperty<string?> StringProperty =
  39. AvaloniaProperty.Register<TestClass, string?>("String");
  40. public static readonly StyledProperty<Struct1> Struct1Property =
  41. AvaloniaProperty.Register<TestClass, Struct1>("Struct1");
  42. public static readonly StyledProperty<Struct2> Struct2Property =
  43. AvaloniaProperty.Register<TestClass, Struct2>("Struct2");
  44. public static readonly StyledProperty<Struct3> Struct3Property =
  45. AvaloniaProperty.Register<TestClass, Struct3>("Struct3");
  46. public static readonly StyledProperty<Struct4> Struct4Property =
  47. AvaloniaProperty.Register<TestClass, Struct4>("Struct4");
  48. public static readonly StyledProperty<Struct5> Struct5Property =
  49. AvaloniaProperty.Register<TestClass, Struct5>("Struct5");
  50. public static readonly StyledProperty<Struct6> Struct6Property =
  51. AvaloniaProperty.Register<TestClass, Struct6>("Struct6");
  52. public static readonly StyledProperty<Struct7> Struct7Property =
  53. AvaloniaProperty.Register<TestClass, Struct7>("Struct7");
  54. public static readonly StyledProperty<Struct8> Struct8Property =
  55. AvaloniaProperty.Register<TestClass, Struct8>("Struct8");
  56. }
  57. }
  58. }