Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

Commit d5c7f58

Browse files
committed
Added benchmarks to solution file and some other changes.
1 parent 79c007b commit d5c7f58

File tree

10 files changed

+209
-44
lines changed

10 files changed

+209
-44
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,8 @@ paket-files/
258258

259259
# Python Tools for Visual Studio (PTVS)
260260
__pycache__/
261-
*.pyc
261+
*.pyc
262+
263+
# BenchmarkDotNet Atrifacts
264+
**/BenchmarkDotNet.Artifacts/
265+
**/BenchmarkTest.xml

src/XmlAbstraction/XmlAbstraction.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XmlAbstraction", "src\XmlAb
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XmlAbstraction.Test", "test\XmlAbstraction.Test.csproj", "{E4409179-F366-4688-A2B9-EC29A9EBA944}"
99
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XmlAbstraction.Benchmark", "benchmarks\XmlAbstraction.Benchmark.csproj", "{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{E4409179-F366-4688-A2B9-EC29A9EBA944}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{E4409179-F366-4688-A2B9-EC29A9EBA944}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{E4409179-F366-4688-A2B9-EC29A9EBA944}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

src/XmlAbstraction/benchmarks/XmlAbstraction.Benchmark.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp2.2;netcoreapp2.1;netcoreapp2.0;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp2.2;netcoreapp2.1;netcoreapp2.0;net46;net461;net462;net47;net471;net472</TargetFrameworks>
55
<AssemblyName>XmlAbstraction.Benchmark</AssemblyName>
66
<RootNamespace>XmlAbstraction.Benchmark</RootNamespace>
77
<LangVersion>latest</LangVersion>

src/XmlAbstraction/benchmarks/XmlObjectBenchmarks.cs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,88 @@ namespace XmlAbstraction.Benchmark
88

99
[ClrJob(baseline: true), CoreJob, MonoJob, CoreRtJob]
1010
[RPlotExporter, RankColumn]
11+
[InProcess]
1112
public class XmlObjectBenchmarks
1213
{
1314
private XmlObject xmlObj;
14-
private string XmlFile = $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}BenchmarkTest.xml";
15+
private readonly string XmlFile = $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}BenchmarkTest.xml";
1516

1617
[Params(@"<?xml version=""1.0"" encoding=""utf-8"" ?><test></test>", @"<?xml version=""1.0"" encoding=""utf-8"" ?><test><test1 TestAttribute1=""0"">test</test1><test2 TestAttribute1=""0"">test2</test2></test>")]
1718
public string InputXml;
1819

1920
[GlobalSetup]
2021
public void Setup()
2122
{
22-
using (var fstrm = File.Create(XmlFile))
23+
using (var fstrm = File.Create(this.XmlFile))
2324
{
24-
fstrm.Write(Encoding.UTF8.GetBytes(InputXml), 0, InputXml.Length);
25+
fstrm.Write(Encoding.UTF8.GetBytes(this.InputXml), 0, this.InputXml.Length);
2526
}
2627

27-
xmlObj = new XmlObject(XmlFile, InputXml);
28+
this.xmlObj = new XmlObject(this.XmlFile, this.InputXml);
2829
}
2930

3031
[Benchmark]
3132
public void ReopenFile()
32-
=> xmlObj.ReopenFile();
33+
=> this.xmlObj.ReopenFile();
3334

