StyledPropertyBenchmark.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Subjects;
  4. using System.Text;
  5. using Avalonia.Data;
  6. using BenchmarkDotNet.Attributes;
  7. namespace Avalonia.Benchmarks.Base
  8. {
  9. [MemoryDiagnoser]
  10. public class StyledPropertyBenchmarks
  11. {
  12. [Benchmark]
  13. public void Set_Int_Property_LocalValue()
  14. {
  15. var obj = new StyledClass();
  16. for (var i = 0; i < 100; ++i)
  17. {
  18. obj.IntValue += 1;
  19. }
  20. }
  21. [Benchmark]
  22. public void Set_Int_Property_Multiple_Priorities()
  23. {
  24. var obj = new StyledClass();
  25. var value = 0;
  26. for (var i = 0; i < 100; ++i)
  27. {
  28. for (var p = BindingPriority.Animation; p <= BindingPriority.Style; ++p)
  29. {
  30. obj.SetValue(StyledClass.IntValueProperty, value++, p);
  31. }
  32. }
  33. }
  34. [Benchmark]
  35. public void Set_Int_Property_TemplatedParent()
  36. {
  37. var obj = new StyledClass();
  38. for (var i = 0; i < 100; ++i)
  39. {
  40. obj.SetValue(StyledClass.IntValueProperty, obj.IntValue + 1, BindingPriority.Template);
  41. }
  42. }
  43. [Benchmark]
  44. public void Bind_Int_Property_LocalValue()
  45. {
  46. var obj = new StyledClass();
  47. var source = new Subject<BindingValue<int>>();
  48. obj.Bind(StyledClass.IntValueProperty, source);
  49. for (var i = 0; i < 100; ++i)
  50. {
  51. source.OnNext(i);
  52. }
  53. }
  54. [Benchmark]
  55. public void Bind_Int_Property_Multiple_Priorities()
  56. {
  57. var obj = new StyledClass();
  58. var sources = new List<Subject<BindingValue<int>>>();
  59. var value = 0;
  60. for (var p = BindingPriority.Animation; p <= BindingPriority.Style; ++p)
  61. {
  62. var source = new Subject<BindingValue<int>>();
  63. sources.Add(source);
  64. obj.Bind(StyledClass.IntValueProperty, source, p);
  65. }
  66. for (var i = 0; i < 100; ++i)
  67. {
  68. foreach (var source in sources)
  69. {
  70. source.OnNext(value++);
  71. }
  72. }
  73. }
  74. [Benchmark]
  75. public void Set_Validated_Int_Property_LocalValue()
  76. {
  77. var obj = new StyledClass();
  78. for (var i = 0; i < 100; ++i)
  79. {
  80. obj.ValidatedIntValue += 1;
  81. }
  82. }
  83. [Benchmark]
  84. public void Set_Coerced_Int_Property_LocalValue()
  85. {
  86. var obj = new StyledClass();
  87. for (var i = 0; i < 100; ++i)
  88. {
  89. obj.CoercedIntValue += 1;
  90. }
  91. }
  92. class StyledClass : AvaloniaObject
  93. {
  94. public static readonly StyledProperty<int> IntValueProperty =
  95. AvaloniaProperty.Register<StyledClass, int>(nameof(IntValue));
  96. public static readonly StyledProperty<int> ValidatedIntValueProperty =
  97. AvaloniaProperty.Register<StyledClass, int>(nameof(ValidatedIntValue), validate: ValidateIntValue);
  98. public static readonly StyledProperty<int> CoercedIntValueProperty =
  99. AvaloniaProperty.Register<StyledClass, int>(nameof(CoercedIntValue), coerce: CoerceIntValue);
  100. public int IntValue
  101. {
  102. get => GetValue(IntValueProperty);
  103. set => SetValue(IntValueProperty, value);
  104. }
  105. public int ValidatedIntValue
  106. {
  107. get => GetValue(ValidatedIntValueProperty);
  108. set => SetValue(ValidatedIntValueProperty, value);
  109. }
  110. public int CoercedIntValue
  111. {
  112. get => GetValue(CoercedIntValueProperty);
  113. set => SetValue(CoercedIntValueProperty, value);
  114. }
  115. private static bool ValidateIntValue(int arg)
  116. {
  117. return arg < 1000;
  118. }
  119. private static int CoerceIntValue(IAvaloniaObject arg1, int arg2)
  120. {
  121. return Math.Min(1000, arg2);
  122. }
  123. }
  124. }
  125. }