Skip to content

Commit

Permalink
Merge pull request #13 from jonisavo/perf/new-cache
Browse files Browse the repository at this point in the history
Add new cache for UIComponents
  • Loading branch information
jonisavo committed May 27, 2022
2 parents 0acc58b + a7b550f commit b510947
Show file tree
Hide file tree
Showing 43 changed files with 532 additions and 56 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Benchmarks

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch: {}

jobs:
benchmark:
name: Run benchmarks on Linux in ${{ matrix.unityVersion }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
unityVersion:
- 2020.3.33f1
- 2021.3.1f1

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/cache@v3
with:
path: Library
key: Library-StandaloneLinux64-${{ matrix.unityVersion }}-${{ hashFiles('Assets/**', 'Packages/**', 'ProjectSettings/**') }}
restore-keys: |
Library-StandaloneLinux64-${{ matrix.unityVersion }}-
Library-StandaloneLinux64-
Library-
- uses: game-ci/unity-test-runner@v2
id: benchmarkRunner
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
with:
unityVersion: ${{ matrix.unityVersion }}
githubToken: ${{ secrets.GITHUB_TOKEN }}
testMode: EditMode
customParameters: -releaseCodeOptimization -assemblyNames "UIComponents.Benchmarks"
coverageOptions: ""

- uses: actions/upload-artifact@v3
if: always()
with:
name: Benchmark results
path: ${{ steps.benchmarkRunner.outputs.artifactsPath }}/editmode-results.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ MonoBehaviour:
m_Address: Assets/Samples/Addressables/Data/AddressablesExampleComponent.uss
m_ReadOnly: 0
m_SerializedLabels: []
- m_GUID: 6e1de5a156244d4dad68fe7b29791ac8
m_Address: Assets/Samples/Addressables/Data/Box.uss
m_ReadOnly: 0
m_SerializedLabels: []
m_ReadOnly: 0
m_Settings: {fileID: 11400000, guid: f579b44f316abdd43a0ccf85ca7cdda4, type: 2}
m_SchemaSet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace UIComponents.Samples.Addressables
[AssetPath("Assets/Samples/Addressables/Data")]
[Layout("AddressablesExampleComponent.uxml")]
[Stylesheet("AddressablesExampleComponent.uss")]
[Stylesheet("Box.uss")]
[Dependency(typeof(IAssetResolver), provide: typeof(AddressableAssetResolver))]
public class AddressablesExampleComponent : UIComponent {}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<UXML xmlns:ui="UnityEngine.UIElements">
<ui:Label name="addressables-example-header" text="AddressablesExampleComponent" />
<ui:Label text="This is an example of a component loaded with Addressables." />
<ui:VisualElement class="white-box">
<ui:Label name="addressables-example-header" text="AddressablesExampleComponent" />
<ui:Label text="This is an example of a component loaded with Addressables." />
</ui:VisualElement>
</UXML>
6 changes: 6 additions & 0 deletions Assets/Samples/Addressables/Data/Box.uss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.white-box {
border-radius: 6px;
border-width: 2px;
border-color: white;
padding: 8px;
}
3 changes: 3 additions & 0 deletions Assets/Samples/Addressables/Data/Box.uss.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/UIComponents.Benchmarks.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/UIComponents.Benchmarks/AssetLoading.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using NUnit.Framework;
using UIComponents.Addressables;
using Unity.PerformanceTesting;

namespace UIComponents.Benchmarks.AssetLoading
{
[TestFixture]
public class AddressablesAssetLoadBenchmarks
{
[AssetPath("Assets/Samples/Addressables/Data")]
[Layout("AddressablesExampleComponent.uxml")]
[Stylesheet("AddressablesExampleComponent.uss")]
[Stylesheet("Box.uss")]
[Dependency(typeof(IAssetResolver), provide: typeof(AddressableAssetResolver))]
private class ComponentWithAssets : UIComponent {}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithWarmCache()
{
BenchmarkUtils.MeasureComponentInitWithWarmCache<ComponentWithAssets>();
}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithColdCache()
{
BenchmarkUtils.MeasureComponentInitWithColdCache<ComponentWithAssets>();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NUnit.Framework;
using UIComponents.Editor;
using Unity.PerformanceTesting;

namespace UIComponents.Benchmarks.AssetLoading
{
public class AssetDatabaseAssetLoadBenchmarks
{
[Layout("AssetDatabaseLayoutFile.uxml")]
[Stylesheet("AssetDatabaseStylesFile.uss")]
[Stylesheet("CommonMargins.uss")]
[AssetPath("Assets/UIComponents.Benchmarks/AssetLoading")]
[Dependency(typeof(IAssetResolver), provide: typeof(AssetDatabaseAssetResolver))]
private class ComponentWithAssets : UIComponent {}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithWarmCache()
{
BenchmarkUtils.MeasureComponentInitWithWarmCache<ComponentWithAssets>();
}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithColdCache()
{
BenchmarkUtils.MeasureComponentInitWithColdCache<ComponentWithAssets>();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<UXML xmlns:ui="UnityEngine.UIElements">
<ui:Label name="assetdatabase-example-header" text="AssetDatabaseExampleComponent" />
<ui:VisualElement class="italic-label-container">
<ui:Label text="This is an example of a component loaded from AssetDatabase." />
<ui:Label text="AssetDatabase is used in the editor." />
</ui:VisualElement>
</UXML>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#assetdatabase-example-header {
font-size: 20px;
-unity-font-style: bold;
white-space: normal;
}

.italic-label-container {
width: 100%;
}

.italic-label-container Label {
white-space: normal;
-unity-font-style: italic;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/UIComponents.Benchmarks/AssetLoading/CommonMargins.uss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.top-margin {
margin-top: 10px;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using Unity.PerformanceTesting;

namespace UIComponents.Benchmarks.AssetLoading
{
public class ResourcesAssetLoadBenchmarks
{
[Layout("Components/ResourcesExampleComponent")]
[Stylesheet("Components/ResourcesExampleComponent.style")]
[Stylesheet("Margins")]
private class ComponentWithAssets : UIComponent {}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithWarmCache()
{
BenchmarkUtils.MeasureComponentInitWithWarmCache<ComponentWithAssets>();
}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithColdCache()
{
BenchmarkUtils.MeasureComponentInitWithColdCache<ComponentWithAssets>();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Assets/UIComponents.Benchmarks/BenchmarkUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Unity.PerformanceTesting;

namespace UIComponents.Benchmarks
{
public static class BenchmarkUtils
{
public const string Version = "0.9.0.0";

private static SampleGroup[] GetProfilerMarkers()
{
return new[]
{
new SampleGroup("UIComponent.DependencySetup", SampleUnit.Microsecond),
new SampleGroup("UIComponent.CacheSetup", SampleUnit.Microsecond),
new SampleGroup("UIComponent.LayoutAndStylesSetup", SampleUnit.Microsecond)
};
}

public static void MeasureComponentInitWithColdCache<TComponent>() where TComponent : UIComponent, new()
{
Measure.Method(() => { new TComponent(); })
.SetUp(() =>
{
UIComponent.ClearCache<TComponent>();
DependencyInjector.RemoveInjector(typeof(TComponent));
})
.SampleGroup(new SampleGroup("Cold Cache Time", SampleUnit.Microsecond))
.ProfilerMarkers(GetProfilerMarkers())
.MeasurementCount(50)
.IterationsPerMeasurement(100)
.GC()
.Run();
}

public static void MeasureComponentInitWithWarmCache<TComponent>() where TComponent : UIComponent, new()
{
Measure.Method(() => { new TComponent(); })
.SampleGroup(new SampleGroup("Warm Cache Time", SampleUnit.Microsecond))
.MeasurementCount(50)
.IterationsPerMeasurement(100)
.ProfilerMarkers(GetProfilerMarkers())
.GC()
.Run();
}
}
}
3 changes: 3 additions & 0 deletions Assets/UIComponents.Benchmarks/BenchmarkUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions Assets/UIComponents.Benchmarks/UIComponentInitBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using NUnit.Framework;
using Unity.PerformanceTesting;

namespace UIComponents.Benchmarks
{
public class UIComponentInitBenchmarks
{
private class EmptyComponent : UIComponent {}

private interface IDependency {}

private class DependencyProvider : IDependency {}

[Dependency(typeof(IDependency), provide: typeof(DependencyProvider))]
private class ComponentWithDependency : UIComponent {}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeEmptyComponentWithWarmCache()
{
BenchmarkUtils.MeasureComponentInitWithWarmCache<EmptyComponent>();
}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeEmptyComponentWithColdCache()
{
BenchmarkUtils.MeasureComponentInitWithColdCache<EmptyComponent>();
}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithDependencyWithWarmCache()
{
BenchmarkUtils.MeasureComponentInitWithWarmCache<ComponentWithDependency>();
}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithDependencyWithColdCache()
{
BenchmarkUtils.MeasureComponentInitWithColdCache<ComponentWithDependency>();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Assets/UIComponents.Benchmarks/UIComponents.Benchmarks.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "UIComponents.Benchmarks",
"rootNamespace": "UIComponents",
"references": [
"Unity.PerformanceTesting",
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"UIComponents",
"UIComponents.Addressables",
"UIComponents.Editor"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b510947

Please sign in to comment.