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

Commit

Permalink
Added benchmarks to solution file and some other changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
AraHaan committed Feb 4, 2019
1 parent 79c007b commit d5c7f58
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 44 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,8 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc

# BenchmarkDotNet Atrifacts
**/BenchmarkDotNet.Artifacts/
**/BenchmarkTest.xml
6 changes: 6 additions & 0 deletions src/XmlAbstraction/XmlAbstraction.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XmlAbstraction", "src\XmlAb
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XmlAbstraction.Test", "test\XmlAbstraction.Test.csproj", "{E4409179-F366-4688-A2B9-EC29A9EBA944}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XmlAbstraction.Benchmark", "benchmarks\XmlAbstraction.Benchmark.csproj", "{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{E4409179-F366-4688-A2B9-EC29A9EBA944}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4409179-F366-4688-A2B9-EC29A9EBA944}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4409179-F366-4688-A2B9-EC29A9EBA944}.Release|Any CPU.Build.0 = Release|Any CPU
{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E4332B7-1DB2-4CE7-BB89-5634D7D3553D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 2 additions & 2 deletions src/XmlAbstraction/benchmarks/XmlAbstraction.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp2.2;netcoreapp2.1;netcoreapp2.0;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp2.2;netcoreapp2.1;netcoreapp2.0;net46;net461;net462;net47;net471;net472</TargetFrameworks>
<AssemblyName>XmlAbstraction.Benchmark</AssemblyName>
<RootNamespace>XmlAbstraction.Benchmark</RootNamespace>
<LangVersion>latest</LangVersion>
Expand Down
69 changes: 62 additions & 7 deletions src/XmlAbstraction/benchmarks/XmlObjectBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,88 @@ namespace XmlAbstraction.Benchmark

[ClrJob(baseline: true), CoreJob, MonoJob, CoreRtJob]
[RPlotExporter, RankColumn]
[InProcess]
public class XmlObjectBenchmarks
{
private XmlObject xmlObj;
private string XmlFile = $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}BenchmarkTest.xml";
private readonly string XmlFile = $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}BenchmarkTest.xml";

[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>")]
public string InputXml;

[GlobalSetup]
public void Setup()
{
using (var fstrm = File.Create(XmlFile))
using (var fstrm = File.Create(this.XmlFile))
{
fstrm.Write(Encoding.UTF8.GetBytes(InputXml), 0, InputXml.Length);
fstrm.Write(Encoding.UTF8.GetBytes(this.InputXml), 0, this.InputXml.Length);
}

xmlObj = new XmlObject(XmlFile, InputXml);
this.xmlObj = new XmlObject(this.XmlFile, this.InputXml);
}

[Benchmark]
public void ReopenFile()
=> xmlObj.ReopenFile();
=> this.xmlObj.ReopenFile();

// TODO: Benchmark every method in the XmlAbstraction library.
[Benchmark]
public void AddElement()
=> this.xmlObj.AddElement("test3", "test3");

[Benchmark]
public void AddAttribute()
=> this.xmlObj.AddAttribute("test3", "TestAttribute1", "0");

[Benchmark]
public void Write()
=> this.xmlObj.Write("test4", "test4");

[Benchmark]
public void Write2()
=> this.xmlObj.Write("test4", "TestAttribute1", "0");

[Benchmark]
public void Write3()
=> this.xmlObj.Write("test5", "testholder", new string[] { "test1", "test2", "test3", "test4" });

[Benchmark]
public void Read()
=> this.xmlObj.Read("test3");

[Benchmark]
public void Read2()
=> this.xmlObj.Read("test3", "TestAttribute1");

[Benchmark]
public void Read3()
=> this.xmlObj.Read("test5", "testholder", null);

[Benchmark]
public void TryRead()
=> this.xmlObj.TryRead("test6");

[Benchmark]
public void TryRead2()
=> this.xmlObj.TryRead("test7", "TestAttribute1");

[Benchmark]
public void TryRead3()
=> this.xmlObj.TryRead("test8", "testholder", null);

[Benchmark]
public void Delete()
=> this.xmlObj.Delete("test6");

[Benchmark]
public void Delete2()
=> this.xmlObj.Delete("test7", "TestAttribute1");

[Benchmark]
public void Save()
=> xmlObj.Save();
=> this.xmlObj.Save();

[GlobalCleanup]
public void Cleanup()
=> File.Delete(this.XmlFile);
}
}
32 changes: 32 additions & 0 deletions src/XmlAbstraction/src/XDocumentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2018-2019, AraHaan
// https://github.com/AraHaan/
// All rights reserved.
// license: MIT, see LICENSE for more details.

namespace XmlAbstraction
{
using System.IO;
using System.Xml.Linq;

/// <summary>
/// A hack class to bridge the gaps in <see cref="XDocument"/> for .NET Standard pre 2.0.
/// </summary>
internal static class XDocumentExtensions
{
/// <summary>
/// Serialize this <see cref="XDocument"/> to a file, overwriting an existing file,
/// if it exists.
/// </summary>
/// <param name="xdoc">The <see cref="XDocument"/> for which to save.</param>
/// <param name="fileName">
/// A string that contains the name of the file.
/// </param>
internal static void Save(this XDocument xdoc, string fileName)
{
using (var fstream = File.Create(fileName))
{
xdoc.Save(fstream);
}
}
}
}
48 changes: 41 additions & 7 deletions src/XmlAbstraction/src/XmlAbstraction.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp2.2;netcoreapp2.1;netcoreapp2.0;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks>
<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>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>XmlAbstraction</RootNamespace>
<AssemblyName>XmlAbstraction</AssemblyName>
Expand All @@ -17,17 +17,42 @@
<RepositoryUrl>https://github.com/AraHaan/XmlAbstraction/</RepositoryUrl>
<PackageProjectUrl>https://github.com/AraHaan/XmlAbstraction/</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/AraHaan/XmlAbstraction/blob/master/LICENSE</PackageLicenseUrl>
<PackageReleaseNotes>Added reference assemblies, Benchmarks and other fixes.</PackageReleaseNotes>
<PublishDocumentationFile>true</PublishDocumentationFile>
<PackageReleaseNotes>Added reference assemblies, Benchmarks and other changes.</PackageReleaseNotes>
<PublishDocumentationFile>false</PublishDocumentationFile>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
</PropertyGroup>

