AvaloniaObject_Construct.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Runtime.CompilerServices;
  2. using BenchmarkDotNet.Attributes;
  3. #nullable enable
  4. namespace Avalonia.Benchmarks.Base
  5. {
  6. [MemoryDiagnoser]
  7. public class AvaloniaObject_Construct
  8. {
  9. public AvaloniaObject_Construct()
  10. {
  11. RuntimeHelpers.RunClassConstructor(typeof(TestClass).TypeHandle);
  12. }
  13. [Benchmark(Baseline = true)]
  14. public void ConstructClrObject_And_Set_Values()
  15. {
  16. var target = new BaselineTestClass();
  17. target.StringProperty = "foo";
  18. target.Struct1Property = new(1);
  19. target.Struct2Property = new(1);
  20. target.Struct3Property = new(1);
  21. target.Struct4Property = new(1);
  22. target.Struct5Property = new(1);
  23. target.Struct6Property = new(1);
  24. target.Struct7Property = new(1);
  25. target.Struct8Property = new(1);
  26. }
  27. [Benchmark]
  28. public void Construct_And_Set_Values()
  29. {
  30. var target = new TestClass();
  31. target.SetValue(TestClass.StringProperty, "foo");
  32. target.SetValue(TestClass.Struct1Property, new(1));
  33. target.SetValue(TestClass.Struct2Property, new(1));
  34. target.SetValue(TestClass.Struct3Property, new(1));
  35. target.SetValue(TestClass.Struct4Property, new(1));
  36. target.SetValue(TestClass.Struct5Property, new(1));
  37. target.SetValue(TestClass.Struct6Property, new(1));
  38. target.SetValue(TestClass.Struct7Property, new(1));
  39. target.SetValue(TestClass.Struct8Property, new(1));
  40. }
  41. private class TestClass : AvaloniaObject
  42. {
  43. public static readonly StyledProperty<string> StringProperty =
  44. AvaloniaProperty.Register<TestClass, string>("String");
  45. public static readonly StyledProperty<Struct1> Struct1Property =
  46. AvaloniaProperty.Register<TestClass, Struct1>("Struct1");
  47. public static readonly StyledProperty<Struct2> Struct2Property =
  48. AvaloniaProperty.Register<TestClass, Struct2>("Struct2");
  49. public static readonly StyledProperty<Struct3> Struct3Property =
  50. AvaloniaProperty.Register<TestClass, Struct3>("Struct3");
  51. public static readonly StyledProperty<Struct4> Struct4Property =
  52. AvaloniaProperty.Register<TestClass, Struct4>("Struct4");
  53. public static readonly StyledProperty<Struct5> Struct5Property =
  54. AvaloniaProperty.Register<TestClass, Struct5>("Struct5");
  55. public static readonly StyledProperty<Struct6> Struct6Property =
  56. AvaloniaProperty.Register<TestClass, Struct6>("Struct6");
  57. public static readonly StyledProperty<Struct7> Struct7Property =
  58. AvaloniaProperty.Register<TestClass, Struct7>("Struct7");
  59. public static readonly StyledProperty<Struct8> Struct8Property =
  60. AvaloniaProperty.Register<TestClass, Struct8>("Struct8");
  61. }
  62. private class BaselineTestClass
  63. {
  64. public string? StringProperty { get; set; }
  65. public Struct1 Struct1Property { get; set; }
  66. public Struct2 Struct2Property { get; set; }
  67. public Struct3 Struct3Property { get; set; }
  68. public Struct4 Struct4Property { get; set; }
  69. public Struct5 Struct5Property { get; set; }
  70. public Struct6 Struct6Property { get; set; }
  71. public Struct7 Struct7Property { get; set; }
  72. public Struct8 Struct8Property { get; set; }
  73. }
  74. }
  75. }