Skip to content

Commit eb49aa7

Browse files
committed
Refactor benchmarks and improve code clarity
- Updated `LibraryComparisonBenchmarks.cs` to use a record type for `TestData`, enhancing readability and modernizing the code structure. - Added summary comments in `HttpUserAgentParserMemoryCacheServiceCollectionExtensions.cs` and improved method documentation for clarity. - Streamlined constructor initialization in `HttpUserAgentParserMemoryCachedProviderOptions.cs`, removing unnecessary keywords while preserving functionality.
1 parent fc31ca7 commit eb49aa7

File tree

3 files changed

+78
-80
lines changed

3 files changed

+78
-80
lines changed

perf/HttpUserAgentParser.Benchmarks/LibraryComparison/LibraryComparisonBenchmarks.cs

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,79 @@
77
using DeviceDetectorNET;
88
using MyCSharp.HttpUserAgentParser.Providers;
99

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
1117
{
12-
[ShortRunJob]
13-
[MemoryDiagnoser]
14-
[CategoriesColumn]
15-
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
16-
public class LibraryComparisonBenchmarks
18+
public record TestData(string Label, string UserAgent)
1719
{
18-
public record TestData(string Label, string UserAgent)
19-
{
20-
public override string ToString() => Label;
21-
}
20+
public override string ToString() => Label;
21+
}
2222

23-
[ParamsSource(nameof(GetTestUserAgents))]
24-
public TestData Data { get; set; }
23+
[ParamsSource(nameof(GetTestUserAgents))]
24+
public TestData Data { get; set; }
2525

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+
}
3131

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+
}
3939

40-
private static readonly HttpUserAgentParserCachedProvider s_myCSharpCachedProvider = new();
40+
private static readonly HttpUserAgentParserCachedProvider s_myCSharpCachedProvider = new();
4141

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+
}
4848

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+
}
5656

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 });
5858

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+
}
6666

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();
7373

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+
};
8282

83-
return info;
84-
}
83+
return info;
8584
}
8685
}

src/HttpUserAgentParser.MemoryCache/DependencyInjection/HttpUserAgentParserMemoryCacheServiceCollectionExtensions.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@
44
using MyCSharp.HttpUserAgentParser.DependencyInjection;
55
using MyCSharp.HttpUserAgentParser.Providers;
66

7-
namespace MyCSharp.HttpUserAgentParser.MemoryCache.DependencyInjection
7+
namespace MyCSharp.HttpUserAgentParser.MemoryCache.DependencyInjection;
8+
9+
/// <summary>
10+
/// Dependency injection extensions for IMemoryCache
11+
/// </summary>
12+
public static class HttpUserAgentParserMemoryCacheServiceCollectionExtensions
813
{
914
/// <summary>
10-
/// Dependency injection extensions for IMemoryCache
15+
/// Registers <see cref="HttpUserAgentParserCachedProvider"/> as singleton to <see cref="IHttpUserAgentParserProvider"/>
1116
/// </summary>
12-
public static class HttpUserAgentParserMemoryCacheServiceCollectionExtensions
17+
public static HttpUserAgentParserDependencyInjectionOptions AddHttpUserAgentMemoryCachedParser(
18+
this IServiceCollection services, Action<HttpUserAgentParserMemoryCachedProviderOptions>? options = null)
1319
{
14-
/// <summary>
15-
/// Registers <see cref="HttpUserAgentParserCachedProvider"/> as singleton to <see cref="IHttpUserAgentParserProvider"/>
16-
/// </summary>
17-
public static HttpUserAgentParserDependencyInjectionOptions AddHttpUserAgentMemoryCachedParser(
18-
this IServiceCollection services, Action<HttpUserAgentParserMemoryCachedProviderOptions>? options = null)
19-
{
20-
HttpUserAgentParserMemoryCachedProviderOptions providerOptions = new();
21-
options?.Invoke(providerOptions);
20+
HttpUserAgentParserMemoryCachedProviderOptions providerOptions = new();
21+
options?.Invoke(providerOptions);
2222

23-
// register options
24-
services.AddSingleton(providerOptions);
23+
// register options
24+
services.AddSingleton(providerOptions);
2525

26-
// register cache provider
27-
return services.AddHttpUserAgentParser<HttpUserAgentParserMemoryCachedProvider>();
28-
}
26+
// register cache provider
27+
return services.AddHttpUserAgentParser<HttpUserAgentParserMemoryCachedProvider>();
2928
}
3029
}

src/HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheEntryOptions ca
4141
public HttpUserAgentParserMemoryCachedProviderOptions(
4242
MemoryCacheOptions? cacheOptions = null, MemoryCacheEntryOptions? cacheEntryOptions = null)
4343
{
44-
this.CacheEntryOptions = cacheEntryOptions ?? new MemoryCacheEntryOptions
44+
CacheEntryOptions = cacheEntryOptions ?? new MemoryCacheEntryOptions
4545
{
4646
// defaults
4747
SlidingExpiration = TimeSpan.FromDays(1)
4848
};
4949

50-
this.CacheOptions = cacheOptions ?? new MemoryCacheOptions
50+
CacheOptions = cacheOptions ?? new MemoryCacheOptions
5151
{
5252
// defaults
5353
SizeLimit = 256

0 commit comments

Comments
 (0)