34-
// TODO: Benchmark every method in the XmlAbstraction library.
35+
[Benchmark]
36+
public void AddElement()
37+
=> this.xmlObj.AddElement("test3", "test3");
38+
39+
[Benchmark]
40+
public void AddAttribute()
41+
=> this.xmlObj.AddAttribute("test3", "TestAttribute1", "0");
42+
43+
[Benchmark]
44+
public void Write()
45+
=> this.xmlObj.Write("test4", "test4");
46+
47+
[Benchmark]
48+
public void Write2()
49+
=> this.xmlObj.Write("test4", "TestAttribute1", "0");
50+
51+
[Benchmark]
52+
public void Write3()
53+
=> this.xmlObj.Write("test5", "testholder", new string[] { "test1", "test2", "test3", "test4" });
54+
55+
[Benchmark]
56+
public void Read()
57+
=> this.xmlObj.Read("test3");
58+
59+
[Benchmark]
60+
public void Read2()
61+
=> this.xmlObj.Read("test3", "TestAttribute1");
62+
63+
[Benchmark]
64+
public void Read3()
65+
=> this.xmlObj.Read("test5", "testholder", null);
66+
67+
[Benchmark]
68+
public void TryRead()
69+
=> this.xmlObj.TryRead("test6");
70+
71+
[Benchmark]
72+
public void TryRead2()
73+
=> this.xmlObj.TryRead("test7", "TestAttribute1");
74+
75+
[Benchmark]
76+
public void TryRead3()
77+
=> this.xmlObj.TryRead("test8", "testholder", null);
78+
79+
[Benchmark]
80+
public void Delete()
81+
=> this.xmlObj.Delete("test6");
82+
83+
[Benchmark]
84+
public void Delete2()
85+
=> this.xmlObj.Delete("test7", "TestAttribute1");
3586

3687
[Benchmark]
3788
public void Save()
38-
=> xmlObj.Save();
89+
=> this.xmlObj.Save();
90+
91+
[GlobalCleanup]
92+
public void Cleanup()
93+
=> File.Delete(this.XmlFile);
3994
}
4095
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2018-2019, AraHaan
2+
// https://github.com/AraHaan/
3+
// All rights reserved.
4+
// license: MIT, see LICENSE for more details.
5+
6+
namespace XmlAbstraction
7+
{
8+
using System.IO;
9+
using System.Xml.Linq;
10+
11+
/// <summary>
12+
/// A hack class to bridge the gaps in <see cref="XDocument"/> for .NET Standard pre 2.0.
13+
/// </summary>
14+
internal static class XDocumentExtensions
15+
{
16+
/// <summary>
17+
/// Serialize this <see cref="XDocument"/> to a file, overwriting an existing file,
18+
/// if it exists.
19+
/// </summary>
20+
/// <param name="xdoc">The <see cref="XDocument"/> for which to save.</param>
21+
/// <param name="fileName">
22+
/// A string that contains the name of the file.
23+
/// </param>
24+
internal static void Save(this XDocument xdoc, string fileName)
25+
{
26+
using (var fstream = File.Create(fileName))
27+
{
28+
xdoc.Save(fstream);
29+
}
30+
}
31+
}
32+
}

src/XmlAbstraction/src/XmlAbstraction.csproj

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp2.2;netcoreapp2.1;netcoreapp2.0;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks>
4+
<TargetFrameworks>netstandard1.3;netstandard1.4;netstandard1.5;netstandard1.6;netstandard2.0;netcoreapp1.0;netcoreapp1.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<RootNamespace>XmlAbstraction</RootNamespace>
77
<AssemblyName>XmlAbstraction</AssemblyName>
@@ -17,17 +17,42 @@
1717
<RepositoryUrl>https://github.com/AraHaan/XmlAbstraction/</RepositoryUrl>
1818
<PackageProjectUrl>https://github.com/AraHaan/XmlAbstraction/</PackageProjectUrl>
1919
<PackageLicenseUrl>https://github.com/AraHaan/XmlAbstraction/blob/master/LICENSE</PackageLicenseUrl>
20-
<PackageReleaseNotes>Added reference assemblies, Benchmarks and other fixes.</PackageReleaseNotes>
21-
<PublishDocumentationFile>true</PublishDocumentationFile>
20+
<PackageReleaseNotes>Added reference assemblies, Benchmarks and other changes.</PackageReleaseNotes>
21+
<PublishDocumentationFile>false</PublishDocumentationFile>
2222
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
2323
</PropertyGroup>
2424

