Skip to content

Commit d59f620

Browse files
committed
Added HttpUserAgentParserBenchmarks
1 parent 443987a commit d59f620

File tree

4 files changed

+67
-19
lines changed

4 files changed

+67
-19
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright © myCSharp 2020-2021, all rights reserved
2+
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using BenchmarkDotNet.Attributes;
6+
7+
#if OS_WIN
8+
using BenchmarkDotNet.Diagnostics.Windows.Configs;
9+
#endif
10+
11+
namespace MyCSharp.HttpUserAgentParser.Benchmarks
12+
{
13+
[MemoryDiagnoser]
14+
#if OS_WIN
15+
[EtwProfiler] // needs admin-rights
16+
#endif
17+
public class HttpUserAgentParserBenchmarks
18+
{
19+
private string[] _testUserAgentMix;
20+
private HttpUserAgentInformation[] _results;
21+
22+
[GlobalSetup]
23+
public void GlobalSetup()
24+
{
25+
_testUserAgentMix = GetTestUserAgents().ToArray();
26+
_results = new HttpUserAgentInformation[_testUserAgentMix.Length];
27+
}
28+
29+
private static IEnumerable<string> GetTestUserAgents()
30+
{
31+
yield return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
32+
yield return "APIs-Google (+https://developers.google.com/webmasters/APIs-Google.html)";
33+
yield return "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0";
34+
yield return "yeah I'm unknown user agent, just to bring some fun to the mix";
35+
}
36+
37+
[Benchmark]
38+
public void Parse()
39+
{
40+
string[] testUserAgentMix = _testUserAgentMix;
41+
HttpUserAgentInformation[] results = _results;
42+
43+
for (int i = 0; i < testUserAgentMix.Length; ++i)
44+
{
45+
results[i] = HttpUserAgentParser.Parse(testUserAgentMix[i]);
46+
}
47+
}
48+
}
49+
}

perf/MyCSharp.HttpUserAgentParser.Benchmarks/LibraryComparison/LibraryComparisonBenchmarks.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,19 @@
1010

1111
namespace MyCSharp.HttpUserAgentParser.Benchmarks.LibraryComparison
1212
{
13-
[Config(typeof(Config))]
13+
[ShortRunJob]
14+
[MemoryDiagnoser]
15+
[CategoriesColumn]
16+
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
1417
public class LibraryComparisonBenchmarks
1518
{
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-
3119
public record TestData(string Label, string UserAgent)
3220
{
3321
public override string ToString() => Label;
3422
}
3523

3624
[ParamsSource(nameof(GetTestUserAgents))]
37-
public TestData Data { get; set; } = null!;
25+
public TestData Data { get; set; }
3826

3927
public IEnumerable<TestData> GetTestUserAgents()
4028
{

perf/MyCSharp.HttpUserAgentParser.Benchmarks/MyCSharp.HttpUserAgentParser.Benchmarks.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net5.0</TargetFramework>
6-
<Nullable>enable</Nullable>
6+
<Nullable>disable</Nullable>
7+
<DebugType>embedded</DebugType>
8+
</PropertyGroup>
9+
10+
<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
11+
<DefineConstants>$(DefineConstants);OS_WIN</DefineConstants>
712
</PropertyGroup>
813

914
<ItemGroup>
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Copyright © myCSharp 2020-2021, all rights reserved
22

33
using System.Reflection;
4+
using BenchmarkDotNet.Configs;
45
using BenchmarkDotNet.Running;
56

6-
BenchmarkSwitcher.FromAssembly(Assembly.GetExecutingAssembly()).Run(args);
7+
// Needed for DeviceDetector.NET
8+
// https://github.com/totpero/DeviceDetector.NET/issues/44
9+
ManualConfig config = ManualConfig.Create(DefaultConfig.Instance)
10+
.WithOptions(ConfigOptions.DisableOptimizationsValidator);
11+
12+
BenchmarkSwitcher.FromAssembly(Assembly.GetExecutingAssembly()).Run(args, config);

0 commit comments

Comments
 (0)