Skip to content

Commit

Permalink
Merge pull request ppy#5927 from peppy/performance-tests-cleanup
Browse files Browse the repository at this point in the history
Refactor performance test scenes
  • Loading branch information
smoogipoo authored Jul 12, 2023
2 parents e1a603f + eb90864 commit 298fecf
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 233 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 50 additions & 4 deletions osu.Framework.Tests/Visual/Performance/PerformanceTestScene.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,87 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Utils;
using osuTK;
using osuTK.Graphics;

namespace osu.Framework.Tests.Visual.Performance
{
public abstract partial class PerformanceTestScene : FrameworkTestScene
{
protected override Container<Drawable> Content => content;

private BufferedContainer content = null!;
private Container content = null!;

private bool rotation;
private bool cycleColour;

[Resolved]
private FrameworkDebugConfigManager debugConfig { get; set; } = null!;

private Bindable<bool> bypassFrontToBack = null!;

private BufferedContainer buffer = null!;

protected override void LoadComplete()
{
base.LoadComplete();

bypassFrontToBack = debugConfig.GetBindable<bool>(DebugSetting.BypassFrontToBackPass);

base.Content.Child = content = new BufferedContainer
base.Content.Child = buffer = new BufferedContainer(pixelSnapping: true)
{
RelativeSizeAxes = Axes.Both,
Child = content = new Container
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
};

AddLabel("General");

AddStep("do nothing", () => { });

AddToggleStep("hide content", v => Content.Alpha = v ? 0 : 1);
AddToggleStep("bypass front to back", v => bypassFrontToBack.Value = v);
AddSliderStep("render scale", 0.01f, 1f, 1f, v => content.FrameBufferScale = new Vector2(v));
AddToggleStep("enable front to back", v => bypassFrontToBack.Value = !v);
AddSliderStep("render scale", 0.01f, 1f, 1f, v => buffer.FrameBufferScale = new Vector2(v));
AddToggleStep("rotate everything", v => rotation = v);
AddToggleStep("cycle colour", v => cycleColour = v);
}

protected override void Update()
{
base.Update();

if (rotation)
{
content.Rotation += (float)Time.Elapsed * 0.01f;
content.Scale = new Vector2(0.5f);
}
else
{
content.Scale = Vector2.One;
content.Rotation = 0;
}

if (cycleColour)
{
var col = Interpolation.ValueAt((MathF.Sin((float)Time.Current / 1000) + 1) / 2, Color4.Red, Color4.SkyBlue, 0f, 1f);

content.Colour = col;
}
else
{
content.Colour = Color4.White;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Utils;
using osuTK;
using osuTK.Graphics;

namespace osu.Framework.Tests.Visual.Performance
{
public abstract partial class RepeatedDrawablePerformanceTestScene : PerformanceTestScene
{
protected readonly BindableFloat DrawableSize = new BindableFloat();
protected readonly BindableInt DrawableCount = new BindableInt();
protected readonly BindableBool GradientColour = new BindableBool();
protected readonly BindableBool RandomiseColour = new BindableBool();

public FillFlowContainer Flow { get; private set; } = null!;

protected override void LoadComplete()
{
base.LoadComplete();

AddLabel("Drawables");

AddSliderStep("size", 1f, 128f, 20f, v => DrawableSize.Value = v);
AddSliderStep("count", 1, 10000, 1000, v => DrawableCount.Value = v);

AddToggleStep("gradient colour", v => GradientColour.Value = v);
AddToggleStep("randomise colour", v => RandomiseColour.Value = v);

Child = Flow = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Padding = new MarginPadding(20f),
Spacing = new Vector2(5f),
};

DrawableCount.BindValueChanged(_ => adjustDrawableCount(), true);

DrawableSize.BindValueChanged(_ => updateMetrics());
GradientColour.BindValueChanged(_ => updateMetrics());
RandomiseColour.BindValueChanged(_ => updateMetrics());
}

protected void Recreate()
{
Flow.Clear();
adjustDrawableCount();
}

protected abstract Drawable CreateDrawable();

private void adjustDrawableCount()
{
while (Flow.Count > DrawableCount.Value)
Flow.Remove(Flow.Children.Last(), true);

while (Flow.Count < DrawableCount.Value)
{
var drawable = CreateDrawable();
updateMetrics(drawable);
Flow.Add(drawable);
}
}

private void updateMetrics()
{
foreach (var b in Flow)
updateMetrics(b);
}

private void updateMetrics(Drawable drawable)
{
drawable.Size = new Vector2(DrawableSize.Value);

if (GradientColour.Value)
{
drawable.Colour = new ColourInfo
{
TopLeft = RandomiseColour.Value ? getRandomColour() : Color4.Red,
TopRight = RandomiseColour.Value ? getRandomColour() : Color4.Blue,
BottomLeft = RandomiseColour.Value ? getRandomColour() : Color4.Green,
BottomRight = RandomiseColour.Value ? getRandomColour() : Color4.Yellow
};
}
else
drawable.Colour = RandomiseColour.Value ? getRandomColour() : Color4.White;
}

private Colour4 getRandomColour() => new Colour4(RNG.NextSingle(), RNG.NextSingle(), RNG.NextSingle(), 1f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osuTK;

namespace osu.Framework.Tests.Visual.Performance
{
public partial class TestSceneBlendingPerformance : TestSceneBoxPerformance
public partial class TestSceneBlendingPerformance : RepeatedDrawablePerformanceTestScene
{
private readonly BindableFloat alpha = new BindableFloat();
private readonly Bindable<BlendingParameters> blendingParameters = new Bindable<BlendingParameters>();
Expand All @@ -18,7 +19,7 @@ protected override void LoadComplete()
base.LoadComplete();

AddLabel("Blending");
AddSliderStep("spacing", -100f, 100f, -20f, v => Flow.Spacing = new Vector2(v));
AddSliderStep("spacing", -20, 20f, -1f, v => Flow.Spacing = new Vector2(v));
AddSliderStep("alpha", 0f, 1f, 0.9f, v => alpha.Value = v);
AddStep("disable blending", () => blendingParameters.Value = BlendingParameters.None);
AddStep("set additive blending", () => blendingParameters.Value = BlendingParameters.Additive);
Expand All @@ -27,15 +28,11 @@ protected override void LoadComplete()

protected override Drawable CreateDrawable() => new TestBlendingBox
{
FillWidth = { BindTarget = FillWidth },
FillHeight = { BindTarget = FillHeight },
GradientColour = { BindTarget = GradientColour },
RandomiseColour = { BindTarget = RandomiseColour },
BlendingParameters = { BindTarget = blendingParameters },
AlphaBindable = { BindTarget = alpha },
};

private partial class TestBlendingBox : TestBox
private partial class TestBlendingBox : Box
{
public readonly IBindable<BlendingParameters> BlendingParameters = new Bindable<BlendingParameters>();
public readonly IBindable<float> AlphaBindable = new Bindable<float>();
Expand Down
103 changes: 3 additions & 100 deletions osu.Framework.Tests/Visual/Performance/TestSceneBoxPerformance.cs
Original file line number Diff line number Diff line change
@@ -1,110 +1,13 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Utils;
using osuTK;
using osuTK.Graphics;
using osu.Framework.Graphics.Shapes;

namespace osu.Framework.Tests.Visual.Performance
{
public partial class TestSceneBoxPerformance : PerformanceTestScene
public sealed partial class TestSceneBoxPerformance : RepeatedDrawablePerformanceTestScene
{
protected readonly BindableFloat FillWidth = new BindableFloat();
protected readonly BindableFloat FillHeight = new BindableFloat();
protected readonly BindableInt SpritesCount = new BindableInt();
protected readonly BindableBool GradientColour = new BindableBool();
protected readonly BindableBool RandomiseColour = new BindableBool();

public FillFlowContainer Flow { get; private set; } = null!;

protected override void LoadComplete()
{
base.LoadComplete();

AddLabel("Boxes");
AddSliderStep("fill width", 0.01f, 1.0f, 0.1f, v => FillWidth.Value = v);
AddSliderStep("fill height", 0.01f, 1.0f, 0.1f, v => FillHeight.Value = v);
AddSliderStep("sprites count", 1, 1000, 100, v => SpritesCount.Value = v);
AddToggleStep("gradient colour", v => GradientColour.Value = v);
AddToggleStep("randomise colour", v => RandomiseColour.Value = v);

Child = Flow = new FillFlowContainer
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding(20f),
Spacing = new Vector2(20f),
};

SpritesCount.BindValueChanged(v =>
{
for (int i = v.OldValue - 1; i >= v.NewValue; i--)
Flow.Remove(Flow.Children[i], true);
for (int i = v.OldValue; i < v.NewValue; i++)
Flow.Add(CreateDrawable());
}, true);
}

protected void Recreate()
{
Flow.Clear();

for (int i = 0; i < SpritesCount.Value; i++)
Flow.Add(CreateDrawable());
}

protected virtual Drawable CreateDrawable() => new TestBox
{
FillWidth = { BindTarget = FillWidth },
FillHeight = { BindTarget = FillHeight },
GradientColour = { BindTarget = GradientColour },
RandomiseColour = { BindTarget = RandomiseColour },
};

protected partial class TestBox : Sprite
{
public readonly IBindable<float> FillWidth = new BindableFloat();
public readonly IBindable<float> FillHeight = new BindableFloat();
public readonly IBindable<bool> GradientColour = new BindableBool();
public readonly IBindable<bool> RandomiseColour = new BindableBool();

[BackgroundDependencyLoader]
private void load(IRenderer renderer)
{
RelativeSizeAxes = Axes.Both;
Texture = renderer.WhitePixel;

FillWidth.BindValueChanged(v => Width = v.NewValue, true);
FillHeight.BindValueChanged(v => Height = v.NewValue, true);

RandomiseColour.BindValueChanged(_ => updateColour());
GradientColour.BindValueChanged(_ => updateColour(), true);

void updateColour()
{
if (GradientColour.Value)
{
Colour = new ColourInfo
{
TopLeft = RandomiseColour.Value ? getRandomColour() : Color4.Red,
TopRight = RandomiseColour.Value ? getRandomColour() : Color4.Blue,
BottomLeft = RandomiseColour.Value ? getRandomColour() : Color4.Green,
BottomRight = RandomiseColour.Value ? getRandomColour() : Color4.Yellow
};
}
else
Colour = RandomiseColour.Value ? getRandomColour() : Color4.White;
}
}

private Colour4 getRandomColour() => new Colour4(RNG.NextSingle(), RNG.NextSingle(), RNG.NextSingle(), 1f);
}
protected override Drawable CreateDrawable() => new Box();
}
}
Loading

0 comments on commit 298fecf

Please sign in to comment.