|
| 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 | +} |
0 commit comments