Skip to content

Commit 01f3d31

Browse files
Copilotnnhy
andcommitted
feat: 为 KvStore / NovaCache 增加 100w/1000w 级别的性能测试和报告
Co-authored-by: nnhy <506367+nnhy@users.noreply.github.com>
1 parent efc785f commit 01f3d31

4 files changed

Lines changed: 390 additions & 47 deletions

File tree

Benchmark/KvStoreBenchmark.cs

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,161 @@ public Int32 MassSearch()
251251
return count;
252252
}
253253
}
254+
255+
/// <summary>KV 引擎海量数据基准测试(100万条)</summary>
256+
[MemoryDiagnoser]
257+
[Config(typeof(AntiViralConfig))]
258+
public class KvStoreMassData100wBenchmark
259+
{
260+
private String _storePath = null!;
261+
private Byte[] _value64 = null!;
262+
private Byte[] _value1024 = null!;
263+
264+
[GlobalSetup]
265+
public void Setup()
266+
{
267+
_storePath = Path.Combine(Path.GetTempPath(), $"NovaBench_KvMass100w_{Guid.NewGuid():N}");
268+
Directory.CreateDirectory(_storePath);
269+
_value64 = new Byte[64];
270+
Random.Shared.NextBytes(_value64);
271+
_value1024 = new Byte[1024];
272+
Random.Shared.NextBytes(_value1024);
273+
}
274+
275+
[GlobalCleanup]
276+
public void Cleanup()
277+
{
278+
try { Directory.Delete(_storePath, true); } catch { }
279+
}
280+
281+
[Benchmark(Description = "海量写入100万条(64B)")]
282+
public void MassWrite_64B()
283+
{
284+
var kvFile = Path.Combine(_storePath, $"mass64_{Guid.NewGuid():N}.kvd");
285+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
286+
for (var i = 0; i < 1_000_000; i++)
287+
store.Set($"key:{i}", _value64);
288+
}
289+
290+
[Benchmark(Description = "海量写入100万条(1024B)")]
291+
public void MassWrite_1024B()
292+
{
293+
var kvFile = Path.Combine(_storePath, $"mass1024_{Guid.NewGuid():N}.kvd");
294+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
295+
for (var i = 0; i < 1_000_000; i++)
296+
store.Set($"key:{i}", _value1024);
297+
}
298+
299+
[Benchmark(Description = "海量写入后读取100万条(64B)")]
300+
public void MassWriteThenRead_64B()
301+
{
302+
var kvFile = Path.Combine(_storePath, $"massrd64_{Guid.NewGuid():N}.kvd");
303+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
304+
for (var i = 0; i < 1_000_000; i++)
305+
store.Set($"key:{i}", _value64);
306+
for (var i = 0; i < 1_000_000; i++)
307+
store.Get($"key:{i}");
308+
}
309+
310+
[Benchmark(Description = "海量批量写入100万条(SetAll, 64B)")]
311+
public void MassSetAll_64B()
312+
{
313+
var kvFile = Path.Combine(_storePath, $"massall64_{Guid.NewGuid():N}.kvd");
314+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
315+
// 分批写入,每批 1000 条
316+
for (var batch = 0; batch < 1000; batch++)
317+
{
318+
var dict = new Dictionary<String, Byte[]?>(1000);
319+
for (var i = 0; i < 1000; i++)
320+
dict[$"key:{batch * 1000 + i}"] = _value64;
321+
store.SetAll(dict);
322+
}
323+
}
324+
325+
[Benchmark(Description = "海量数据搜索(100万条中搜索)")]
326+
public Int32 MassSearch()
327+
{
328+
var kvFile = Path.Combine(_storePath, $"masssrc_{Guid.NewGuid():N}.kvd");
329+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
330+
for (var i = 0; i < 1_000_000; i++)
331+
store.Set($"key:{i}", _value64);
332+
333+
var count = 0;
334+
foreach (var _ in store.Search("key:999*", 0, 100))
335+
count++;
336+
return count;
337+
}
338+
}
339+
340+
/// <summary>KV 引擎海量数据基准测试(1000万条)</summary>
341+
[MemoryDiagnoser]
342+
[Config(typeof(AntiViralConfig))]
343+
public class KvStoreMassData1000wBenchmark
344+
{
345+
private String _storePath = null!;
346+
private Byte[] _value64 = null!;
347+
348+
[GlobalSetup]
349+
public void Setup()
350+
{
351+
_storePath = Path.Combine(Path.GetTempPath(), $"NovaBench_KvMass1000w_{Guid.NewGuid():N}");
352+
Directory.CreateDirectory(_storePath);
353+
_value64 = new Byte[64];
354+
Random.Shared.NextBytes(_value64);
355+
}
356+
357+
[GlobalCleanup]
358+
public void Cleanup()
359+
{
360+
try { Directory.Delete(_storePath, true); } catch { }
361+
}
362+
363+
[Benchmark(Description = "海量写入1000万条(64B)")]
364+
public void MassWrite_64B()
365+
{
366+
var kvFile = Path.Combine(_storePath, $"mass64_{Guid.NewGuid():N}.kvd");
367+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
368+
for (var i = 0; i < 10_000_000; i++)
369+
store.Set($"key:{i}", _value64);
370+
}
371+
372+
[Benchmark(Description = "海量写入后读取1000万条(64B)")]
373+
public void MassWriteThenRead_64B()
374+
{
375+
var kvFile = Path.Combine(_storePath, $"massrd64_{Guid.NewGuid():N}.kvd");
376+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
377+
for (var i = 0; i < 10_000_000; i++)
378+
store.Set($"key:{i}", _value64);
379+
for (var i = 0; i < 10_000_000; i++)
380+
store.Get($"key:{i}");
381+
}
382+
383+
[Benchmark(Description = "海量批量写入1000万条(SetAll, 64B)")]
384+
public void MassSetAll_64B()
385+
{
386+
var kvFile = Path.Combine(_storePath, $"massall64_{Guid.NewGuid():N}.kvd");
387+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
388+
// 分批写入,每批 1000 条
389+
for (var batch = 0; batch < 10_000; batch++)
390+
{
391+
var dict = new Dictionary<String, Byte[]?>(1000);
392+
for (var i = 0; i < 1000; i++)
393+
dict[$"key:{batch * 1000 + i}"] = _value64;
394+
store.SetAll(dict);
395+
}
396+
}
397+
398+
[Benchmark(Description = "海量数据搜索(1000万条中搜索)")]
399+
public Int32 MassSearch()
400+
{
401+
var kvFile = Path.Combine(_storePath, $"masssrc_{Guid.NewGuid():N}.kvd");
402+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
403+
for (var i = 0; i < 10_000_000; i++)
404+
store.Set($"key:{i}", _value64);
405+
406+
var count = 0;
407+
foreach (var _ in store.Search("key:999*", 0, 100))
408+
count++;
409+
return count;
410+
}
411+
}

