Skip to content

Commit 8f7ac2d

Browse files
TurnerjRehanSaeed
authored andcommitted
Add basic serialize/deserialize benchmarks (#107)
* Add basic serialize/deserialize benchmarks * Add additional benchmark settings * Add support for building the benchmark project on Linux
1 parent 5987625 commit 8f7ac2d

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
namespace Schema.NET.Benchmarks.Core
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using BenchmarkDotNet.Attributes;
6+
7+
public class BookBenchmark : SchemaBenchmarkBase
8+
{
9+
[GlobalSetup]
10+
public void Setup() => this.ConfigureBenchmark(new Book()
11+
{
12+
Id = new Uri("http://example.com/book/1"),
13+
Name = "The Catcher in the Rye",
14+
Author = new Person()
15+
{
16+
Name = "J.D. Salinger"
17+
},
18+
Url = new Uri("http://www.barnesandnoble.com/store/info/offer/JDSalinger"),
19+
WorkExample = new List<ICreativeWork>()
20+
{
21+
new Book()
22+
{
23+
Isbn = "031676948",
24+
BookEdition = "2nd Edition",
25+
BookFormat = BookFormatType.Hardcover,
26+
PotentialAction = new ReadAction()
27+
{
28+
Target = new EntryPoint()
29+
{
30+
UrlTemplate = "http://www.barnesandnoble.com/store/info/offer/0316769487?purchase=true",
31+
ActionPlatform = new List<Uri>()
32+
{
33+
new Uri("https://schema.org/DesktopWebPlatform"),
34+
new Uri("https://schema.org/IOSPlatform"),
35+
new Uri("https://schema.org/AndroidPlatform")
36+
}
37+
},
38+
ExpectsAcceptanceOf = new Offer()
39+
{
40+
Price = 6.99M,
41+
PriceCurrency = "USD",
42+
EligibleRegion = new Country()
43+
{
44+
Name = "US"
45+
},
46+
Availability = ItemAvailability.InStock
47+
}
48+
},
49+
},
50+
new Book()
51+
{
52+
Isbn = "031676947",
53+
BookEdition = "1st Edition",
54+
BookFormat = BookFormatType.EBook,
55+
PotentialAction = new ReadAction()
56+
{
57+
Target = new EntryPoint()
58+
{
59+
UrlTemplate = "http://www.barnesandnoble.com/store/info/offer/031676947?purchase=true",
60+
ActionPlatform = new List<Uri>()
61+
{
62+
new Uri("https://schema.org/DesktopWebPlatform"),
63+
new Uri("https://schema.org/IOSPlatform"),
64+
new Uri("https://schema.org/AndroidPlatform")
65+
}
66+
},
67+
ExpectsAcceptanceOf = new Offer()
68+
{
69+
Price = 1.99M,
70+
PriceCurrency = "USD",
71+
EligibleRegion = new Country()
72+
{
73+
Name = "UK"
74+
},
75+
Availability = ItemAvailability.InStock
76+
}
77+
},
78+
}
79+
}
80+
});
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Schema.NET.Benchmarks.Core
2+
{
3+
using System;
4+
using BenchmarkDotNet.Attributes;
5+
6+
public class WebsiteBenchmark : SchemaBenchmarkBase
7+
{
8+
[GlobalSetup]
9+
public void Setup() => this.ConfigureBenchmark(new WebSite()
10+
{
11+
PotentialAction = new SearchAction()
12+
{
13+
Target = new Uri("http://example.com/search?&q={query}"),
14+
QueryInput = "required"
15+
},
16+
Url = new Uri("https://example.com")
17+
});
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Schema.NET.Benchmarks
2+
{
3+
using System;
4+
using BenchmarkDotNet.Running;
5+
6+
internal class Program
7+
{
8+
private static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup Label="Build">
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks>
6+
<CodeAnalysisRuleSet>../../MinimumRecommendedRulesWithStyleCop.ruleset</CodeAnalysisRuleSet>
7+
<LangVersion>latest</LangVersion>
8+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
9+
</PropertyGroup>
10+
11+
<ItemGroup Label="Project References">
12+
<ProjectReference Include="..\..\Source\Schema.NET\Schema.NET.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup Label="Package References">
16+
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
17+
<!-- See https://github.com/Microsoft/dotnet/tree/master/releases/reference-assemblies -->
18+
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" PrivateAssets="All" Version="1.0.0" />
19+
</ItemGroup>
20+
21+
<ItemGroup Label="Analyzer Package References">
22+
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" PrivateAssets="All" Version="16.4.16" />
23+
<PackageReference Include="StyleCop.Analyzers" PrivateAssets="All" Version="1.1.118" />
24+
</ItemGroup>
25+
26+
27+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace Schema.NET.Benchmarks
2+
{
3+
using System;
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Jobs;
6+
using Newtonsoft.Json;
7+
8+
[KeepBenchmarkFiles]
9+
[MemoryDiagnoser]
10+
[MinColumn]
11+
[MaxColumn]
12+
[HtmlExporter]
13+
[CsvMeasurementsExporter]
14+
[RPlotExporter]
15+
[SimpleJob(RuntimeMoniker.Net472)]
16+
[SimpleJob(RuntimeMoniker.NetCoreApp30)]
17+
public abstract class SchemaBenchmarkBase
18+
{
19+
protected Thing Thing { get; set; }
20+
21+
private Type ThingType { get; set; }
22+
23+
private string SerializedThing { get; set; }
24+
25+
[Benchmark]
26+
public string Serialize() => this.Thing.ToString();
27+
28+
[Benchmark]
29+
public object Deserialize() => JsonConvert.DeserializeObject(this.SerializedThing, this.ThingType);
30+
31+
protected void ConfigureBenchmark(Thing thing)
32+
{
33+
this.Thing = thing;
34+
this.ThingType = this.Thing.GetType();
35+
this.SerializedThing = this.Thing.ToString();
36+
}
37+
}
38+
}

Schema.NET.sln

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{0555
5555
.github\SECURITY.md = .github\SECURITY.md
5656
EndProjectSection
5757
EndProject
58+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmarks", "Benchmarks", "{E25F6292-80CE-45FE-97B0-0EBBF8E1FC6A}"
59+
EndProject
60+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Schema.NET.Benchmarks", "Benchmarks\Schema.NET.Benchmarks\Schema.NET.Benchmarks.csproj", "{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}"
61+
EndProject
5862
Global
5963
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6064
Debug|Any CPU = Debug|Any CPU
@@ -73,6 +77,10 @@ Global
7377
{3E75002C-0EF2-4F04-8EC6-CED90BA27AD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
7478
{3E75002C-0EF2-4F04-8EC6-CED90BA27AD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
7579
{3E75002C-0EF2-4F04-8EC6-CED90BA27AD1}.Release|Any CPU.Build.0 = Release|Any CPU
80+
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
81+
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
82+
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
83+
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC}.Release|Any CPU.Build.0 = Release|Any CPU
7684
EndGlobalSection
7785
GlobalSection(SolutionProperties) = preSolution
7886
HideSolutionNode = FALSE
@@ -83,6 +91,7 @@ Global
8391
{3E75002C-0EF2-4F04-8EC6-CED90BA27AD1} = {E1B24F25-B8A4-46EE-B7EB-7803DCFC543F}
8492
{566DF0E2-1288-4083-9B55-4C8B69BB1432} = {0555C737-CE4B-4C78-87AB-6296E1E32D01}
8593
{0555C737-CE4B-4C78-87AB-6296E1E32D01} = {7EDFA103-DB69-4C88-9DE4-97ADBF8253A1}
94+
{EA1CBFC3-F165-4811-AA9F-157DEB8E31CC} = {E25F6292-80CE-45FE-97B0-0EBBF8E1FC6A}
8695
EndGlobalSection
8796
GlobalSection(ExtensibilityGlobals) = postSolution
8897
SolutionGuid = {73F36209-F8D6-4066-8951-D97729F773CF}

0 commit comments

Comments
 (0)