|
|
@@ -1,9 +1,11 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Diagnostics;
|
|
|
+using System.Reactive.Linq;
|
|
|
using System.Reactive.Subjects;
|
|
|
using System.Runtime.CompilerServices;
|
|
|
using Avalonia.Animation;
|
|
|
+using Avalonia.Animation.Animators;
|
|
|
using Avalonia.Layout;
|
|
|
using BenchmarkDotNet.Attributes;
|
|
|
|
|
|
@@ -14,26 +16,29 @@ namespace Avalonia.Benchmarks.Animations
|
|
|
{
|
|
|
private readonly DoubleTransition _transition;
|
|
|
private readonly DoubleTransitionOld _oldTransition;
|
|
|
- private readonly int _frameCount;
|
|
|
private readonly Subject<double> _timeProducer;
|
|
|
private readonly List<double> _producedValues;
|
|
|
+ private readonly AddValueObserver _observer;
|
|
|
+
|
|
|
+ [Params(10, 100)]
|
|
|
+ public int FrameCount { get; set; }
|
|
|
|
|
|
public TransitionBenchmark()
|
|
|
{
|
|
|
- _frameCount = 100;
|
|
|
-
|
|
|
_oldTransition = new DoubleTransitionOld
|
|
|
{
|
|
|
- Duration = TimeSpan.FromMilliseconds(_frameCount), Property = Layoutable.WidthProperty
|
|
|
+ Duration = TimeSpan.FromMilliseconds(FrameCount), Property = Layoutable.WidthProperty
|
|
|
};
|
|
|
|
|
|
_transition = new DoubleTransition
|
|
|
{
|
|
|
- Duration = TimeSpan.FromMilliseconds(_frameCount), Property = Layoutable.WidthProperty
|
|
|
+ Duration = TimeSpan.FromMilliseconds(FrameCount), Property = Layoutable.WidthProperty
|
|
|
};
|
|
|
|
|
|
_timeProducer = new Subject<double>();
|
|
|
- _producedValues = new List<double>(_frameCount);
|
|
|
+ _producedValues = new List<double>(FrameCount);
|
|
|
+
|
|
|
+ _observer = new AddValueObserver(_producedValues);
|
|
|
}
|
|
|
|
|
|
[Benchmark(Baseline = true)]
|
|
|
@@ -56,14 +61,26 @@ namespace Avalonia.Benchmarks.Animations
|
|
|
|
|
|
_producedValues.Clear();
|
|
|
|
|
|
- using var transitionSub = transitionObs.Subscribe(new AddValueObserver(_producedValues));
|
|
|
+ using var transitionSub = transitionObs.Subscribe(_observer);
|
|
|
|
|
|
- for (int i = 0; i < _frameCount; i++)
|
|
|
+ for (int i = 0; i < FrameCount; i++)
|
|
|
{
|
|
|
- _timeProducer.OnNext(TimeSpan.FromMilliseconds(i).TotalSeconds);
|
|
|
+ _timeProducer.OnNext(i/1000d);
|
|
|
}
|
|
|
|
|
|
- Debug.Assert(_producedValues.Count == _frameCount);
|
|
|
+ Debug.Assert(_producedValues.Count == FrameCount);
|
|
|
+ }
|
|
|
+
|
|
|
+ private class DoubleTransitionOld : Transition<double>
|
|
|
+ {
|
|
|
+ private static readonly DoubleAnimator s_animator = new DoubleAnimator();
|
|
|
+
|
|
|
+ /// <inheritdocs/>
|
|
|
+ public override IObservable<double> DoTransition(IObservable<double> progress, double oldValue, double newValue)
|
|
|
+ {
|
|
|
+ return progress
|
|
|
+ .Select(progress => s_animator.Interpolate(Easing.Ease(progress), oldValue, newValue));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private class AddValueObserver : IObserver<double>
|