|
7 | 7 | using DeviceDetectorNET;
|
8 | 8 | using MyCSharp.HttpUserAgentParser.Providers;
|
9 | 9 |
|
10 |
| -namespace MyCSharp.HttpUserAgentParser.Benchmarks.LibraryComparison |
| 10 | +namespace MyCSharp.HttpUserAgentParser.Benchmarks.LibraryComparison; |
| 11 | + |
| 12 | +[ShortRunJob] |
| 13 | +[MemoryDiagnoser] |
| 14 | +[CategoriesColumn] |
| 15 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 16 | +public class LibraryComparisonBenchmarks |
11 | 17 | {
|
12 |
| - [ShortRunJob] |
13 |
| - [MemoryDiagnoser] |
14 |
| - [CategoriesColumn] |
15 |
| - [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
16 |
| - public class LibraryComparisonBenchmarks |
| 18 | + public record TestData(string Label, string UserAgent) |
17 | 19 | {
|
18 |
| - public record TestData(string Label, string UserAgent) |
19 |
| - { |
20 |
| - public override string ToString() => Label; |
21 |
| - } |
| 20 | + public override string ToString() => Label; |
| 21 | + } |
22 | 22 |
|
23 |
| - [ParamsSource(nameof(GetTestUserAgents))] |
24 |
| - public TestData Data { get; set; } |
| 23 | + [ParamsSource(nameof(GetTestUserAgents))] |
| 24 | + public TestData Data { get; set; } |
25 | 25 |
|
26 |
| - public IEnumerable<TestData> GetTestUserAgents() |
27 |
| - { |
28 |
| - 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"); |
29 |
| - yield return new("Google-Bot", "APIs-Google (+https://developers.google.com/webmasters/APIs-Google.html)"); |
30 |
| - } |
| 26 | + public IEnumerable<TestData> GetTestUserAgents() |
| 27 | + { |
| 28 | + 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"); |
| 29 | + yield return new("Google-Bot", "APIs-Google (+https://developers.google.com/webmasters/APIs-Google.html)"); |
| 30 | + } |
31 | 31 |
|
32 |
| - [Benchmark(Baseline = true, Description = "MyCSharp")] |
33 |
| - [BenchmarkCategory("Basic")] |
34 |
| - public HttpUserAgentInformation MyCSharpBasic() |
35 |
| - { |
36 |
| - HttpUserAgentInformation info = HttpUserAgentParser.Parse(Data.UserAgent); |
37 |
| - return info; |
38 |
| - } |
| 32 | + [Benchmark(Baseline = true, Description = "MyCSharp")] |
| 33 | + [BenchmarkCategory("Basic")] |
| 34 | + public HttpUserAgentInformation MyCSharpBasic() |
| 35 | + { |
| 36 | + HttpUserAgentInformation info = HttpUserAgentParser.Parse(Data.UserAgent); |
| 37 | + return info; |
| 38 | + } |
39 | 39 |
|
40 |
| - private static readonly HttpUserAgentParserCachedProvider s_myCSharpCachedProvider = new(); |
| 40 | + private static readonly HttpUserAgentParserCachedProvider s_myCSharpCachedProvider = new(); |
41 | 41 |
|
42 |
| - [Benchmark(Baseline = true, Description = "MyCSharp")] |
43 |
| - [BenchmarkCategory("Cached")] |
44 |
| - public HttpUserAgentInformation MyCSharpCached() |
45 |
| - { |
46 |
| - return s_myCSharpCachedProvider.Parse(Data.UserAgent); |
47 |
| - } |
| 42 | + [Benchmark(Baseline = true, Description = "MyCSharp")] |
| 43 | + [BenchmarkCategory("Cached")] |
| 44 | + public HttpUserAgentInformation MyCSharpCached() |
| 45 | + { |
| 46 | + return s_myCSharpCachedProvider.Parse(Data.UserAgent); |
| 47 | + } |
48 | 48 |
|
49 |
| - [Benchmark(Description = "UAParser")] |
50 |
| - [BenchmarkCategory("Basic")] |
51 |
| - public UAParser.ClientInfo UAParserBasic() |
52 |
| - { |
53 |
| - UAParser.ClientInfo info = UAParser.Parser.GetDefault().Parse(Data.UserAgent); |
54 |
| - return info; |
55 |
| - } |
| 49 | + [Benchmark(Description = "UAParser")] |
| 50 | + [BenchmarkCategory("Basic")] |
| 51 | + public UAParser.ClientInfo UAParserBasic() |
| 52 | + { |
| 53 | + UAParser.ClientInfo info = UAParser.Parser.GetDefault().Parse(Data.UserAgent); |
| 54 | + return info; |
| 55 | + } |
56 | 56 |
|
57 |
| - private static readonly UAParser.Parser s_uaParser = UAParser.Parser.GetDefault(new UAParser.ParserOptions { UseCompiledRegex = true }); |
| 57 | + private static readonly UAParser.Parser s_uaParser = UAParser.Parser.GetDefault(new UAParser.ParserOptions { UseCompiledRegex = true }); |
58 | 58 |
|
59 |
| - [Benchmark(Description = "UAParser")] |
60 |
| - [BenchmarkCategory("Cached")] |
61 |
| - public UAParser.ClientInfo UAParserCached() |
62 |
| - { |
63 |
| - UAParser.ClientInfo info = s_uaParser.Parse(Data.UserAgent); |
64 |
| - return info; |
65 |
| - } |
| 59 | + [Benchmark(Description = "UAParser")] |
| 60 | + [BenchmarkCategory("Cached")] |
| 61 | + public UAParser.ClientInfo UAParserCached() |
| 62 | + { |
| 63 | + UAParser.ClientInfo info = s_uaParser.Parse(Data.UserAgent); |
| 64 | + return info; |
| 65 | + } |
66 | 66 |
|
67 |
| - [Benchmark(Description = "DeviceDetector.NET")] |
68 |
| - [BenchmarkCategory("Basic")] |
69 |
| - public object DeviceDetectorNETBasic() |
70 |
| - { |
71 |
| - DeviceDetector dd = new(Data.UserAgent); |
72 |
| - dd.Parse(); |
| 67 | + [Benchmark(Description = "DeviceDetector.NET")] |
| 68 | + [BenchmarkCategory("Basic")] |
| 69 | + public object DeviceDetectorNETBasic() |
| 70 | + { |
| 71 | + DeviceDetector dd = new(Data.UserAgent); |
| 72 | + dd.Parse(); |
73 | 73 |
|
74 |
| - var info = new |
75 |
| - { |
76 |
| - Client = dd.GetClient(), |
77 |
| - OS = dd.GetOs(), |
78 |
| - Device = dd.GetDeviceName(), |
79 |
| - Brand = dd.GetBrandName(), |
80 |
| - Model = dd.GetModel() |
81 |
| - }; |
| 74 | + var info = new |
| 75 | + { |
| 76 | + Client = dd.GetClient(), |
| 77 | + OS = dd.GetOs(), |
| 78 | + Device = dd.GetDeviceName(), |
| 79 | + Brand = dd.GetBrandName(), |
| 80 | + Model = dd.GetModel() |
| 81 | + }; |
82 | 82 |
|
83 |
| - return info; |
84 |
| - } |
| 83 | + return info; |
85 | 84 | }
|
86 | 85 | }
|
0 commit comments