From fc2439e0cffe599a44abade756023a2d84603120 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 14 Nov 2019 15:15:26 +0100 Subject: [PATCH] Added some benchmarks for styled properties. --- tests/Avalonia.Benchmarks/Base/Properties.cs | 2 +- .../Base/StyledPropertyBenchmark.cs | 101 ++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 tests/Avalonia.Benchmarks/Base/StyledPropertyBenchmark.cs diff --git a/tests/Avalonia.Benchmarks/Base/Properties.cs b/tests/Avalonia.Benchmarks/Base/Properties.cs index 45fc68ac966..e3650d9f4bc 100644 --- a/tests/Avalonia.Benchmarks/Base/Properties.cs +++ b/tests/Avalonia.Benchmarks/Base/Properties.cs @@ -35,7 +35,7 @@ public void BindIntProperty() class Class1 : AvaloniaObject { - public static readonly AvaloniaProperty IntProperty = + public static readonly StyledProperty IntProperty = AvaloniaProperty.Register("Int"); } } diff --git a/tests/Avalonia.Benchmarks/Base/StyledPropertyBenchmark.cs b/tests/Avalonia.Benchmarks/Base/StyledPropertyBenchmark.cs new file mode 100644 index 00000000000..4b57776759d --- /dev/null +++ b/tests/Avalonia.Benchmarks/Base/StyledPropertyBenchmark.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Reactive.Subjects; +using System.Text; +using Avalonia.Data; +using BenchmarkDotNet.Attributes; + +namespace Avalonia.Benchmarks.Base +{ + [MemoryDiagnoser] + public class StyledPropertyBenchmarks + { + [Benchmark] + public void Set_Int_Property_LocalValue() + { + var obj = new StyledClass(); + + for (var i = 0; i < 100; ++i) + { + obj.IntValue += 1; + } + } + + [Benchmark] + public void Set_Int_Property_Multiple_Priorities() + { + var obj = new StyledClass(); + var value = 0; + + for (var i = 0; i < 100; ++i) + { + for (var p = BindingPriority.Animation; p <= BindingPriority.Style; ++p) + { + obj.SetValue(StyledClass.IntValueProperty, value++, p); + } + } + } + + [Benchmark] + public void Set_Int_Property_TemplatedParent() + { + var obj = new StyledClass(); + + for (var i = 0; i < 100; ++i) + { + obj.SetValue(StyledClass.IntValueProperty, obj.IntValue + 1, BindingPriority.TemplatedParent); + } + } + + [Benchmark] + public void Bind_Int_Property_LocalValue() + { + var obj = new StyledClass(); + var source = new Subject>(); + + obj.Bind(StyledClass.IntValueProperty, source); + + for (var i = 0; i < 100; ++i) + { + source.OnNext(i); + } + } + + [Benchmark] + public void Bind_Int_Property_Multiple_Priorities() + { + var obj = new StyledClass(); + var sources = new List>>(); + var value = 0; + + for (var p = BindingPriority.Animation; p <= BindingPriority.Style; ++p) + { + var source = new Subject>(); + sources.Add(source); + obj.Bind(StyledClass.IntValueProperty, source, p); + } + + for (var i = 0; i < 100; ++i) + { + foreach (var source in sources) + { + source.OnNext(value++); + } + } + } + + class StyledClass : AvaloniaObject + { + private int _intValue; + + public static readonly StyledProperty IntValueProperty = + AvaloniaProperty.Register(nameof(IntValue)); + + public int IntValue + { + get => GetValue(IntValueProperty); + set => SetValue(IntValueProperty, value); + } + } + } +}