Benchmark/NovaCacheBenchmark.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,105 @@ public void MassWriteThenRead_64B()
234234
cache.Get<String>($"key:{i}");
235235
}
236236
}
237+
238+
/// <summary>NovaCache 嵌入模式海量数据基准测试(100万条)</summary>
239+
[MemoryDiagnoser]
240+
[Config(typeof(AntiViralConfig))]
241+
public class NovaCacheEmbeddedMassData100wBenchmark
242+
{
243+
private String _storePath = null!;
244+
private String _stringValue64 = null!;
245+
private String _stringValue1024 = null!;
246+
247+
[GlobalSetup]
248+
public void Setup()
249+
{
250+
_storePath = Path.Combine(Path.GetTempPath(), $"NovaBench_CacheMass100w_{Guid.NewGuid():N}");
251+
Directory.CreateDirectory(_storePath);
252+
_stringValue64 = new String('A', 64);
253+
_stringValue1024 = new String('A', 1024);
254+
}
255+
256+
[GlobalCleanup]
257+
public void Cleanup()
258+
{
259+
try { Directory.Delete(_storePath, true); } catch { }
260+
}
261+
262+
[Benchmark(Description = "嵌入模式海量写入100万条(64B)")]
263+
public void MassWrite_64B()
264+
{
265+
var kvFile = Path.Combine(_storePath, $"emass64_{Guid.NewGuid():N}.kvd");
266+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
267+
var cache = new NovaCache(store);
268+
for (var i = 0; i < 1_000_000; i++)
269+
cache.Set($"key:{i}", _stringValue64);
270+
}
271+
272+
[Benchmark(Description = "嵌入模式海量写入100万条(1024B)")]
273+
public void MassWrite_1024B()
274+
{
275+
var kvFile = Path.Combine(_storePath, $"emass1024_{Guid.NewGuid():N}.kvd");
276+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
277+
var cache = new NovaCache(store);
278+
for (var i = 0; i < 1_000_000; i++)
279+
cache.Set($"key:{i}", _stringValue1024);
280+
}
281+
282+
[Benchmark(Description = "嵌入模式海量写入后读取100万条(64B)")]
283+
public void MassWriteThenRead_64B()
284+
{
285+
var kvFile = Path.Combine(_storePath, $"emassrd64_{Guid.NewGuid():N}.kvd");
286+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
287+
var cache = new NovaCache(store);
288+
for (var i = 0; i < 1_000_000; i++)
289+
cache.Set($"key:{i}", _stringValue64);
290+
for (var i = 0; i < 1_000_000; i++)
291+
cache.Get<String>($"key:{i}");
292+
}
293+
}
294+
295+
/// <summary>NovaCache 嵌入模式海量数据基准测试(1000万条)</summary>
296+
[MemoryDiagnoser]
297+
[Config(typeof(AntiViralConfig))]
298+
public class NovaCacheEmbeddedMassData1000wBenchmark
299+
{
300+
private String _storePath = null!;
301+
private String _stringValue64 = null!;
302+
303+
[GlobalSetup]
304+
public void Setup()
305+
{
306+
_storePath = Path.Combine(Path.GetTempPath(), $"NovaBench_CacheMass1000w_{Guid.NewGuid():N}");
307+
Directory.CreateDirectory(_storePath);
308+
_stringValue64 = new String('A', 64);
309+
}
310+
311+
[GlobalCleanup]
312+
public void Cleanup()
313+
{
314+
try { Directory.Delete(_storePath, true); } catch { }
315+
}
316+
317+
[Benchmark(Description = "嵌入模式海量写入1000万条(64B)")]
318+
public void MassWrite_64B()
319+
{
320+
var kvFile = Path.Combine(_storePath, $"emass64_{Guid.NewGuid():N}.kvd");
321+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
322+
var cache = new NovaCache(store);
323+
for (var i = 0; i < 10_000_000; i++)
324+
cache.Set($"key:{i}", _stringValue64);
325+
}
326+
327+
[Benchmark(Description = "嵌入模式海量写入后读取1000万条(64B)")]
328+
public void MassWriteThenRead_64B()
329+
{
330+
var kvFile = Path.Combine(_storePath, $"emassrd64_{Guid.NewGuid():N}.kvd");
331+
using var store = new KvStore(new DbOptions { DefaultKvTtl = TimeSpan.Zero }, kvFile);
332+
var cache = new NovaCache(store);
333+
for (var i = 0; i < 10_000_000; i++)
334+
cache.Set($"key:{i}", _stringValue64);
335+
for (var i = 0; i < 10_000_000; i++)
336+
cache.Get<String>($"key:{i}");
337+
}
338+
}

0 commit comments

Comments
 (0)