Skip to content

Commit 8611ba2

Browse files
Add very basic benchmarks
1 parent ba72c29 commit 8611ba2

File tree

4 files changed

+164
-3
lines changed

4 files changed

+164
-3
lines changed

Lightning.Net.sln

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.4
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30204.135
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LightningDB", "src\LightningDB\LightningDB.csproj", "{C864E62B-5C53-479C-9A6F-6D086E1EC3DF}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LightningDB.Tests", "src\LightningDB.Tests\LightningDB.Tests.csproj", "{3A8F29FB-1110-4755-9700-4EB064BB463E}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecondProcess", "src\SecondProcess\SecondProcess.csproj", "{9621DE3C-6C1A-484F-8DE7-C8F4D6F71EA9}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecondProcess", "src\SecondProcess\SecondProcess.csproj", "{9621DE3C-6C1A-484F-8DE7-C8F4D6F71EA9}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LightningDB.Benchmarks", "src\LightningDB.Benchmarks\LightningDB.Benchmarks.csproj", "{C28814DF-2BC4-4F0A-AF13-86B8B0CB68C6}"
1113
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,8 +29,15 @@ Global
2729
{9621DE3C-6C1A-484F-8DE7-C8F4D6F71EA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{9621DE3C-6C1A-484F-8DE7-C8F4D6F71EA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{9621DE3C-6C1A-484F-8DE7-C8F4D6F71EA9}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{C28814DF-2BC4-4F0A-AF13-86B8B0CB68C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{C28814DF-2BC4-4F0A-AF13-86B8B0CB68C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{C28814DF-2BC4-4F0A-AF13-86B8B0CB68C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{C28814DF-2BC4-4F0A-AF13-86B8B0CB68C6}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE
3339
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {A9864EE9-A41B-4A4D-9CE9-595A2779AB44}
42+
EndGlobalSection
3443
EndGlobal
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Dynamic;
4+
using System.IO;
5+
using System.Runtime.InteropServices;
6+
using BenchmarkDotNet;
7+
using BenchmarkDotNet.Attributes;
8+
9+
using LightningDB;
10+
11+
using Microsoft.CodeAnalysis.CSharp.Syntax;
12+
13+
namespace LightningDB.Benchmarks
14+
{
15+
public abstract class BenchmarksBase
16+
{
17+
public LightningEnvironment Env { get; set; }
18+
public LightningDatabase DB { get; set; }
19+
20+
[GlobalSetup]
21+
public void GlobalSetup()
22+
{
23+
const string Path = "TestDirectory";
24+
25+
if (Directory.Exists(Path))
26+
Directory.Delete(Path, true);
27+
28+
Env = new LightningEnvironment(Path) {
29+
MaxDatabases = 1
30+
};
31+
32+
Env.Open();
33+
34+
using var tx = Env.BeginTransaction();
35+
DB = tx.OpenDatabase();
36+
37+
RunSetup();
38+
}
39+
40+
public abstract void RunSetup();
41+
42+
[GlobalCleanup]
43+
public void GlobalCleanup()
44+
{
45+
DB.Dispose();
46+
Env.Dispose();
47+
}
48+
}
49+
50+
[MemoryDiagnoser]
51+
public class WriteBenchmarks : BenchmarksBase
52+
{
53+
[Params(1, 10, 100)]
54+
public int BatchSize { get; set; }
55+
56+
57+
[Params(4, 8, 64, 256)]
58+
public int WriteSize { get; set; }
59+
60+
61+
//[Params(true, false)]
62+
[Params(true)]
63+
public bool SequentialKeys { get; set; }
64+
65+
public byte[] ValueBuffer { get; set; }
66+
public byte[][] KeyBuffers { get; set; }
67+
68+
69+
70+
public override void RunSetup()
71+
{
72+
ValueBuffer = new byte[WriteSize];
73+
KeyBuffers = new byte[BatchSize][];
74+
75+
if(SequentialKeys) {
76+
for(int i = 0; i < BatchSize; i++) {
77+
var key = new byte[4];
78+
MemoryMarshal.Write(key, ref i);
79+
KeyBuffers[i] = key;
80+
}
81+
}
82+
else {
83+
var random = new Random(0);
84+
var seen = new HashSet<int>(BatchSize);
85+
86+
for (int i = 0; i < BatchSize;) {
87+
var keyValue = random.Next(0, BatchSize);
88+
89+
if (seen.Add(keyValue)) {
90+
var key = new byte[4];
91+
MemoryMarshal.Write(key, ref keyValue);
92+
KeyBuffers[i++] = key;
93+
}
94+
else
95+
continue;
96+
}
97+
}
98+
}
99+
100+
101+
[Benchmark]
102+
public void Write()
103+
{
104+
using(var transaction = Env.BeginTransaction())
105+
{
106+
for (int i = 0; i < BatchSize; i++) {
107+
transaction.Put(DB, KeyBuffers[i], ValueBuffer);
108+
}
109+
}
110+
}
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\LightningDB\LightningDB.csproj" />
14+
</ItemGroup>
15+
16+
<ItemGroup Condition=" '$(OS)' == 'Unix' ">
17+
<None Include="../LightningDB/runtimes/osx/native/lmdb.dylib" CopyToOutputDirectory="PreserveNewest" />
18+
</ItemGroup>
19+
<ItemGroup Condition=" '$(Platform)' == 'x86' AND '$(OS)' != 'Unix' ">
20+
<None Include="../LightningDB/runtimes/win-x86/native/lmdb.dll" CopyToOutputDirectory="PreserveNewest" />
21+
</ItemGroup>
22+
<ItemGroup Condition=" ('$(Platform)' == 'x64' OR '$(Platform)' == 'AnyCPU') AND '$(OS)' == 'Windows_NT' ">
23+
<None Include="../LightningDB/runtimes/win-x64/native/lmdb.dll" CopyToOutputDirectory="PreserveNewest" />
24+
</ItemGroup>
25+
26+
27+
</Project>

src/LightningDB.Benchmarks/Main.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using BenchmarkDotNet.Running;
3+
using BenchmarkDotNet.Toolchains;
4+
5+
namespace LightningDB.Benchmarks {
6+
public static class Entry
7+
{
8+
public static void Main(string[] args)
9+
{
10+
BenchmarkRunner.Run<WriteBenchmarks>();
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)