Skip to content

Commit

Permalink
feat: Adds the Dopes performance test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jun 16, 2020
1 parent 6f1fc26 commit ff61bc2
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\Performance\Performance_Dopes.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\Properties\DataContextProperty.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -3482,6 +3486,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\MarkupExtensionTests\Entity.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\MarkupExtensionTests\InverseBool.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\MarkupExtensionTests\MarkupExtensionBehaviors.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\Performance\Performance_Dopes.xaml.cs">
<DependentUpon>Performance_Dopes.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\StyleTests\Style_Inline.xaml.cs">
<DependentUpon>Style_Inline.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<Page
x:Class="UITests.Windows_UI_Xaml.Performance.Performance_Dopes"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Windows_UI_Xaml.Performance"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="55" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Canvas x:Name="absolute" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0">

</Canvas>

<Grid x:Name="grid" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>

<TextBlock x:Name="dopes"
Grid.Row="0"
Grid.Column="0"
Margin="0,20,0,0"
Padding="7,7,7,7"
Foreground="White"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Text="Warming up.."
Visibility="Collapsed"/>

<StackPanel x:Name="buttons"
Orientation="Horizontal"
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
Margin="5,0,0,25" >

<Button x:Name="stop"
Content="@ Stop"
Background="Red"
HorizontalAlignment="Center"
Visibility="Collapsed"
Click="Stop_Clicked"/>

<Button x:Name="startST"
Content="@ Build"
Background="Blue"
Click="startST_Clicked"/>
<Button x:Name="startGridST"
Content="@ Grid"
Background="Blue"
Click="startGridST_Clicked"/>
<Button x:Name="startChangeST"
Content="@ Change"
Background="Blue"
Click="startChangeST_Clicked"/>
<Button x:Name="startReuseST"
Content="@ Reuse"
Background="Blue"
Click="startChangeReuse_Clicked"/>

</StackPanel>

</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace UITests.Windows_UI_Xaml.Performance
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
[SampleControlInfo("Performance", "MarkupExtension")]
public sealed partial class Performance_Dopes : Page
{
volatile bool breakTest = false;
const int max = 600;

public Performance_Dopes()
{
this.InitializeComponent();
}

private void StartTestST()
{
var rand = new Random(0);

breakTest = false;

var width = absolute.ActualWidth;
var height = absolute.ActualHeight;

const int step = 20;
var labels = new TextBlock[step * 2];

var processed = 0;

long prevTicks = 0;
long prevMs = 0;
int prevProcessed = 0;
double avgSum = 0;
int avgN = 0;
var sw = new Stopwatch();

Action loop = null;

loop = () =>
{
var now = sw.ElapsedMilliseconds;
if (breakTest)
{
var avg = avgSum / avgN;
dopes.Text = string.Format("{0:0.00} Dopes/s (AVG)", avg).PadLeft(21);
return;
}
//60hz, 16ms to build the frame
while (sw.ElapsedMilliseconds - now < 16)
{
var label = new TextBlock()
{
Text = "Dope",
Foreground = new SolidColorBrush(Color.FromArgb(0xFF, (byte)(rand.NextDouble() * 255), (byte)(rand.NextDouble() * 255), (byte)(rand.NextDouble() * 255)))
};
label.RenderTransform = new RotateTransform() { Angle = rand.NextDouble() * 360 };
Canvas.SetLeft(label, rand.NextDouble() * width);
Canvas.SetTop(label, rand.NextDouble() * height);
if (processed > max)
{
absolute.Children.RemoveAt(0);
}
absolute.Children.Add(label);
processed++;
if (sw.ElapsedMilliseconds - prevMs > 500)
{
var r = (double)(processed - prevProcessed) / ((double)(sw.ElapsedTicks - prevTicks) / Stopwatch.Frequency);
prevTicks = sw.ElapsedTicks;
prevProcessed = processed;
if (processed > max)
{
dopes.Text = string.Format("{0:0.00} Dopes/s", r).PadLeft(15);
avgSum += r;
avgN++;
}
prevMs = sw.ElapsedMilliseconds;
}
}
_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => loop());
};

sw.Start();

_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => loop());
}

private void StartTestReuseST()
{
var rand = new Random(0);

breakTest = false;

var width = absolute.ActualWidth;
var height = absolute.ActualHeight;

const int step = 20;
var labels = new TextBlock[step * 2];

var processed = 0;

long prevTicks = 0;
long prevMs = 0;
int prevProcessed = 0;
double avgSum = 0;
int avgN = 0;
var sw = new Stopwatch();

Action loop = null;

System.Collections.Concurrent.ConcurrentBag<TextBlock> _cache = new System.Collections.Concurrent.ConcurrentBag<TextBlock>();

loop = () =>
{
var now = sw.ElapsedMilliseconds;
if (breakTest)
{
var avg = avgSum / avgN;
dopes.Text = string.Format("{0:0.00} Dopes/s (AVG)", avg).PadLeft(21);
return;
}
//60hz, 16ms to build the frame
while (sw.ElapsedMilliseconds - now < 16)
{
if (!_cache.TryTake(out var label))
{
label = new TextBlock();
}
label.Text = "Dope";
label.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, (byte)(rand.NextDouble() * 255), (byte)(rand.NextDouble() * 255), (byte)(rand.NextDouble() * 255)));
label.RenderTransform = new RotateTransform() { Angle = rand.NextDouble() * 360 };
Canvas.SetLeft(label, rand.NextDouble() * width);
Canvas.SetTop(label, rand.NextDouble() * height);
if (processed > max)
{
_cache.Add(absolute.Children[0] as TextBlock);
absolute.Children.RemoveAt(0);
}
absolute.Children.Add(label);
processed++;
if (sw.ElapsedMilliseconds - prevMs > 500)
{
var r = (double)(processed - prevProcessed) / ((double)(sw.ElapsedTicks - prevTicks) / Stopwatch.Frequency);
prevTicks = sw.ElapsedTicks;
prevProcessed = processed;
if (processed > max)
{
dopes.Text = string.Format("{0:0.00} Dopes/s", r).PadLeft(15);
avgSum += r;
avgN++;
}
prevMs = sw.ElapsedMilliseconds;
}
}
_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => loop());
};

sw.Start();

_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => loop());
}

private void SetControlsAtStart()
{
startChangeST.Visibility = startST.Visibility = startGridST.Visibility = Visibility.Collapsed;
stop.Visibility = dopes.Visibility = Visibility.Visible;
absolute.Children.Clear();
grid.Children.Clear();
dopes.Text = "Warming up..";
}

private void startMT_Clicked(System.Object sender, object e)
{
SetControlsAtStart();
//StartTestMT2();
}

private void startST_Clicked(System.Object sender, object e)
{
SetControlsAtStart();
StartTestST();
}

private void startGridST_Clicked(System.Object sender, object e)
{
SetControlsAtStart();
//StartTestGridST();
}

private void startChangeST_Clicked(System.Object sender, object e)
{
SetControlsAtStart();
//StartTestChangeST();
}

private void startChangeReuse_Clicked(System.Object sender, object e)
{
SetControlsAtStart();
StartTestReuseST();
}

private void Stop_Clicked(System.Object sender, object e)
{
breakTest = true;
stop.Visibility = Visibility.Collapsed;
startChangeST.Visibility = startST.Visibility = startGridST.Visibility = Visibility.Visible;
}

}
}

0 comments on commit ff61bc2

Please sign in to comment.