From ffe0020698478f59e12a1ebd839df5f1ccc80ba8 Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Mon, 10 Feb 2020 23:30:54 +0100 Subject: [PATCH] Benchmark for creating Calendars. --- .../Layout/CalendarBenchmark.cs | 43 ++++++++++ .../NullFormattedTextImpl.cs | 36 +++++++++ .../NullRenderingPlatform.cs | 78 +++++++++++++++++++ .../NullThreadingPlatform.cs | 28 +++++++ 4 files changed, 185 insertions(+) create mode 100644 tests/Avalonia.Benchmarks/Layout/CalendarBenchmark.cs create mode 100644 tests/Avalonia.Benchmarks/NullFormattedTextImpl.cs create mode 100644 tests/Avalonia.Benchmarks/NullRenderingPlatform.cs create mode 100644 tests/Avalonia.Benchmarks/NullThreadingPlatform.cs diff --git a/tests/Avalonia.Benchmarks/Layout/CalendarBenchmark.cs b/tests/Avalonia.Benchmarks/Layout/CalendarBenchmark.cs new file mode 100644 index 0000000000..8739e21486 --- /dev/null +++ b/tests/Avalonia.Benchmarks/Layout/CalendarBenchmark.cs @@ -0,0 +1,43 @@ +using System; +using System.Runtime.CompilerServices; +using Avalonia.Controls; +using Avalonia.UnitTests; +using BenchmarkDotNet.Attributes; + +namespace Avalonia.Benchmarks.Layout +{ + [MemoryDiagnoser, InProcess, IterationCount(16)] + public class CalendarBenchmark : IDisposable + { + private readonly IDisposable _app; + private readonly TestRoot _root; + + public CalendarBenchmark() + { + _app = UnitTestApplication.Start(TestServices.StyledWindow.With(renderInterface: new NullRenderingPlatform(), threadingInterface: new NullThreadingPlatform())); + + _root = new TestRoot(true, null) + { + Renderer = new NullRenderer(), + }; + + _root.LayoutManager.ExecuteInitialLayoutPass(_root); + } + + [Benchmark] + [MethodImpl(MethodImplOptions.NoInlining)] + public void CreateCalendar() + { + var calendar = new Calendar(); + + _root.Child = calendar; + + _root.LayoutManager.ExecuteLayoutPass(); + } + + public void Dispose() + { + _app.Dispose(); + } + } +} diff --git a/tests/Avalonia.Benchmarks/NullFormattedTextImpl.cs b/tests/Avalonia.Benchmarks/NullFormattedTextImpl.cs new file mode 100644 index 0000000000..f886d077cc --- /dev/null +++ b/tests/Avalonia.Benchmarks/NullFormattedTextImpl.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using Avalonia.Media; +using Avalonia.Platform; + +namespace Avalonia.Benchmarks +{ + internal class NullFormattedTextImpl : IFormattedTextImpl + { + public Size Constraint { get; } + + public Rect Bounds { get; } + + public string Text { get; } + + public IEnumerable GetLines() + { + throw new NotImplementedException(); + } + + public TextHitTestResult HitTestPoint(Point point) + { + throw new NotImplementedException(); + } + + public Rect HitTestTextPosition(int index) + { + throw new NotImplementedException(); + } + + public IEnumerable HitTestTextRange(int index, int length) + { + throw new NotImplementedException(); + } + } +} diff --git a/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs b/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs new file mode 100644 index 0000000000..101d40f00a --- /dev/null +++ b/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Avalonia.Media; +using Avalonia.Platform; +using Avalonia.UnitTests; + +namespace Avalonia.Benchmarks +{ + internal class NullRenderingPlatform : IPlatformRenderInterface + { + public IFormattedTextImpl CreateFormattedText(string text, Typeface typeface, double fontSize, TextAlignment textAlignment, + TextWrapping wrapping, Size constraint, IReadOnlyList spans) + { + return new NullFormattedTextImpl(); + } + + public IGeometryImpl CreateEllipseGeometry(Rect rect) + { + throw new NotImplementedException(); + } + + public IGeometryImpl CreateLineGeometry(Point p1, Point p2) + { + throw new NotImplementedException(); + } + + public IGeometryImpl CreateRectangleGeometry(Rect rect) + { + throw new NotImplementedException(); + } + + public IStreamGeometryImpl CreateStreamGeometry() + { + return new MockStreamGeometryImpl(); + } + + public IRenderTarget CreateRenderTarget(IEnumerable surfaces) + { + throw new NotImplementedException(); + } + + public IRenderTargetBitmapImpl CreateRenderTargetBitmap(PixelSize size, Vector dpi) + { + throw new NotImplementedException(); + } + + public IWriteableBitmapImpl CreateWriteableBitmap(PixelSize size, Vector dpi, PixelFormat? format = null) + { + throw new NotImplementedException(); + } + + public IBitmapImpl LoadBitmap(string fileName) + { + throw new NotImplementedException(); + } + + public IBitmapImpl LoadBitmap(Stream stream) + { + throw new NotImplementedException(); + } + + public IBitmapImpl LoadBitmap(PixelFormat format, IntPtr data, PixelSize size, Vector dpi, int stride) + { + throw new NotImplementedException(); + } + + public IFontManagerImpl CreateFontManager() + { + return new MockFontManagerImpl(); + } + + public IGlyphRunImpl CreateGlyphRun(GlyphRun glyphRun, out double width) + { + throw new NotImplementedException(); + } + } +} diff --git a/tests/Avalonia.Benchmarks/NullThreadingPlatform.cs b/tests/Avalonia.Benchmarks/NullThreadingPlatform.cs new file mode 100644 index 0000000000..ba84b5afcc --- /dev/null +++ b/tests/Avalonia.Benchmarks/NullThreadingPlatform.cs @@ -0,0 +1,28 @@ +using System; +using System.Reactive.Disposables; +using System.Threading; +using Avalonia.Platform; +using Avalonia.Threading; + +namespace Avalonia.Benchmarks +{ + internal class NullThreadingPlatform : IPlatformThreadingInterface + { + public void RunLoop(CancellationToken cancellationToken) + { + } + + public IDisposable StartTimer(DispatcherPriority priority, TimeSpan interval, Action tick) + { + return Disposable.Empty; + } + + public void Signal(DispatcherPriority priority) + { + } + + public bool CurrentThreadIsLoopThread => true; + + public event Action Signaled; + } +}