-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for checking size of objects (#1459)
- Loading branch information
1 parent
dfd0e25
commit 9f3ddc0
Showing
8 changed files
with
199 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
test/DocumentFormat.OpenXml.Framework.Tests/ObjectSizeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9f3ddc0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark 'Documents'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
1.50
.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