Skip to content

Commit c1c1246

Browse files
Copilotnnhy
andcommitted
refactor: 提取 SetAll 中重复的编码逻辑,改善测试注释
Co-authored-by: nnhy <506367+nnhy@users.noreply.github.com>
1 parent bd9ba73 commit c1c1246

2 files changed

Lines changed: 9 additions & 14 deletions

File tree

NewLife.NovaDb/Caching/NovaCache.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,25 +322,19 @@ public override void SetAll<T>(IDictionary<String, T> values, Int32 expire = -1)
322322
{
323323
if (expire < 0) expire = Expire;
324324

325+
var dict = new Dictionary<String, Byte[]?>();
326+
foreach (var kvp in values)
327+
dict[kvp.Key] = Encoder.Encode(kvp.Value)?.ReadBytes();
328+
325329
if (_kvStore != null)
326330
{
327-
var dict = new Dictionary<String, Byte[]?>();
328-
foreach (var kvp in values)
329-
dict[kvp.Key] = Encoder.Encode(kvp.Value)?.ReadBytes();
330-
331331
var ttl = expire > 0 ? TimeSpan.FromSeconds(expire) : (TimeSpan?)null;
332332
_kvStore.SetAll(dict, ttl);
333333
return;
334334
}
335335

336336
if (_client != null)
337-
{
338-
var dict = new Dictionary<String, Byte[]?>();
339-
foreach (var kvp in values)
340-
dict[kvp.Key] = Encoder.Encode(kvp.Value)?.ReadBytes();
341-
342337
_client.KvSetAllAsync(Name, dict, expire).ConfigureAwait(false).GetAwaiter().GetResult();
343-
}
344338
}
345339

346340
/// <summary>提交变更</summary>

XUnitTest/Caching/NovaCacheNetworkIntegrationTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public void TestBatchThroughputExceeds100K()
262262
{
263263
using var cache = CreateNetworkCache();
264264

265-
// 准备批量数据
265+
// 准备批量数据:每批 1000 条,每条 64 字节
266266
var batchSize = 1000;
267267
var totalOps = 0;
268268
var data = new Dictionary<String, String>();
@@ -273,7 +273,7 @@ public void TestBatchThroughputExceeds100K()
273273
cache.SetAll(data);
274274
cache.GetAll<String>(data.Keys);
275275

276-
// 计时:批量写入
276+
// 计时:批量写入(每次 SetAll 将 1000 条数据打包为一次 RPC 调用)
277277
var sw = System.Diagnostics.Stopwatch.StartNew();
278278
var iterations = 200;
279279
for (var n = 0; n < iterations; n++)
@@ -285,7 +285,7 @@ public void TestBatchThroughputExceeds100K()
285285

286286
var writeOpsPerSec = totalOps / sw.Elapsed.TotalSeconds;
287287

288-
// 计时:批量读取
288+
// 计时:批量读取(每次 GetAll 将 1000 个键打包为一次 RPC 调用)
289289
totalOps = 0;
290290
var keys = data.Keys.ToArray();
291291
sw.Restart();
@@ -298,7 +298,8 @@ public void TestBatchThroughputExceeds100K()
298298

299299
var readOpsPerSec = totalOps / sw.Elapsed.TotalSeconds;
300300

301-
// 至少一个方向应超过 100,000 ops/s
301+
// 批量操作通过将多个操作合并到单次 RPC 调用来摊薄网络开销,
302+
// 写入或读取方向的吞吐量应超过 100,000 ops/s
302303
Assert.True(writeOpsPerSec > 100_000 || readOpsPerSec > 100_000,
303304
$"批量吞吐量未达标: Write={writeOpsPerSec:N0} ops/s, Read={readOpsPerSec:N0} ops/s");
304305
}

0 commit comments

Comments
 (0)