25+
<ItemGroup>
26+
<None Remove="stylecop.json" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<AdditionalFiles Include="stylecop.json" />
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<!-- Add reference binaries. -->
35+
<TfmSpecificPackageFile Include="$(OutputPath)/ref/">
36+
<PackagePath>ref/$(TargetFramework)</PackagePath>
37+
</TfmSpecificPackageFile>
38+
<!-- Add documentation file to each of the reference assembly paths. -->
39+
<TfmSpecificPackageFile Include="$(OutputPath)/$(ProjectName).xml">
40+
<PackagePath>ref/$(TargetFramework)</PackagePath>
41+
</TfmSpecificPackageFile>
42+
<!-- Add debugging symbols. -->
43+
<TfmSpecificPackageFile Include="$(OutputPath)/$(ProjectName).pdb">
44+
<PackagePath>lib/$(TargetFramework)</PackagePath>
45+
</TfmSpecificPackageFile>
46+
</ItemGroup>
47+
2548
<!-- Conditionally obtain references for the .NET Framework targets. -->
26-
<ItemGroup Condition="'$(TargetFramework)' == 'net40' OR '$(TargetFramework)' == 'net45' OR '$(TargetFramework)' == 'net451' OR '$(TargetFramework)' == 'net452' OR '$(TargetFramework)' == 'net46' OR '$(TargetFramework)' == 'net461' OR '$(TargetFramework)' == 'net462' OR '$(TargetFramework)' == 'net47' OR '$(TargetFramework)' == 'net471' OR '$(TargetFramework)' == 'net472'">
49+
<!--
50+
<ItemGroup Condition="$(TargetFramework.StartsWith('net4'))">
2751
<Reference Include="System" />
2852
<Reference Include="System.Xml.Linq" />
2953
<Reference Include="System.Xml" />
3054
</ItemGroup>
55+
-->
3156

3257
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
3358
<DefineConstants>DEBUG;TRACE</DefineConstants>
@@ -41,7 +66,7 @@
4166

