Skip to content

Commit 443987a

Browse files
committed
Added benchmark project for comparison with other libraries
1 parent 30a7553 commit 443987a

File tree

3 files changed

+119
-25
lines changed

3 files changed

+119
-25
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright © myCSharp 2020-2021, all rights reserved
2+
3+
using System.Collections.Generic;
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Columns;
6+
using BenchmarkDotNet.Configs;
7+
using BenchmarkDotNet.Diagnosers;
8+
using DeviceDetectorNET;
9+
using MyCSharp.HttpUserAgentParser.Providers;
10+
11+
namespace MyCSharp.HttpUserAgentParser.Benchmarks.LibraryComparison
12+
{
13+
[Config(typeof(Config))]
14+
public class LibraryComparisonBenchmarks
15+
{
16+
private class Config : ManualConfig
17+
{
18+
public Config()
19+
{
20+
AddDiagnoser(MemoryDiagnoser.Default);
21+
22+
AddColumn(CategoriesColumn.Default);
23+
AddLogicalGroupRules(BenchmarkLogicalGroupRule.ByCategory);
24+
25+
// Needed for DeviceDetector.NET
26+
// https://github.com/totpero/DeviceDetector.NET/issues/44
27+
WithOptions(ConfigOptions.DisableOptimizationsValidator);
28+
}
29+
}
30+
31+
public record TestData(string Label, string UserAgent)
32+
{
33+
public override string ToString() => Label;
34+
}
35+
36+
[ParamsSource(nameof(GetTestUserAgents))]
37+
public TestData Data { get; set; } = null!;
38+
39+
public IEnumerable<TestData> GetTestUserAgents()
40+
{
41+
yield return new("Chrome Win10", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36");
42+
yield return new("Google-Bot", "APIs-Google (+https://developers.google.com/webmasters/APIs-Google.html)");
43+
}
44+
45+
[Benchmark(Baseline = true, Description = "MyCSharp")]
46+
[BenchmarkCategory("Basic")]
47+
public HttpUserAgentInformation MyCSharpBasic()
48+
{
49+
HttpUserAgentInformation info = HttpUserAgentParser.Parse(Data.UserAgent);
50+
return info;
51+
}
52+
53+
private static readonly HttpUserAgentParserCachedProvider s_myCSharpCachedProvider = new();
54+
55+
[Benchmark(Baseline = true, Description = "MyCSharp")]
56+
[BenchmarkCategory("Cached")]
57+
public HttpUserAgentInformation MyCSharpCached()
58+
{
59+
return s_myCSharpCachedProvider.Parse(Data.UserAgent);
60+
}
61+
62+
[Benchmark(Description = "UAParser")]
63+
[BenchmarkCategory("Basic")]
64+
public UAParser.ClientInfo UAParserBasic()
65+
{
66+
UAParser.ClientInfo info = UAParser.Parser.GetDefault().Parse(Data.UserAgent);
67+
return info;
68+
}
69+
70+
private static readonly UAParser.Parser s_uaParser = UAParser.Parser.GetDefault(new UAParser.ParserOptions { UseCompiledRegex = true });
71+
72+
[Benchmark(Description = "UAParser")]
73+
[BenchmarkCategory("Cached")]
74+
public UAParser.ClientInfo UAParserCached()
75+
{
76+
UAParser.ClientInfo info = s_uaParser.Parse(Data.UserAgent);
77+
return info;
78+
}
79+
80+
[Benchmark(Description = "DeviceDetector.NET")]
81+
[BenchmarkCategory("Basic")]
82+
public object DeviceDetectorNETBasic()
83+
{
84+
DeviceDetector dd = new(Data.UserAgent);
85+
dd.Parse();
86+
87+
var info = new
88+
{
89+
Client = dd.GetClient(),
90+
OS = dd.GetOs(),
91+
Device = dd.GetDeviceName(),
92+
Brand = dd.GetBrandName(),
93+
Model = dd.GetModel()
94+
};
95+
96+
return info;
97+
}
98+
}
99+
}
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
6-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7-
<LangVersion>9.0</LangVersion>
8-
<DebugType>full</DebugType>
9-
<DebugSymbols>true</DebugSymbols>
10-
</PropertyGroup>
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
118

12-
<ItemGroup>
13-
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
14-
<PackageReference Include="UAParser" Version="3.1.46" />
15-
</ItemGroup>
9+
<ItemGroup>
10+
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
11+
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.1" Condition="'$(OS)' == 'Windows_NT'" />
12+
</ItemGroup>
1613

17-
<ItemGroup>
18-
<ProjectReference Include="..\..\src\MyCSharp.HttpUserAgentParser\MyCSharp.HttpUserAgentParser.csproj" />
19-
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\src\MyCSharp.HttpUserAgentParser\MyCSharp.HttpUserAgentParser.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup Label="Libraries for comparison">
19+
<PackageReference Include="UAParser" Version="3.1.46" />
20+
<PackageReference Include="DeviceDetector.NET" Version="4.2.0" />
21+
<PackageReference Include="Ng.UserAgentService" Version="1.0.3" />
22+
</ItemGroup>
2023

2124
</Project>
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
// Copyright © myCSharp 2020-2021, all rights reserved
22

3+
using System.Reflection;
34
using BenchmarkDotNet.Running;
45

5-
namespace MyCSharp.HttpUserAgentParser.Benchmarks
6-
{
7-
class Program
8-
{
9-
static void Main()
10-
{
11-
BenchmarkRunner.Run<UserAgentBenchmarks>();
12-
}
13-
}
14-
}
6+
BenchmarkSwitcher.FromAssembly(Assembly.GetExecutingAssembly()).Run(args);

0 commit comments

Comments
 (0)