File tree Expand file tree Collapse file tree 4 files changed +67
-19
lines changed
perf/MyCSharp.HttpUserAgentParser.Benchmarks Expand file tree Collapse file tree 4 files changed +67
-19
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 10
10
11
11
namespace MyCSharp . HttpUserAgentParser . Benchmarks . LibraryComparison
12
12
{
13
- [ Config ( typeof ( Config ) ) ]
13
+ [ ShortRunJob ]
14
+ [ MemoryDiagnoser ]
15
+ [ CategoriesColumn ]
16
+ [ GroupBenchmarksBy ( BenchmarkLogicalGroupRule . ByCategory ) ]
14
17
public class LibraryComparisonBenchmarks
15
18
{
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
19
public record TestData ( string Label , string UserAgent )
32
20
{
33
21
public override string ToString ( ) => Label ;
34
22
}
35
23
36
24
[ ParamsSource ( nameof ( GetTestUserAgents ) ) ]
37
- public TestData Data { get ; set ; } = null ! ;
25
+ public TestData Data { get ; set ; }
38
26
39
27
public IEnumerable < TestData > GetTestUserAgents ( )
40
28
{
Original file line number Diff line number Diff line change 3
3
<PropertyGroup >
4
4
<OutputType >Exe</OutputType >
5
5
<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 >
7
12
</PropertyGroup >
8
13
9
14
<ItemGroup >
Original file line number Diff line number Diff line change 1
1
// Copyright © myCSharp 2020-2021, all rights reserved
2
2
3
3
using System . Reflection ;
4
+ using BenchmarkDotNet . Configs ;
4
5
using BenchmarkDotNet . Running ;
5
6
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 ) ;
You can’t perform that action at this time.
0 commit comments