4267
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
4368
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
44-
<WarningsAsErrors />
69+
<WarningsAsErrors>NU1605</WarningsAsErrors>
4570
<OutputPath>bin\Any CPU\Release\</OutputPath>
4671
<ErrorReport>send</ErrorReport>
4772
<DebugType>full</DebugType>
@@ -65,6 +90,15 @@
6590
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
6691
<PrivateAssets>all</PrivateAssets>
6792
</PackageReference>
93+
<PackageReference Include="System.Collections" Condition="'$(TargetFramework)' != 'net40'">
94+
<Version>4.3.0</Version>
95+
</PackageReference>
96+
<PackageReference Include="System.Xml.XDocument" Condition="'$(TargetFramework)' != 'net40'">
97+
<Version>4.3.0</Version>
98+
</PackageReference>
99+
<PackageReference Include="System.IO.FileSystem" Condition="!$(TargetFramework.StartsWith('net4'))">
100+
<Version>4.3.0</Version>
101+
</PackageReference>
68102
</ItemGroup>
69-
103+
70104
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2018-2019, AraHaan
2+
// https://github.com/AraHaan/
3+
// All rights reserved.
4+
// license: MIT, see LICENSE for more details.
5+
6+
namespace XmlAbstraction
7+
{
8+
internal class XmlAttributeData
9+
{
10+
internal string AttributeName { get; set; } = string.Empty;
11+
12+
internal string Value { get; set; } = string.Empty;
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2018-2019, AraHaan
2+
// https://github.com/AraHaan/
3+
// All rights reserved.
4+
// license: MIT, see LICENSE for more details.
5+
6+
namespace XmlAbstraction
7+
{
8+
using System.Collections.Generic;
9+
10+
internal class XmlElementData
11+
{
12+
internal string Name { get; set; } = string.Empty;
13+
14+
internal List<XmlElementData> Subelements { get; set; } = null;
15+
16+
internal List<XmlAttributeData> Attributes { get; set; } = new List<XmlAttributeData>();
17+
18+
internal string Value { get; set; } = string.Empty;
19+
}
20+
}

src/XmlAbstraction/src/XmlObject.cs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// <copyright file="XmlObject.cs" company="PlaceholderCompany">
2-
// Copyright (c) PlaceholderCompany. All rights reserved.
3-
// </copyright>
1+
// Copyright (c) 2018-2019, AraHaan
2+
// https://github.com/AraHaan/
3+
// All rights reserved.
4+
// license: MIT, see LICENSE for more details.
45

56
namespace XmlAbstraction
67
{
@@ -65,8 +66,8 @@ public XmlObject(string xmlfilename, string fallbackxmlcontent)
6566
/// <param name="saveToCurrentDirectory">
6667
/// Controls weather to save the file to the xmlfilename param string if
6768
/// it is the full path or to the Current Directory if it supplies file name only.
68-
/// This implies that that file is saved to <see cref="Environment.CurrentDirectory"/> +
69-
/// <see cref="Path.DirectorySeparatorChar"/> prefixed before the filename.
69+
/// This implies that that file is saved to the fully qualified path of the
70+
/// current working directory prefixed before the filename.
7071
/// </param>
7172
public XmlObject(string xmlfilename, string fallbackxmlcontent, bool saveToCurrentDirectory)
7273
{
@@ -83,10 +84,10 @@ public XmlObject(string xmlfilename, string fallbackxmlcontent, bool saveToCurre
8384
throw new DirectoryNotFoundException("Directory in filename was not found.");
8485
}
8586

86-
if (!xmlfilename.Contains(Environment.CurrentDirectory) &&
87-
directory.Parent.FullName == Environment.CurrentDirectory)
87+
if (!xmlfilename.Contains(Directory.GetCurrentDirectory()) &&
88+
directory.Parent.FullName == Directory.GetCurrentDirectory())
8889
{
89-
xmlfilename = Environment.CurrentDirectory + Path.DirectorySeparatorChar + xmlfilename;
90+
xmlfilename = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + xmlfilename;
9091
}
9192
}
9293

@@ -202,7 +203,7 @@ public void ReopenFile()
202203
/// </summary>
203204
/// <param name="elementname">The name of the element to create.</param>
204205
/// <param name="value">The value for the element.</param>
205-
/// <exception cref="System.Exception">
206+
/// <exception cref="Exception">
206207
/// Thrown if the element already exists in the <see cref="XmlObject"/>.
207208
/// </exception>
208209
public void AddElement(string elementname, string value)
@@ -921,23 +922,5 @@ private void SaveAddedSubelements(XElement xElement, XmlElementData elemdata)
921922
}
922923
}
923924
}
924-
925-
private class XmlAttributeData
926-
{
927-
internal string AttributeName { get; set; } = string.Empty;
928-
929-
internal string Value { get; set; } = string.Empty;
930-
}
931-
932-
private class XmlElementData
933-
{
934-
internal string Name { get; set; } = string.Empty;
935-
936-
internal List<XmlElementData> Subelements { get; set; } = null;
937-
938-
internal List<XmlAttributeData> Attributes { get; set; } = new List<XmlAttributeData>();
939-
940-
internal string Value { get; set; } = string.Empty;
941-
}
942925
}
943926
}

src/XmlAbstraction/src/stylecop.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// ACTION REQUIRED: This file was automatically added to your project, but it
3+
// will not take effect until additional steps are taken to enable it. See the
4+
// following page for additional information:
5+
//
6+
// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md
7+
8+
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
9+
"settings": {
10+
"documentationRules": {
11+
"companyName": "AraHaan",
12+
"copyrightText": "Copyright (c) 2018-2019, {companyName}\nhttps://github.com/AraHaan/\nAll rights reserved.\nlicense: MIT, see LICENSE for more details.",
13+
"documentInternalElements": false,
14+
"xmlHeader": false
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)