Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add basic serialize/deserialize benchmarks
  • Loading branch information
Turnerj committed Dec 12, 2019
commit 48991b8cb3e89b41894c472369806f34f6ffd431
82 changes: 82 additions & 0 deletions Benchmarks/Schema.NET.Benchmarks/Core/BookBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
namespace Schema.NET.Benchmarks.Core
{
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;

public class BookBenchmark : SchemaBenchmarkBase
{
[GlobalSetup]
public void Setup() => this.ConfigureBenchmark(new Book()
{
Id = new Uri("http://example.com/book/1"),
Name = "The Catcher in the Rye",
Author = new Person()
{
Name = "J.D. Salinger"
},
Url = new Uri("http://www.barnesandnoble.com/store/info/offer/JDSalinger"),
WorkExample = new List<ICreativeWork>()
{
new Book()
{
Isbn = "031676948",
BookEdition = "2nd Edition",
BookFormat = BookFormatType.Hardcover,
PotentialAction = new ReadAction()
{
Target = new EntryPoint()
{
UrlTemplate = "http://www.barnesandnoble.com/store/info/offer/0316769487?purchase=true",
ActionPlatform = new List<Uri>()
{
new Uri("https://schema.org/DesktopWebPlatform"),
new Uri("https://schema.org/IOSPlatform"),
new Uri("https://schema.org/AndroidPlatform")
}
},
ExpectsAcceptanceOf = new Offer()
{
Price = 6.99M,
PriceCurrency = "USD",
EligibleRegion = new Country()
{
Name = "US"
},
Availability = ItemAvailability.InStock
}
},
},
new Book()
{
Isbn = "031676947",
BookEdition = "1st Edition",
BookFormat = BookFormatType.EBook,
PotentialAction = new ReadAction()
{
Target = new EntryPoint()
{
UrlTemplate = "http://www.barnesandnoble.com/store/info/offer/031676947?purchase=true",
ActionPlatform = new List<Uri>()
{
new Uri("https://schema.org/DesktopWebPlatform"),
new Uri("https://schema.org/IOSPlatform"),
new Uri("https://schema.org/AndroidPlatform")
}
},
ExpectsAcceptanceOf = new Offer()
{
Price = 1.99M,
PriceCurrency = "USD",
EligibleRegion = new Country()
{
Name = "UK"
},
Availability = ItemAvailability.InStock
}
},
}
}
});
}
}
19 changes: 19 additions & 0 deletions Benchmarks/Schema.NET.Benchmarks/Core/WebsiteBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Schema.NET.Benchmarks.Core
{
using System;
using BenchmarkDotNet.Attributes;

public class WebsiteBenchmark : SchemaBenchmarkBase
{
[GlobalSetup]
public void Setup() => this.ConfigureBenchmark(new WebSite()
{
PotentialAction = new SearchAction()
{
Target = new Uri("http://example.com/search?&q={query}"),
QueryInput = "required"
},
Url = new Uri("https://example.com")
});
}
}
10 changes: 10 additions & 0 deletions Benchmarks/Schema.NET.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Schema.NET.Benchmarks
{
using System;
using BenchmarkDotNet.Running;

internal class Program
{
private static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
25 changes: 25 additions & 0 deletions Benchmarks/Schema.NET.Benchmarks/Schema.NET.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Label="Build">
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
<CodeAnalysisRuleSet>../../MinimumRecommendedRulesWithStyleCop.ruleset</CodeAnalysisRuleSet>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup Label="Project References">
<ProjectReference Include="..\..\Source\Schema.NET\Schema.NET.csproj" />
</ItemGroup>

<ItemGroup Label="Package References">
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
</ItemGroup>

<ItemGroup Label="Analyzer Package References">
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" PrivateAssets="All" Version="16.4.16" />
<PackageReference Include="StyleCop.Analyzers" PrivateAssets="All" Version="1.1.118" />
</ItemGroup>


</Project>
31 changes: 31 additions & 0 deletions Benchmarks/Schema.NET.Benchmarks/SchemaBenchmarkBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Schema.NET.Benchmarks
{
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Newtonsoft.Json;

[SimpleJob(RuntimeMoniker.NetCoreApp30)]
Copy link
Owner

Choose a reason for hiding this comment

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

There are a few more useful atrributes that could be added here to generate some graphs and more stats. See https://github.com/Dotnet-Boxed/Framework/blob/master/Benchmarks/Boxed.Mapping.Benchmark/MapListBenchmark.cs

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, happy to add those. Do you think it is worth benchmarking multiple versions of .NET like your linked example or just leave it still as .NET Core 3?

Copy link
Owner

Choose a reason for hiding this comment

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

.NET Framework is still used a fair bit, so I added it. The results can be very different.

[MemoryDiagnoser]
public abstract class SchemaBenchmarkBase
{
protected Thing Thing { get; set; }

private Type ThingType { get; set; }

private string SerializedThing { get; set; }

[Benchmark]
public string Serialize() => this.Thing.ToString();

[Benchmark]
public object Deserialize() => JsonConvert.DeserializeObject(this.SerializedThing, this.ThingType);

protected void ConfigureBenchmark(Thing thing)
{
this.Thing = thing;
this.ThingType = this.Thing.GetType();
this.SerializedThing = this.Thing.ToString();
}
}
}
9 changes: 9 additions & 0 deletions Schema.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{0555
.github\SECURITY.md = .github\SECURITY.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmarks", "Benchmarks", "{E25F6292-80CE-45FE-97B0-0EBBF8E1FC6A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Schema.NET.Benchmarks", "Benchmarks\Schema.NET.Benchmarks\Schema.NET.Benchmarks.csproj", "{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -73,6 +77,10 @@ Global
{3E75002C-0EF2-4F04-8EC6-CED90BA27AD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E75002C-0EF2-4F04-8EC6-CED90BA27AD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E75002C-0EF2-4F04-8EC6-CED90BA27AD1}.Release|Any CPU.Build.0 = Release|Any CPU
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -83,6 +91,7 @@ Global
{3E75002C-0EF2-4F04-8EC6-CED90BA27AD1} = {E1B24F25-B8A4-46EE-B7EB-7803DCFC543F}
{566DF0E2-1288-4083-9B55-4C8B69BB1432} = {0555C737-CE4B-4C78-87AB-6296E1E32D01}
{0555C737-CE4B-4C78-87AB-6296E1E32D01} = {7EDFA103-DB69-4C88-9DE4-97ADBF8253A1}
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC} = {E25F6292-80CE-45FE-97B0-0EBBF8E1FC6A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {73F36209-F8D6-4066-8951-D97729F773CF}
Expand Down