Skip to content

Commit

Permalink
NewLife.Core的内存缓存突破10亿TPS。最后一次优化,是把当前测试进程的优先级提高,抢占尽可能多的CPU时间
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Sep 15, 2024
1 parent 6a19f1b commit 3b6ddc5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
30 changes: 20 additions & 10 deletions NewLife.Core/Caching/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public abstract class Cache : DisposeBase, ICache
#region 构造
/// <summary>构造函数</summary>
protected Cache() => Name = GetType().Name.TrimEnd("Cache");

/// <summary>销毁。释放资源</summary>
/// <param name="disposing"></param>
protected override void Dispose(Boolean disposing)
{
base.Dispose(disposing);

_keys = null;
//_keys2 = null;
}
#endregion

#region 基础操作
Expand Down Expand Up @@ -367,12 +377,12 @@ public virtual Int64 Bench(Boolean rand = false, Int32 batch = 0)

// 提前准备Keys,减少性能测试中的干扰
var key = "bstr_";
var key2 = "bint_";
//var key2 = "bint_";
var max = cpu > 64 ? cpu : 64;
var maxTimes = times * max;
if (!rand) maxTimes = max;
_keys = new String[maxTimes];
_keys2 = new String[maxTimes];
//_keys2 = new String[maxTimes];

var sb = new StringBuilder();
for (var i = 0; i < _keys.Length; i++)
Expand All @@ -382,10 +392,10 @@ public virtual Int64 Bench(Boolean rand = false, Int32 batch = 0)
sb.Append(i);
_keys[i] = sb.ToString();

sb.Clear();
sb.Append(key2);
sb.Append(i);
_keys2[i] = sb.ToString();
//sb.Clear();
//sb.Append(key2);
//sb.Append(i);
//_keys2[i] = sb.ToString();
}

// 单线程
Expand Down Expand Up @@ -417,7 +427,7 @@ public virtual Int64 Bench(Boolean rand = false, Int32 batch = 0)
protected virtual Int32 GetTimesPerThread(Boolean rand, Int32 batch) => 10_000;

private String[]? _keys;
private String[]? _keys2;
//private String[]? _keys2;
/// <summary>使用指定线程测试指定次数</summary>
/// <param name="times">次数</param>
/// <param name="threads">线程</param>
Expand All @@ -444,12 +454,12 @@ public virtual Int64 BenchOne(Int64 times, Int32 threads, Boolean rand, Int32 ba
// 读取测试
rs += BenchGet(_keys, times, threads, rand, batch);

// 累加测试
rs += BenchInc(_keys!, times, threads, rand, batch);

// 删除测试
rs += BenchRemove(_keys, times, threads, rand, batch);

// 累加测试
rs += BenchInc(_keys2!, times, threads, rand, batch);

return rs;
}

Expand Down
19 changes: 17 additions & 2 deletions NewLife.Core/Caching/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class KeyEventArgs : CancelEventArgs
public String Key { get; set; } = null!;
}

/// <summary>默认字典缓存</summary>
/// <summary>内存缓存。并行字典实现,峰值性能10亿ops</summary>
public class MemoryCache : Cache
{
#region 属性
Expand Down Expand Up @@ -497,7 +497,22 @@ protected class CacheItem
/// <summary>设置数值和过期时间</summary>
/// <param name="value"></param>
/// <param name="expire">过期时间,秒</param>
public void Set<T>(T value, Int32 expire) => Set(value, TimeSpan.FromSeconds(expire));
public void Set<T>(T value, Int32 expire)
{
var type = typeof(T);
TypeCode = type.GetTypeCode();

if (IsInt())
_valueLong = value.ToLong();
else
_value = value;

var now = VisitTime = Runtime.TickCount64;
if (expire <= 0)
ExpiredTime = Int64.MaxValue;
else
ExpiredTime = now + expire * 1000;
}

/// <summary>设置数值和过期时间</summary>
/// <param name="value"></param>
Expand Down
8 changes: 6 additions & 2 deletions Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,12 @@ private static void Test3()

private static void Test4()
{
var v = Rand.NextBytes(32);
Console.WriteLine(v.ToBase64());
// 提升进程优先级
var p = Process.GetCurrentProcess();
p.PriorityClass = ProcessPriorityClass.High;

//var v = Rand.NextBytes(32);
//Console.WriteLine(v.ToBase64());

ICache ch = null;
//ICache ch = new DbCache();
Expand Down

0 comments on commit 3b6ddc5

Please sign in to comment.