mirror of
https://gitee.com/LongbowEnterprise/BootstrapBlazor.git
synced 2025-12-06 15:29:09 +08:00
feat(ICacheManager): add TryGetCacheEntry method (#5216)
* doc: 增加到期时间列 * test: 增加 benmark 测试功能 * feat: 增加 TryGetCacheEntry 方法 * doc: 增加过期时间 * test: 更新单元测试 * test: 更新单元测试
This commit is contained in:
62
tools/Benchmarks/Benmarks.cs
Normal file
62
tools/Benchmarks/Benmarks.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the Apache 2.0 License
|
||||
// See the LICENSE file in the project root for more information.
|
||||
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
|
||||
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Jobs;
|
||||
using Microsoft.Diagnostics.Runtime.Utilities;
|
||||
using System.Dynamic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace UnitTest.Benchmarks;
|
||||
|
||||
[SimpleJob(RuntimeMoniker.Net90)]
|
||||
public class Benchmarks
|
||||
{
|
||||
static readonly Foo _instance = new();
|
||||
|
||||
static readonly FieldInfo _privateField = typeof(Foo).GetField("_name", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
|
||||
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_name")]
|
||||
static extern ref string GetNameValue(Foo @this);
|
||||
|
||||
[Benchmark]
|
||||
public object? Reflection()
|
||||
{
|
||||
var fieldInfo = typeof(Foo).GetField("_name", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
return fieldInfo.GetValue(_instance);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public object? ReflectionWithCache() => _privateField.GetValue(_instance);
|
||||
|
||||
[Benchmark]
|
||||
public string UnsafeAccessor() => GetNameValue(_instance);
|
||||
|
||||
[Benchmark]
|
||||
public string DirectAccess() => _instance.GetName();
|
||||
|
||||
[Benchmark]
|
||||
public string Lambda() => GetFieldValue();
|
||||
|
||||
[Benchmark]
|
||||
public string LambdaWithCache() => GetFooFieldFunc(_instance);
|
||||
|
||||
public string GetFieldValue()
|
||||
{
|
||||
var method = GetFooFieldExpression().Compile();
|
||||
return method.Invoke(_instance);
|
||||
}
|
||||
|
||||
private static Func<Foo, string> GetFooFieldFunc = GetFooFieldExpression().Compile();
|
||||
|
||||
static Expression<Func<Foo, string>> GetFooFieldExpression()
|
||||
{
|
||||
var param_p1 = Expression.Parameter(typeof(Foo));
|
||||
var body = Expression.Field(param_p1, _privateField);
|
||||
return Expression.Lambda<Func<Foo, string>>(Expression.Convert(body, typeof(string)), param_p1);
|
||||
}
|
||||
}
|
||||
13
tools/Benchmarks/Foo.cs
Normal file
13
tools/Benchmarks/Foo.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the Apache 2.0 License
|
||||
// See the LICENSE file in the project root for more information.
|
||||
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
|
||||
|
||||
namespace UnitTest.Benchmarks;
|
||||
|
||||
public class Foo
|
||||
{
|
||||
private string _name = "test";
|
||||
|
||||
public string GetName() => _name;
|
||||
}
|
||||
11
tools/Benchmarks/Program.cs
Normal file
11
tools/Benchmarks/Program.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the Apache 2.0 License
|
||||
// See the LICENSE file in the project root for more information.
|
||||
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
|
||||
|
||||
using BenchmarkDotNet.Running;
|
||||
using UnitTest.Benchmarks;
|
||||
|
||||
BenchmarkRunner.Run<Benchmarks>();
|
||||
|
||||
Console.ReadKey();
|
||||
14
tools/Benchmarks/UnitTest.Benchmarks.csproj
Normal file
14
tools/Benchmarks/UnitTest.Benchmarks.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user