Skip to content

Commit

Permalink
Add tests for checking size of objects (#1459)
Browse files Browse the repository at this point in the history
  • Loading branch information
twsouthwick authored Jun 27, 2023
1 parent dfd0e25 commit 9f3ddc0
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
shell: pwsh
run: cd test/DocumentFormat.OpenXml.Benchmarks; ls -recurse
- name: Store Validation
uses: rhysd/github-action-benchmark@v1
uses: benchmark-action/github-action-benchmark@v1
with:
name: Validation
tool: 'benchmarkdotnet'
Expand All @@ -41,7 +41,7 @@ jobs:
fail-on-alert: true
alert-comment-cc-users: '@twsouthwick'
- name: Store CompiledParticle
uses: rhysd/github-action-benchmark@v1
uses: benchmark-action/github-action-benchmark@v1
with:
name: CompiledParticle
tool: 'benchmarkdotnet'
Expand All @@ -54,7 +54,7 @@ jobs:
fail-on-alert: true
alert-comment-cc-users: '@twsouthwick'
- name: Store Documents
uses: rhysd/github-action-benchmark@v1
uses: benchmark-action/github-action-benchmark@v1
with:
name: Documents
tool: 'benchmarkdotnet'
Expand All @@ -67,7 +67,7 @@ jobs:
fail-on-alert: true
alert-comment-cc-users: '@twsouthwick'
- name: Store ElementMetadataTests
uses: rhysd/github-action-benchmark@v1
uses: benchmark-action/github-action-benchmark@v1
with:
name: ElementMetadataTests
tool: 'benchmarkdotnet'
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,6 @@ msbuild.binlog
/.idea/

Microsoft.Interop.JavaScript.JSImportGenerator/
Microsoft.Interop.LibraryImportGenerator/
Microsoft.Interop.LibraryImportGenerator/

BenchmarkDotNet.Artifacts/
7 changes: 6 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<ProductTargetFrameworks>netstandard2.0;net35;net40</ProductTargetFrameworks>
<FrameworkTargetFrameworks>netstandard2.0;net6.0;net35;net40;net46</FrameworkTargetFrameworks>
<AssetsTargetFrameworks>net452;netstandard2.0</AssetsTargetFrameworks>
<TestTargetFrameworks>net452;net46;net6.0;net7.0</TestTargetFrameworks>
<TestTargetFrameworks>net452;net462;net6.0;net7.0</TestTargetFrameworks>
<LatestTargetFramework>net7.0</LatestTargetFramework>
<SamplesFrameworks>net472;net7.0</SamplesFrameworks>
</PropertyGroup>
Expand All @@ -108,6 +108,11 @@
</Otherwise>
</Choose>

<PropertyGroup>
<SupportsNetStandard>true</SupportsNetStandard>
<SupportsNetStandard Condition=" '$(TargetFramework)' == 'net35' OR '$(TargetFramework)' == 'net40' OR '$(TargetFramework)' == 'net452' OR '$(TargetFramework)' == 'net46' ">false</SupportsNetStandard>
</PropertyGroup>

<Target Name="UnknownProjectLoadStyle" BeforeTargets="Build" Condition="$(__InvalidProjectLoadStyle)">
<Warning Text="Unknown ProjectLoadStyle '$(ProjectLoadStyle)' set. Please verify settings in Directory.Build.props and environment variables to a known value. For now, default values will be used." />
</Target>
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageVersion Include="NuGet.Packaging.Core" Version="6.4.0" />
<PackageVersion Include="NuGet.Protocol" Version="6.4.0" />
<PackageVersion Include="NuGet.Resolver" Version="6.4.0" />
<PackageVersion Include="ObjectLayoutInspector" Version="0.1.4" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.507" />
<PackageVersion Include="Svg" Version="3.4.4" />
<PackageVersion Include="System.CodeDom" Version="7.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/DocumentFormat.OpenXml.Framework/OpenXmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2616,7 +2616,7 @@ internal void RemoveAttributesBasedonMC()

[DebuggerDisplay("Count = {GetCount()}")]
[DebuggerTypeProxy(typeof(FeatureCollectionDebugView))]
private protected partial class ElementFeatureCollection : IFeatureCollection
internal class ElementFeatureCollection : IFeatureCollection
{
private readonly OpenXmlElement _owner;

Expand Down
39 changes: 39 additions & 0 deletions test/DocumentFormat.OpenXml.Benchmarks/ElementTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using BenchmarkDotNet.Attributes;
using System.Xml;

namespace DocumentFormat.OpenXml.Benchmarks;

public class ElementTests
{
[Benchmark]
public object Construction()
{
var element = new TestElement();

// Force features to be created
return element.Features;
}

private class TestElement : OpenXmlElement
{
public override bool HasChildren => throw new System.NotImplementedException();

public override void RemoveAllChildren()
{
throw new System.NotImplementedException();
}

internal override void WriteContentTo(XmlWriter w)
{
throw new System.NotImplementedException();
}

private protected override void Populate(XmlReader xmlReader, OpenXmlLoadMode loadMode)
{
throw new System.NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<EmbeddedResource Include="ElementChildren.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Condition=" '$(SupportsNetStandard)' == 'true' " Include="ObjectLayoutInspector" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DocumentFormat.OpenXml.Framework\DocumentFormat.OpenXml.Framework.csproj" />
<ProjectReference Include="..\..\src\DocumentFormat.OpenXml\DocumentFormat.OpenXml.csproj" />
Expand Down
141 changes: 141 additions & 0 deletions test/DocumentFormat.OpenXml.Framework.Tests/ObjectSizeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if NET462 || NET

using DocumentFormat.OpenXml.Packaging;
using ObjectLayoutInspector;
using System;
using System.Collections.Generic;
using System.Xml;
using Xunit;
using Xunit.Abstractions;

namespace DocumentFormat.OpenXml.Framework.Tests;

public class ObjectSizeTests
{
private readonly ITestOutputHelper _output;

public ObjectSizeTests(ITestOutputHelper output)
{
_output = output;
}

[InlineData(typeof(TestOpenXmlElement), 64, 80, 0, 6)]
[InlineData(typeof(TestOpenXmlElement.FeatureCollection), 24, 40, 0, 3)]
[InlineData(typeof(TestOpenXmlPackage), 24, 40, 3, 5)]
[InlineData(typeof(TestOpenXmlPackage.FeatureCollection), 96, 112, 0, 11)]
[InlineData(typeof(TestOpenXmlPart), 32, 48, 7, 5)]
[InlineData(typeof(TestOpenXmlPart.FeatureCollection), 48, 64, 0, 5)]
[InlineData(typeof(TestOpenXmlPartContainer), 8, 24, 0, 1)]
[InlineData(typeof(TestOpenXmlCompositeElement), 72, 88, 0, 7)]
[InlineData(typeof(TestOpenXmlLeafElement), 72, 88, 0, 7)]
[InlineData(typeof(TestOpenXmlLeafTextElement), 80, 96, 0, 8)]
[InlineData(typeof(OpenXmlUnknownElement), 104, 120, 0, 11)]
[Theory]
public void VerifySize(Type type, int size, int fullSize, int padding, int fieldCount)
{
// Arrange
var layout = TypeLayout.GetLayout(type);

_output.WriteLine($"typeof({GetTypeName(type)}), {layout.Size}, {layout.FullSize}, {layout.Paddings}, {layout.Fields.Length}");
_output.WriteLine(layout.ToString());

// Assert
Assert.Equal(size, layout.Size);
Assert.Equal(fullSize, layout.FullSize);
Assert.Equal(padding, layout.Paddings);
Assert.Equal(fieldCount, layout.Fields.Length);
}

private static string GetTypeName(Type type)
{
return type.FullName.Substring(type.FullName.IndexOf('+') + 1).Replace('+', '.');
}

private sealed class TestOpenXmlElement : OpenXmlElement
{
public override bool HasChildren => throw new NotImplementedException();

public override void RemoveAllChildren()
{
throw new NotImplementedException();
}

internal override void WriteContentTo(XmlWriter w)
{
throw new NotImplementedException();
}

private protected override void Populate(XmlReader xmlReader, OpenXmlLoadMode loadMode)
{
throw new NotImplementedException();
}

internal class FeatureCollection : ElementFeatureCollection
{
public FeatureCollection()
: base(null!)
{
}
}
}

private sealed class TestOpenXmlPackage : OpenXmlPackage
{
internal sealed class FeatureCollection : PackageFeatureCollection
{
public FeatureCollection()
: base(null!)
{
}
}
}

private sealed class TestOpenXmlPart : OpenXmlPart
{
public override string RelationshipType => throw new NotImplementedException();

internal sealed class FeatureCollection : PartFeatureCollection
{
public FeatureCollection()
: base(null!)
{
}
}
}

private sealed class TestOpenXmlPartContainer : OpenXmlPartContainer
{
internal override OpenXmlPackage InternalOpenXmlPackage => throw new NotImplementedException();

internal override OpenXmlPart ThisOpenXmlPart => throw new NotImplementedException();

internal override IRelationshipCollection Relationships => throw new NotImplementedException();

protected override void ThrowIfObjectDisposed()
{
throw new NotImplementedException();
}

internal override void FindAllReachableParts(IDictionary<OpenXmlPart, bool> reachableParts)
{
throw new NotImplementedException();
}
}

private sealed class TestOpenXmlCompositeElement : OpenXmlCompositeElement
{
}

private sealed class TestOpenXmlLeafElement : OpenXmlLeafElement
{
}

private sealed class TestOpenXmlLeafTextElement : OpenXmlLeafTextElement
{
}
}

#endif

1 comment on commit 9f3ddc0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Documents'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 9f3ddc0 Previous: a8427d9 Ratio
DocumentFormat.OpenXml.Benchmarks.Documents.ReadFile 2563955.672286184 ns (± 1057006.7562881988) 1312580.2176339286 ns (± 35492.45396060977) 1.95

This comment was automatically generated by workflow using github-action-benchmark.

CC: @twsouthwick

Please sign in to comment.