A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using BenchmarkDotNet.Attributes;
5using NUnit.Framework;
6using osu.Framework.Graphics;
7using osu.Framework.Graphics.Containers;
8
9namespace osu.Framework.Benchmarks
10{
11 public class BenchmarkDrawableAudioWrapper : GameBenchmark
12 {
13 [Test]
14 [Benchmark]
15 public void TransferBetweenParentAdjustmentContainers()
16 {
17 ((TestGame)Game).TransferBetween = true;
18 RunSingleFrame();
19 }
20
21 [Test]
22 [Benchmark]
23 public void TransferToSameParentAdjustmentContainer()
24 {
25 ((TestGame)Game).TransferBetween = false;
26 RunSingleFrame();
27 }
28
29 protected override Game CreateGame() => new TestGame();
30
31 private class TestGame : Game
32 {
33 public readonly AudioContainer Container1 = new AudioContainer { RelativeSizeAxes = Axes.Both };
34 public readonly AudioContainer Container2 = new AudioContainer { RelativeSizeAxes = Axes.Both };
35
36 public readonly AudioContainer Sample = new AudioContainer(); // usually a sample, but we're just testing the base class here
37
38 public bool TransferBetween { get; set; }
39
40 private AudioContainer lastContainer;
41
42 protected override void LoadComplete()
43 {
44 base.LoadComplete();
45
46 InternalChildren = new Drawable[]
47 {
48 Container1,
49 Container2,
50 };
51
52 transferTo(Container1);
53 }
54
55 private void transferTo(AudioContainer target)
56 {
57 lastContainer?.Remove(Sample);
58 target.Add(Sample);
59 lastContainer = target;
60 }
61
62 protected override void Update()
63 {
64 base.Update();
65
66 if (TransferBetween)
67 {
68 transferTo(lastContainer == Container1 ? Container2 : Container1);
69 }
70 else
71 {
72 // simulates the case of pooling, where a sample is usually transferred to the same parent.
73 transferTo(Container1);
74 }
75 }
76 }
77 }
78}