<ItemGroup>
<None Remove="stylecop.json" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>

<ItemGroup>
<!-- Add reference binaries. -->
<TfmSpecificPackageFile Include="$(OutputPath)/ref/">
<PackagePath>ref/$(TargetFramework)</PackagePath>
</TfmSpecificPackageFile>
<!-- Add documentation file to each of the reference assembly paths. -->
<TfmSpecificPackageFile Include="$(OutputPath)/$(ProjectName).xml">
<PackagePath>ref/$(TargetFramework)</PackagePath>
</TfmSpecificPackageFile>
<!-- Add debugging symbols. -->
<TfmSpecificPackageFile Include="$(OutputPath)/$(ProjectName).pdb">
<PackagePath>lib/$(TargetFramework)</PackagePath>
</TfmSpecificPackageFile>
</ItemGroup>

<!-- Conditionally obtain references for the .NET Framework targets. -->
<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'">
<!--
<ItemGroup Condition="$(TargetFramework.StartsWith('net4'))">
<Reference Include="System" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml" />
</ItemGroup>
-->

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

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
<WarningsAsErrors>NU1605</WarningsAsErrors>
<OutputPath>bin\Any CPU\Release\</OutputPath>
<ErrorReport>send</ErrorReport>
<DebugType>full</DebugType>
Expand All @@ -65,6 +90,15 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Collections" Condition="'$(TargetFramework)' != 'net40'">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="System.Xml.XDocument" Condition="'$(TargetFramework)' != 'net40'">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="System.IO.FileSystem" Condition="!$(TargetFramework.StartsWith('net4'))">
<Version>4.3.0</Version>
</PackageReference>
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions src/XmlAbstraction/src/XmlAttributeData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2018-2019, AraHaan
// https://github.com/AraHaan/
// All rights reserved.
// license: MIT, see LICENSE for more details.

namespace XmlAbstraction
{
internal class XmlAttributeData
{
internal string AttributeName { get; set; } = string.Empty;

internal string Value { get; set; } = string.Empty;
}
}
20 changes: 20 additions & 0 deletions src/XmlAbstraction/src/XmlElementData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2018-2019, AraHaan
// https://github.com/AraHaan/
// All rights reserved.
// license: MIT, see LICENSE for more details.

namespace XmlAbstraction
{
using System.Collections.Generic;

internal class XmlElementData
{
internal string Name { get; set; } = string.Empty;

internal List<XmlElementData> Subelements { get; set; } = null;

internal List<XmlAttributeData> Attributes { get; set; } = new List<XmlAttributeData>();

internal string Value { get; set; } = string.Empty;
}
}
37 changes: 10 additions & 27 deletions src/XmlAbstraction/src/XmlObject.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// <copyright file="XmlObject.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
// Copyright (c) 2018-2019, AraHaan
// https://github.com/AraHaan/
// All rights reserved.
// license: MIT, see LICENSE for more details.

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

if (!xmlfilename.Contains(Environment.CurrentDirectory) &&
directory.Parent.FullName == Environment.CurrentDirectory)
if (!xmlfilename.Contains(Directory.GetCurrentDirectory()) &&
directory.Parent.FullName == Directory.GetCurrentDirectory())
{
xmlfilename = Environment.CurrentDirectory + Path.DirectorySeparatorChar + xmlfilename;
xmlfilename = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + xmlfilename;
}
}

Expand Down Expand Up @@ -202,7 +203,7 @@ public void ReopenFile()
/// </summary>
/// <param name="elementname">The name of the element to create.</param>
/// <param name="value">The value for the element.</param>
/// <exception cref="System.Exception">
/// <exception cref="Exception">
/// Thrown if the element already exists in the <see cref="XmlObject"/>.
/// </exception>
public void AddElement(string elementname, string value)
Expand Down Expand Up @@ -921,23 +922,5 @@ private void SaveAddedSubelements(XElement xElement, XmlElementData elemdata)
}
}
}

private class XmlAttributeData
{
internal string AttributeName { get; set; } = string.Empty;

internal string Value { get; set; } = string.Empty;
}

private class XmlElementData
{
internal string Name { get; set; } = string.Empty;

internal List<XmlElementData> Subelements { get; set; } = null;

internal List<XmlAttributeData> Attributes { get; set; } = new List<XmlAttributeData>();

internal string Value { get; set; } = string.Empty;
}
}
}
17 changes: 17 additions & 0 deletions src/XmlAbstraction/src/stylecop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// ACTION REQUIRED: This file was automatically added to your project, but it
// will not take effect until additional steps are taken to enable it. See the
// following page for additional information:
//
// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md

"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
"settings": {
"documentationRules": {
"companyName": "AraHaan",
"copyrightText": "Copyright (c) 2018-2019, {companyName}\nhttps://github.com/AraHaan/\nAll rights reserved.\nlicense: MIT, see LICENSE for more details.",
"documentInternalElements": false,
"xmlHeader": false
}
}
}

0 comments on commit d5c7f58

Please sign in to comment.