Skip to content

Commit 3b6ddc5

Browse files
committed
NewLife.Core的内存缓存突破10亿TPS。最后一次优化,是把当前测试进程的优先级提高,抢占尽可能多的CPU时间
1 parent 6a19f1b commit 3b6ddc5

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

NewLife.Core/Caching/Cache.cs

+20-10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public abstract class Cache : DisposeBase, ICache
3636
#region 构造
3737
/// <summary>构造函数</summary>
3838
protected Cache() => Name = GetType().Name.TrimEnd("Cache");
39+
40+
/// <summary>销毁。释放资源</summary>
41+
/// <param name="disposing"></param>
42+
protected override void Dispose(Boolean disposing)
43+
{
44+
base.Dispose(disposing);
45+
46+
_keys = null;
47+
//_keys2 = null;
48+
}
3949
#endregion
4050

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

368378
// 提前准备Keys,减少性能测试中的干扰
369379
var key = "bstr_";
370-
var key2 = "bint_";
380+
//var key2 = "bint_";
371381
var max = cpu > 64 ? cpu : 64;
372382
var maxTimes = times * max;
373383
if (!rand) maxTimes = max;
374384
_keys = new String[maxTimes];
375-
_keys2 = new String[maxTimes];
385+
//_keys2 = new String[maxTimes];
376386

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

385-
sb.Clear();
386-
sb.Append(key2);
387-
sb.Append(i);
388-
_keys2[i] = sb.ToString();
395+
//sb.Clear();
396+
//sb.Append(key2);
397+
//sb.Append(i);
398+
//_keys2[i] = sb.ToString();
389399
}
390400

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

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

457+
// 累加测试
458+
rs += BenchInc(_keys!, times, threads, rand, batch);
459+
447460
// 删除测试
448461
rs += BenchRemove(_keys, times, threads, rand, batch);
449462

450-
// 累加测试
451-
rs += BenchInc(_keys2!, times, threads, rand, batch);
452-
453463
return rs;
454464
}
455465

NewLife.Core/Caching/MemoryCache.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class KeyEventArgs : CancelEventArgs
1717
public String Key { get; set; } = null!;
1818
}
1919

20-
/// <summary>默认字典缓存</summary>
20+
/// <summary>内存缓存。并行字典实现,峰值性能10亿ops</summary>
2121
public class MemoryCache : Cache
2222
{
2323
#region 属性
@@ -497,7 +497,22 @@ protected class CacheItem
497497
/// <summary>设置数值和过期时间</summary>
498498
/// <param name="value"></param>
499499
/// <param name="expire">过期时间,秒</param>
500-
public void Set<T>(T value, Int32 expire) => Set(value, TimeSpan.FromSeconds(expire));
500+
public void Set<T>(T value, Int32 expire)
501+
{
502+
var type = typeof(T);
503+
TypeCode = type.GetTypeCode();
504+
505+
if (IsInt())
506+
_valueLong = value.ToLong();
507+
else
508+
_value = value;
509+
510+
var now = VisitTime = Runtime.TickCount64;
511+
if (expire <= 0)
512+
ExpiredTime = Int64.MaxValue;
513+
else
514+
ExpiredTime = now + expire * 1000;
515+
}
501516

502517
/// <summary>设置数值和过期时间</summary>
503518
/// <param name="value"></param>

Test/Program.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,12 @@ private static void Test3()
204204

205205
private static void Test4()
206206
{
207-
var v = Rand.NextBytes(32);
208-
Console.WriteLine(v.ToBase64());
207+
// 提升进程优先级
208+
var p = Process.GetCurrentProcess();
209+
p.PriorityClass = ProcessPriorityClass.High;
210+
211+
//var v = Rand.NextBytes(32);
212+
//Console.WriteLine(v.ToBase64());
209213

210214
ICache ch = null;
211215
//ICache ch = new DbCache();

0 commit comments

Comments
 (0)